简体   繁体   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

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.

相关问题 如何在 Google 云平台 gcp 上托管 python 脚本? - How can I host a python script on Google cloud platform gcp? 如何获取 Google Cloud Platform blob (Python) 的唯一标识符? - How can I get the unique identifier for a Google Cloud Platform blob (Python)? 如何在 Google Cloud Platform 的 DataStore 中存储值? - How do I store values in DataStore in Google Cloud Platform? 我可以在Google Cloud Platform的Python应用程序中运行Webpack吗? - Can I run Webpack in a Python app on Google Cloud Platform? 如何使用适用于 Cloud Functions 的 Google Python 客户端获取 Google Cloud Functions 的列表? - How can I get a list of Google Cloud Functions using Google Python Client for Cloud Functions? 如何将辅助python文件上传到Google云平台? - How do I upload a secondary python file to google cloud platform? 如何使用python下载谷歌云平台上文件夹内的文件? - How can i download the files inside a folder on google cloud platform using python? 如何在Google Cloud Platform上使用Python进行尴尬的并行任务以最大化吞吐量? - How can I maximize throughput for an embarrassingly-parallel task in Python on Google Cloud Platform? 如何使用 python 从 google-cloud-platform 下载我的数据? - how can i download my data from google-cloud-platform using python? 谷歌云存储签名 url 分块下载在 python 中? - Google cloud storage signed url chunked download In python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM