Set content type when uploading to Azure Blob Storage
Asked Answered
P

1

16

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)

Pricket answered 15/1, 2020 at 13:25 Comment(4)
The update was exactly what I needed. Thanks Mick!Afreet
Thanks for the update. Wasted a day by trying to find this in the insanely bad docs.Lippmann
How did you import the class ContentSettings ?Apelles
@Apelles from azure.storage.blob import BlobServiceClient, ContentSettingsWaly
A
11

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.

Arytenoid answered 15/1, 2020 at 13:34 Comment(1)
in case you are looking and do not see the comment above the ContentSettings model is available from the import from azure.storage.blob import BlobServiceClient, ContentSettingsWaly

© 2022 - 2024 — McMap. All rights reserved.