Set content type when uploading to Azure Blob Storage

我的未来我决定 提交于 2021-01-23 06:21:45

问题


I am uploading a static site using the Azure Blob storage client library.

        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)

This is working but the HTML file is downloading instead of displaying. This is because the content type is wrong: Content-Type: application/octet-stream.

Is there any way to set this using upload_blob?

Update:

To get this working, I needed this:


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


回答1:


Looking at the code here, one of the parameters to this method is content_settings which is of type ContentSettings. You can define content_type there.



来源:https://stackoverflow.com/questions/59752380/set-content-type-when-uploading-to-azure-blob-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!