【问题标题】:How do I generate a pre-signed URL in Google Cloud Function that links to a specific bucket GCS?如何在 Google Cloud Function 中生成链接到特定存储桶 GCS 的预签名 URL?
【发布时间】:2021-01-22 10:36:21
【问题描述】:

如何将服务账号生成的私钥文件添加到云功能进行认证?

以下代码返回身份验证凭据错误,它说我需要一个私钥来签署凭据。如何创建这个并传入云函数?

import datetime

from google.cloud import storage


def generate_download_signed_url_v4(bucket_name, blob_name):
    """Generates a v4 signed URL for downloading a blob.

    Note that this method requires a service account key file. You can not use
    this if you are using Application Default Credentials from Google Compute
    Engine or from the Google Cloud SDK.
    """
    # bucket_name = 'your-bucket-name'
    # blob_name = 'your-object-name'

    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(blob_name)

    url = blob.generate_signed_url(
        version="v4",
        # This URL is valid for 15 minutes
        expiration=datetime.timedelta(minutes=15),
        # Allow GET requests using this URL.
        method="GET",
    )

    print("Generated GET signed URL:")
    print(url)
    print("You can use this URL with any user agent, for example:")
    print("curl '{}'".format(url))
    return url

【问题讨论】:

  • 我今天提供了this answer。它也适用于云功能,您不需要服务帐户密钥文件(更安全)。够你吃吗?
  • @guillaumeblaquiere 仍然遇到相同的身份验证问题,“您需要私钥来签署凭据”......不知道该怎么办
  • 你的依赖版本是什么?可以分享一下吗?
  • 你找到答案了吗?

标签: python google-cloud-functions pre-signed-url signed-url


【解决方案1】:

我可以帮助您找到有关如何使用 Python 在 GCF 中创建签名 url 的信息。您需要使用Cloud Storage Python Client libraries,正如官方 GCP 文档中的 example 中所述。此外,Medium 上的 example 也可以帮助您。

【讨论】:

  • 感谢@Jaroslav --- 主要问题,是否不确定在哪里添加私钥文件,以便对其链接的云功能的服务帐户进行身份验证?
  • 应用程序使用服务帐户进行授权的 API 调用,授权为服务帐户本身。你需要使用私钥吗?
猜你喜欢
  • 2019-01-30
  • 2019-06-11
  • 1970-01-01
  • 2017-08-05
  • 2017-11-28
  • 2021-01-21
  • 2019-01-27
  • 2019-11-16
  • 1970-01-01
相关资源
最近更新 更多