【问题标题】:Set content type when uploading to Azure Blob Storage上传到 Azure Blob 存储时设置内容类型
【发布时间】:2020-05-02 06:39:06
【问题描述】:

我正在使用 Azure Blob 存储客户端库上传静态站点。

        blob_service_client = BlobServiceClient.from_connection_string(az_string)
        blob_client = blob_service_client.get_blob_client(container=container_name, blob=local_file_name)
        print("\nUploading to Azure Storage as blob:\n\t" + local_file_name)

        with open('populated.html', "rb") as data:
            test = blob_client.upload_blob(data, overwrite=True)

这是可行的,但 HTML 文件正在下载而不是显示。这是因为内容类型错误:Content-Type: application/octet-stream

有什么方法可以使用upload_blob 进行设置吗?

更新:

为了让它工作,我需要这个:


my_content_settings = ContentSettings(content_type='text/html')
blob_client.upload_blob(data, overwrite=True, content_settings=my_content_settings)

【问题讨论】:

  • 更新正是我所需要的。谢谢米克!
  • 感谢您的更新。试图在极其糟糕的文档中找到这一点浪费了一天时间。
  • 你是如何导入ContentSettings类的?
  • @harvpan from azure.storage.blob import BlobServiceClient, ContentSettings

标签: python azure azure-storage azure-blob-storage


【解决方案1】:

查看代码here,该方法的参数之一是content_settings,其类型为ContentSettings。你可以在那里定义content_type

【讨论】:

  • 如果您正在查看但没有看到上面的评论 ContentSettings 模型可从导入 from azure.storage.blob import BlobServiceClient, ContentSettings
猜你喜欢
  • 2021-04-18
  • 2021-09-17
  • 2016-11-11
  • 1970-01-01
  • 2021-02-27
  • 2013-03-27
  • 2014-08-23
  • 2020-08-01
  • 2012-04-19
相关资源
最近更新 更多