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
,但在如何创建签名 PUT URL
以直接将对象直接上传到 Google Cloud Storage Bucket 方面无法取得任何进展。关于如何使用 Google Python 客户端的最新版本签署 URL 没有一致的文档。
我使用了google 云存储库。我推荐它,因为它得到了 google 的支持,并抽象了签名 url 舞蹈
中的一些复杂性
先领证
https://console.developers.google.com/
将证书保存到您的项目
安装google云库
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
我正在使用 google-api-python-client
,但在如何创建签名 PUT URL
以直接将对象直接上传到 Google Cloud Storage Bucket 方面无法取得任何进展。关于如何使用 Google Python 客户端的最新版本签署 URL 没有一致的文档。
我使用了google 云存储库。我推荐它,因为它得到了 google 的支持,并抽象了签名 url 舞蹈
中的一些复杂性先领证 https://console.developers.google.com/
将证书保存到您的项目
安装google云库
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