繁体   English   中英

Google Cloud Platform:我如何获得使用Python将对象放入Google Cloud Store的签名URL

[英]Google Cloud Platform: How can I get a signed URL for putting an object to Google Cloud Store with Python

我正在使用google-api-python-client但是在仅将对象直接上传到Google Cloud Storage Bucket方面如何创建签名的PUT URL方面无法取得任何进展。 没有关于如何使用最新版本的Google Python客户端获取签名URL的一致文档。

我使用了谷歌云存储库。 我推荐它作为google支持的工具,并在签名的URL舞中抽象出一些复杂性

首先获得证书https://console.developers.google.com/

将证书保存到您的项目

安装谷歌云库

pip install google-cloud-storage==1.9.0

导入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)

完整文档https://googleapis.github.io/google-cloud-python/latest/storage/client.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM