【问题标题】:How can I use the GOOGLE_APPLICATION_CREDENTIALS environment variable to sign a Cloud Storage URL?如何使用 GOOGLE_APPLICATION_CREDENTIALS 环境变量来签署 Cloud Storage URL?
【发布时间】:2019-09-01 13:36:12
【问题描述】:

如何使用implicit GOOGLE_APPLICATION_CREDENTIALS environment variable provided to a Cloud Function 签署 Cloud Storage URL?

当存在GOOGLE_APPLICATION_CREDENTIALS 环境变量时,以下内容在本地工作,但在云函数内部运行时会因KeyError 而失败。

google_credentials = service_account.Credentials.from_service_account_file(
    os.environ['GOOGLE_APPLICATION_CREDENTIALS']
)
client_email = google_credentials.service_account_email
credential_scope = '{}/auto/storage/goog4_request'.format(datestamp)
credential = '{}/{}'.format(client_email, credential_scope)
# ...
signature = binascii.hexlify(
    google_credentials.signer.sign(string_to_sign)
).decode()

有问题的云功能将部署在多个环境中,所以我想避免实际部署凭据文件——确实工作。

如果相关,我使用 Python 3.7 并基于 this 示例解决我的问题。

【问题讨论】:

    标签: python-3.x google-cloud-storage google-cloud-functions


    【解决方案1】:

    在部署您的函数时,应该不需要 GOOGLE_APPLICATION_CREDENTIALS。请查看此 [1] 以了解如何处理经过身份验证的请求。

    从 [2] 中可以看出,云存储客户端库可能需要 IAM (iam.googleapis.com) API 和 iam.serviceAccounts.signBlob 权限。尽管 Cloud Functions 具有“默认应用程序凭据”,但它(通常)不包括 iam.serviceAccounts.signBlob 权限。

    正如您在文档中看到的那样,您还需要确保您的服务帐户具有适当的角色。您还可以选择您的函数将使用哪个服务帐户来运行。

    如果这些信息有助于解决您的问题,请告诉我。


    [1]https://cloud.google.com/functions/docs/writing/http#authentication_and_cors
    [2]https://cloud.google.com/functions/docs/writing/http#uploading_files_via_cloud_storage

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 2022-11-08
      相关资源
      最近更新 更多