我正在使用google-api-python-client但无法在如何创建PUT URL仅用于将对象直接上传到 Google Cloud Storage Bucket 的签名方面取得任何进展。没有关于如何使用最新版本的 Google Python 客户端获取签名 URL 的一致文档。
1 回答
4
我使用了谷歌云存储库。我推荐它,因为它得到了 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
于 2019-01-03T21:45:16.207 回答