我如何在谷歌云功能中生成一个预先签名的URL链接到特定的bucket GCS?

2024-04-20 09:09:34 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

Tags: 函数namefromurlforgetbucketstorage
1条回答
网友
1楼 · 发布于 2024-04-20 09:09:34

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

相关问题 更多 >