【发布时间】:2018-02-16 19:59:45
【问题描述】:
我有两个 api 端点,一个从 http 请求中获取文件并使用 python api 将其上传到谷歌云存储桶,另一个再次下载它。在第一个视图中,我从 http 请求中获取文件内容类型并将其上传到存储桶,设置元数据:
from google.cloud import storage
file_obj = request.FILES['file']
client = storage.Client.from_service_account_json(path.join(
path.realpath(path.dirname(__file__)),
'..',
'settings',
'api-key.json'
))
bucket = client.get_bucket('storage-bucket')
blob = bucket.blob(filename)
blob.upload_from_string(
file_text,
content_type=file_obj.content_type
)
然后在另一个视图中,我下载文件:
...
bucket = client.get_bucket('storage-bucket')
blob = bucket.blob(filename)
blob.download_to_filename(path)
如何访问我之前设置的文件元数据 (content_type) ?它在 blob 对象上不再可用,因为实例化了一个新对象,但它仍然保存该文件。
【问题讨论】:
标签: python google-cloud-platform google-cloud-storage