首页 > 解决方案 > 在没有凭据的情况下使用 Python Google Storage Client

问题描述

我正在使用Python Google Storage Client,但是我使用的是具有公共读/写访问权限的存储桶。(我知道这通常是一个糟糕的主意,但我有一个罕见的用例,它很好)。

当我尝试检索一些文件时,我得到一个DefaultCredentialsError.

BUCKET_NAME = 'my-public-bucket-name'
storage_client = storage.Client()
bucket = storage_client.get_bucket(BUCKET_NAME)

def list_blobs(prefix, delimiter=None):
    blobs = bucket.list_blobs(prefix=prefix, delimiter=delimiter)

    print('Blobs:')
    for blob in blobs:
        print(blob.name)

具体错误如下:

google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started

该页面建议使用 Oath 或其他令牌,但我不应该需要这些,因为我的存储桶是公开的?我可以向 chrome 中的存储桶发出 HTTP 请求并接收数据。

我应该如何解决这个问题?我可以提供默认或空凭据吗?

标签: pythongoogle-cloud-platformgoogle-cloud-storagebucket

解决方案


来自“云存储认证”:

您在 Cloud Storage 中执行的大部分操作都必须经过身份验证。唯一的例外是对允许匿名访问的对象的操作。allUsers如果组具有READ权限,则可以匿名访问对象。该allUsers组包括 Internet 上的任何人。


推荐阅读