【问题标题】:Get content_type from Google Cloud file从 Google Cloud 文件中获取 content_type
【发布时间】: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


    【解决方案1】:

    你应该试试

     blob = bucket.get_blob(blob_name)
     blob.content_type
    

    【讨论】:

      猜你喜欢
      • 2013-08-22
      • 2015-03-29
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      • 2019-11-17
      • 2017-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多