如何在Google Cloud Function中生成链接到特定bucket GCS的预签名URL



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

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

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

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

相关内容

  • 没有找到相关文章

最新更新