Book a Demo Free Trial

How to set the content type of Azure Storage Blobs based on its file extension in bulk?

Gaurav Mantri

Apr 20, 2021

Category: Azure Storage

In this post we will talk about content type property of a blob in Azure Blob Storage, what would happen if you set the content type property incorrectly (or don’t set it) and how you can use Cerebrata Cerulean to quickly set the content type property of blobs in bulk.

About Content Type Property

In Azure Blob Storage, for any blob one of an important system property is its content type. Content type becomes important because based on the value of this property, a user agent (typically a web browser) decides how to display the content.

Setting it to incorrect value or not setting it all can lead to unintended behavior when displaying the content.

Consider this for example. Let’s say you have uploaded a PNG image and forgot to set the content type property. In Azure Storage, if you do not set the content type property of the blob it is set as “application/octet-stream” by default. Now when you try to access that blob by using its URL in the browser’s address bar, instead of displaying the image in the browser it gets downloaded. This is because your web browser does not know how to deal with an object with “application/octet-stream” content type.

This problem can be easily fixed by simply changing the content type property of the blob to the correct one (image/png in this case). For one or two blobs, it is not a big deal but what would you do if you have a large number of blobs (thousands or may be millions). Setting the content type property of each blob individually is just not possible.

How Cerebrata Cerulean can help?

Cerebrata Cerulean has this feature that enables you to set content type properties on multiple blobs conveniently. For example, take a look at the screenshot below.

Here I have a blob container that has some blobs. I made the mistake of not setting the content type property of the blob while uploading them through code (Uploading them through Cerulean will always set the proper content type). As a result of this, all the blobs in my container have “application/octet-stream” as content type.

To change the content type of all the blobs, I would simply click on “Set Properties” button.

On the popup window that opens, all I have to do is select “Assign Content Type based on Blob Extension” option under “Content Type” and click on “Submit” button.

Cerebrata Cerulean will spawn a background task that will change the content type property of all the blobs based on the blob file extension as shown in the screenshot below.

To change content type property of selected blobs, simply select the blobs for which you wish to change the content type and follow the same procedure as above.

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.

As you can see that Cerebrata Cerulean makes it really easy for you to set the content type properties on multiple blobs thus making you more productive. BTW, this feature is not available in Microsoft Storage Explorer or any other tool available in the market. You can of course write some code to accomplish the same but then that would take your focus away from your core application.

Moreover, you can use the same feature to set other properties of blobs in bulk. Especially important is “Cache Control” property of the blob.

Apart from Azure Storage, Cerebrata Cerulean enables you to manage your Azure Cosmos DB accounts (SQL API, Gremlin API and Table API), Service Bus Namespaces, Cognitive Search Service accounts, Redis Cache accounts and much more. It is also cross platform so that you can manage your Azure resources from a platform of your choice – Windows, Mac or Linux. Please visit https://www.cerebrata.com to learn more.