【发布时间】:2020-09-23 20:31:18
【问题描述】:
我被要求与外部谷歌存储桶进行集成,我收到了一个凭据 json,
在尝试做的时候
gsutil ls gs://bucket_name(在使用 creds json 配置自己之后)我收到了有效的响应,以及当我尝试将文件上传到存储桶时。
尝试使用 Python3 时,它不起作用:
在使用google-cloud-storage==1.16.0(也尝试了较新的版本)时,我正在做:
project_id = credentials_dict.get("project_id")
credentials = service_account.Credentials.from_service_account_info(credentials_dict)
client = storage.Client(credentials=credentials, project=project_id)
bucket = client.get_bucket(bucket_name)
但是在get_bucket 行,我得到:
google.api_core.exceptions.Forbidden: 403 GET https://www.googleapis.com/storage/v1/b/BUCKET_NAME?projection=noAcl: USERNAME@PROJECT_ID.iam.gserviceaccount.com does not have storage.buckets.get access to the Google Cloud Storage bucket.
我正在与之集成的外部合作伙伴说用户设置正确,并证明他们表明我可以使用gsutil 执行操作。
你能帮忙吗?知道可能是什么问题吗?
【问题讨论】:
-
credentials_dict是什么?对于from_service_account_info()调用,它必须是格式化的服务帐户密钥文件的内容。例如。来自文档:service_account_info = json.load(open('service_account.json')) credentials = service_account.Credentials.from_service_account_info( service_account_info) -
抱歉格式化,如果是这种情况,我会把它作为答案,只是想澄清一下,没有什么是 credentials_dict 首先...
-
另外,您为服务帐户设置了哪些权限? gsutil 默认使用
gcloud init设置的用户凭据,因此您的用户可能拥有比您的服务帐户更多的权限。 -
你应该配置你的谷歌可以 python sdk 并使用正确的凭据初始化它。
标签: python python-3.x google-cloud-storage gsutil