从在云运行中运行的管道中访问云存储

问题描述

我有在云运行中运行的管道 API,我想访问同一项目中谷歌存储桶中的文件。 API 运行良好,但我无法通过身份验证。我正在尝试使用 googleAuthR 和 gargle 库,但我做错了。

这是我的 api.R

#* @get /
function(){
  text <- "Hello,this is from a cloud run function...."
  return ( text )
}

#* @get /havetoken
function(){
  require(googleAuthR)
  require(googleCloudStorageR)
  require(gargle)
  text <- paste0("Do I have a token: ",gar_has_token())
  #gcs_auth(token = token_fetch())
  gar_auth( gar_gce_auth_default()  )
  text <- paste0(text,"<br>Do I have a token: ",gar_has_token())
  return ( gcs_list_buckets(projectId = "<MYPROJECT>") )
}

我的 dockerfile 是:

FROM gcr.io/gcer-public/googlecloudrunner:master
copY api.R .
ENTRYPOINT ["R","-e","pr <- plumber::plumb(commandArgs()[4]); pr$run(host='0.0.0.0',port=as.numeric(Sys.getenv('PORT')))"]
CMD ["api.R"]
docker build -t gcs_test1 .
docker image tag gcs_test1:latest gcr.io/<MYPROJECT>/gcscr:latest
gcloud run deploy gcs-test1 --image=gcr.io/<MYPROJECT>/gcscr:latest  --platform managed --allow-unauthenticated --service-account=gcs-sa@<MYPROJECT>.iam.gserviceaccount.com

我的服务帐户目前具有编辑者角色,但我想将其缩减为 Storage Admin + Cloud Run Service Agent。

如果您对如何访问 GCS 有任何建议,我将不胜感激。

解决方法

使用 googleAuthR::gar_gce_auth() 从运行在 Google Cloud 上的机器获取令牌