[英]Google Cloud Platform: How can I get a signed URL for putting an object to Google Cloud Store with Python
I'm using google-api-python-client
but can't make any progress in how to create signed PUT URL
just for direct uploading object to Google Cloud Storage Bucket. 我正在使用
google-api-python-client
但是在仅将对象直接上传到Google Cloud Storage Bucket方面如何创建签名的PUT URL
方面无法取得任何进展。 There is no consistent documentation on how I can get signed URL using the last version of the Google Python client. 没有关于如何使用最新版本的Google Python客户端获取签名URL的一致文档。
I used the google cloud storage library. 我使用了谷歌云存储库。 I recommend it as its supported by google and abstracts some of the complexities in the signed url dance
我推荐它作为google支持的工具,并在签名的URL舞中抽象出一些复杂性
First get a certificate https://console.developers.google.com/ 首先获得证书https://console.developers.google.com/
Save the certificate to your project 将证书保存到您的项目
Install google cloud library 安装谷歌云库
pip install google-cloud-storage==1.9.0
Import generate_signed_url and google.storage and then initialize storage client with the certificate and access the bucket 导入generate_signed_url和google.storage,然后使用证书初始化存储客户端并访问存储桶
from google.cloud.storage._signing import generate_signed_url
from google.cloud import storage
client = storage.Client.from_service_account_json('path/to/certificate.json')
expiration = datetime.datetime.now() + datetime.timedelta(days=1)
API_ACCESS_ENDPOINT = 'https://storage.googleapis.com'
canonical_resource = bucketpath + "resource.jpeg"
url = generate_signed_url(
client._credentials, resource=canonical_resource,
api_access_endpoint=API_ACCESS_ENDPOINT,
expiration=expiration, method="PUT",
content_type="jpeg"
)
print(url)
full documentation https://googleapis.github.io/google-cloud-python/latest/storage/client.html 完整文档https://googleapis.github.io/google-cloud-python/latest/storage/client.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.