【问题标题】:How to properly use create_anonymous_client() function in google cloud storage python library for access on public buckets?如何正确使用谷歌云存储 python 库中的 create_anonymous_client() 函数访问公共存储桶?
【发布时间】:2023-03-04 02:45:01
【问题描述】:

我在谷歌云存储上创建了一个可公开列出的存储桶。如果我尝试在浏览器中列出存储桶对象,我可以看到所有键。我试图使用 create_anonymous_client() 函数,以便可以在 python 脚本中列出存储桶键。它给了我一个例外。我到处找了找,仍然找不到正确的方法来使用该功能。

from google.cloud import storage

client = storage.Client.create_anonymous_client()
a = client.lookup_bucket('publically_listable_bucket')
a.list_blobs()

我得到的异常:

ValueError: Anonymous credentials cannot be refreshed.

补充查询:我可以使用boto3列出和下载公共谷歌云存储桶的内容吗?如果可以,如何匿名?

【问题讨论】:

  • 如文档所述:“Boto 是适用于 Python 的 Amazon Web Services (AWS) 开发工具包”因此它不是为 Google 相关服务而设计的,您最好使用专为他们设计的图书馆。来源:boto3.readthedocs.io/en/latest/index.html

标签: python google-cloud-platform google-cloud-storage


【解决方案1】:

我也在苦苦挣扎,在网上的任何地方都找不到答案。事实证明,您只需使用 bucket() 方法即可访问存储桶。

我不知道为什么,但这种方法有时可能需要几秒钟。

client = storage.Client.create_anonymous_client()

bucket = client.bucket('publically_listable_bucket')

blobs = list(bucket.list_blobs())

【讨论】:

    【解决方案2】:

    此错误表示您尝试列出的存储桶未授予正确的权限。您必须将“Storage Object Viewer”或“Storage Legacy Bucket Reader”角色授予“allUsers”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-24
      • 2015-03-28
      • 2020-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-12
      相关资源
      最近更新 更多