Python 访问谷歌存储桶的问题

时间:2021-02-06 01:20:26

标签: google-cloud-storage

使用相同的服务帐户,我可以毫无问题地列出 shell 中的对象,即 gsutil ls gs://{bucket-name} 工作正常。但是当我尝试使用下面的代码使用 Python 客户端时,它给了我一个 403 错误

import os
from google.cloud import storage

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "path-to-credential"
client = storage.Client()

for bucket in client.list_buckets():
    print(bucket)

# error
Forbidden: 403 GET https://storage.googleapis.com/storage/v1/b?project=project-name&projection=noAcl&prettyPrint=false: ml-service-account@project-name.iam.gserviceaccount.com does not have storage.buckets.list access to the Google Cloud project.

不确定我做错了哪一部分,提前致谢。

1 个答案:

答案 0 :(得分:0)

您的代码没有问题,错误指向您正在使用的服务帐户:

ml-service-account@project-name.iam.gserviceaccount.com does not have storage.buckets.list access to the Google Cloud project.

确保您的服务帐户具有“所有者”“编辑”“查看者”的角色strong> 或 “storage.admin” 角色在您项目的 IAM 中。