【问题标题】:Python azure uploaded file content type changed to application/octet-streamPython azure 上传的文件内容类型更改为 application/octet-stream
【发布时间】:2018-03-18 15:57:22
【问题描述】:

我正在使用 python Azure sdk。当文件上传时,其内容类型更改为 application/octet-stream。

我想为 PNG 图像设置其默认内容类型,例如 image/png

我正在使用以下方法上传文件put_block_blob_from_path()

有没有办法保留默认的内容类型?谢谢

【问题讨论】:

    标签: python azure


    【解决方案1】:

    如果未提供任何内容,Azure blob 将使用默认值“application/octet-stream”。为了获得正确的 mimetypes,这是我对我的烧瓶应用程序所做的:

        @app.route('/', methods=['GET', 'POST'])
    def upload_file():
            if request.method == 'POST':
                f = request.files['file']
                mime_type = f.content_type
                print (mime_type)
                print (type(f))
                try:
                    blob_service.create_blob_from_stream(container, f.filename, f,
                    content_settings=ContentSettings(content_type=mime_type))
    

    mime_type 被传递给 ContentSettings 以获取上传到 azure blob 的文件的当前 mimetypes。

    【讨论】:

      【解决方案2】:

      您可以像这样显式设置 content-type 属性:

      from azure.storage.blob import ContentSettings
      
      block_blob_service.create_blob_from_path(
          'mycontainer',
          'myblockblob',
          'mypngimage.png',
          content_settings=ContentSettings(content_type='image/png')
      )
      

      来源: https://docs.microsoft.com/en-us/azure/storage/blobs/storage-python-how-to-use-blob-storage

      【讨论】:

      • 要添加到这个出色的答案中,blob 的内容类型属性是可选的,如果未设置,则默认为 application/octet-stream
      猜你喜欢
      • 2019-10-16
      • 2018-10-05
      • 2018-09-11
      • 2021-11-15
      • 2019-04-01
      • 2020-08-01
      • 2018-10-27
      • 1970-01-01
      • 2012-04-16
      相关资源
      最近更新 更多