首页 > 解决方案 > 即使环境变量设置正确,谷歌存储也没有使用服务帐户

问题描述

我试图在 Jupyter Notebook 中使用以下命令将两个文件保存到 GCP Storage:

!gsutil cp ./dist/my_custom_code-0.1.tar.gz gs://$BUCKET_NAME/custom_prediction_routine_tutorial/my_custom_code-0.1.tar.gz
!gsutil cp model.h5 preprocessor.pkl gs://$BUCKET_NAME/custom_prediction_routine_tutorial/model/

存储桶已正确创建,因为我可以在 GCP 的存储桶列表中看到它。同样在存储桶的权限中,我可以看到创建的服务帐户。另外,我确保通过运行设置环境变量:

export GOOGLE_APPLICATION_CREDENTIALS="/home/george/Documents/Credentials/prediction-routine-new-b7a445077e61.json"

这可以通过在 Python 中运行来验证:

import os

print('Credendtials from environ: {}'.format(os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')))

这表明:

Credentials from environ: /home/george/Documents/Credentials/prediction-routine-new-b7a445077e61.json

而且我确实将json文件存储在指定位置。但是,当我尝试使用顶部显示的命令保存文件时,我不断收到以下错误消息:

AccessDeniedException: 403 george***@gmail.com does not have storage.objects.list access to the Google Cloud Storage bucket.
Copying file://model.h5 [Content-Type=application/octet-stream]...
AccessDeniedException: 403 george***@gmail.com does not have storage.objects.create access to the Google Cloud Storage object.

那么问题来了,为什么 Google Storage 不使用我的服务帐户并继续使用我的用户帐户?


更新

正如@Hao Z 指出的那样,在激活项目的服务帐户后,GCP 现在正在使用我的服务帐户。但是,我确实为此服务帐户设置了权限...

IAM 权限


更新 2

这似乎是一个已知问题:https ://github.com/GoogleCloudPlatform/gsutil/issues/546

标签: pythonauthenticationgoogle-cloud-platformgsutil

解决方案


检查如何将服务帐户与 gsutil 一起使用,以上传到 CS + BigQuery

相关位:

Download service account key file, and put it in e.g. /etc/backup-account.json
gcloud auth activate-service-account --key-file /etc/backup-account.json

或者您可以执行 gsutil -i 来模拟服务帐户。使用“gsutil help creds”获取更多信息。我猜 env 变量仅由 Python SDK 使用,而不是由 CLI 使用。


推荐阅读