【发布时间】: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