谷歌云平台:我如何获得一个签名的URL,以便使用Python将对象放入谷歌云存储

谷歌云平台:我如何获得一个签名的URL,以便使用Python将对象放入谷歌云存储,python,google-cloud-platform,google-cloud-storage,google-api-client,Python,Google Cloud Platform,Google Cloud Storage,Google Api Client,我正在使用googleapi python客户端,但在如何创建签名PUT URL以直接将对象上传到google云存储桶方面没有任何进展。关于如何使用最新版本的Google Python客户端获取签名URL,没有一致的文档。我使用了Google云存储库。我推荐它,因为它得到了谷歌的支持,并且抽象了签名url舞蹈中的一些复杂性 先拿到证书 将证书保存到项目中 安装谷歌云库 pip install google-cloud-storage==1.9.0 导入生成签名url和google.stora

我正在使用
googleapi python客户端
,但在如何创建签名
PUT URL
以直接将对象上传到google云存储桶方面没有任何进展。关于如何使用最新版本的Google Python客户端获取签名URL,没有一致的文档。

我使用了Google云存储库。我推荐它,因为它得到了谷歌的支持,并且抽象了签名url舞蹈中的一些复杂性

先拿到证书

将证书保存到项目中

安装谷歌云库

pip install google-cloud-storage==1.9.0
导入生成签名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)
完整文档

你是最棒的!感谢您结束了我为找到此解决方案而进行的斗争!这种方法的问题在于,无论bucket的CORS选项是什么,响应中都不包括CORS头。正在寻找解决方案,可以是签署简单POST url,也可以是xml/json url