Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I setup ContentType of Azure Blob correctly?

In my Web API I use the following code for creating the blob:

        var container = Client.GetContainerReference(DefaultContainer);
        var blockBlob = container.GetBlockBlobReference(blobName);

        blockBlob.UploadFromStream(fileStream);

        blockBlob.Properties.ContentType = "video/mp4";
        blockBlob.SetProperties();

I need the ContentType header value to be video/mp4 when the file is downloaded from outside.

However, when I downloading that file with external link, Azure doesn't add corresponding ContentType. (It doesn't attach actually any).

So, how can I achieve that?

like image 347
Agat Avatar asked Oct 26 '25 14:10

Agat


1 Answers

You need to get the properties/metadata first before updating them, so your code should be:

var container = Client.GetContainerReference(DefaultContainer);
var blockBlob = container.GetBlockBlobReference(blobName);
blockBlob.UploadFromStream(fileStream);

// Set the content type
blockBlob.FetchAttributes();
blockBlob.Properties.ContentType = "text/html";
blockBlob.SetProperties();
like image 137
Thomas Avatar answered Oct 29 '25 08:10

Thomas