2

如何将使用服务帐户生成的私钥文件添加到云功能进行身份验证?

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

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
4

1 回答 1

0

我可以帮助您找到有关如何使用 Python 在 GCF 中创建签名 url 的信息。您需要使用Cloud Storage Python 客户端库,如官方 GCP 文档中此示例中所述。Medium上的这个例子也可以帮助你。

于 2020-10-07T14:14:11.270 回答