Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set content type when uploading to Azure Blob Storage

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)

like image 334
Mick Avatar asked Jan 15 '20 13:01

Mick


People also ask

How do I change the content type of Microsoft Azure Blob storage?

To change the content type property of all blobs inside a container, simply right click on the blob container and click on “Set Properties (All Blobs)…” context menu item and follow the same procedure as above.

What kind of content is ideal for storing in Blob storage?

Blob storage is optimized for storing massive amounts of unstructured data, such as text or binary data. Blob storage is ideal for: Serving images or documents directly to a browser. Storing files for distributed access.

Can we change the type once the blob has been created?

Once the blob has been created, its type cannot be changed, and it can be updated only by using operations appropriate for that blob type, i.e., writing a block or list of blocks to a block blob, appending blocks to an append blob, and writing pages to a page blob.

What is blob content type?

The Blob object represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a ReadableStream so its methods can be used for processing the data. Blobs can represent data that isn't necessarily in a JavaScript-native format.


1 Answers

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.

like image 152
Gaurav Mantri Avatar answered Oct 12 '22 08:10

Gaurav Mantri