Blob.generate_signed_url()未能成为AttributeError

Mat*_*tti 7 python google-cloud-storage google-compute-engine google-cloud-python

因此,我正在尝试使用google-cloud-storagePython库(https://googlecloudplatform.github.io/google-cloud-python/latest/storage/blobs.html)为我的Google云端存储对象生成临时的全局可读URL - 更具体地说, Blob.generate_signed_url()方法.我是在命令行Python脚本中的Compute Engine实例中执行此操作.我一直收到以下错误:

AttributeError: you need a private key to sign credentials.the credentials you are currently using <class 'oauth2cl
ient.service_account.ServiceAccountCredentials'> just contains a token. see https://google-cloud-python.readthedocs
.io/en/latest/core/auth.html?highlight=authentication#setting-up-a-service-account for more details.
Run Code Online (Sandbox Code Playgroud)

我知道在GCE(https://github.com/GoogleCloudPlatform/google-auth-library-python/issues/50)中执行此操作存在问题,但我已按照此处的说明创建了新的服务帐户凭据:https://cloud.google.com/storage/docs/access-control/create-signed-urls-program和我的key.json文件肯定包含私钥.我仍然看到那个错误.

这是我的代码:

keyfile = "/path/to/my/key.json"
credentials = ServiceAccountCredentials.from_json_keyfile_name(keyfile)
expiration = timedelta(3) # valid for 3 days
url = blob.generate_signed_url(expiration, method="GET",
                               credentials=credentials) 
Run Code Online (Sandbox Code Playgroud)

我已经阅读了问题跟踪器这里https://github.com/GoogleCloudPlatform/google-cloud-python/issues?page=2&q=is%3Aissue+is%3Aopen并没有任何相关的跳出来所以我假设这应该工作.看不出这里出了什么问题.

小智 7

目前,如果blob.generate_signed_url不明确引用凭据,则无法使用。(来源:谷歌云-Python文档)。但是,你可以做一个变通方法,因为看到这里,其中包括:

signing_credentials = compute_engine.IDTokenCredentials(
    auth_request,
    "",
    service_account_email=credentials.service_account_email
)
signed_url = signed_blob_path.generate_signed_url(
    expires_at_ms,
    credentials=signing_credentials,
    version="v4"
)
Run Code Online (Sandbox Code Playgroud)


tom*_*n4a 6

我遇到了同样的问题。最终通过直接从服务帐户 json 启动存储客户端来修复它。

storage_client = storage.Client.from_service_account_json('path_to_service_account_key.json')
Run Code Online (Sandbox Code Playgroud)

我知道我参加聚会迟到了,但希望这会有所帮助!

  • 你救了我的命!对于将来阅读本文的其他人,我发现这仅适用于创建 v2 签名 URL,当使用 v4 时,这不适用 (3认同)
  • 如果我不想在 GIT 中保存 sv 帐户密钥怎么办?有办法吗? (2认同)