Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Content-type of media files stored on Blob

We have a website hosted on Azure. It is media based, and we are using JWPlayer to playback media with HTTP pseudostreaming. The media files are stored on blob in 3 formats - mp4, ogg, webm.

The issue is the content type of media files is set as application/octet-stream for all types. Due to this there are some issues in media playback and progress bar.

How can I set the appropriate Content-type of files stored on blob (like - video/mp4, video/ogg, video/webm)?

I do not want to do it manually for each file by going in blob interface. There must be some other way to do it which I am not aware of. Perhaps a config file, settings file, etc sorts. Or perhaps a code block to set up the Content-type for all files stored in a folder.

Any suggestions? Thanks

like image 998
Rahul Patwa Avatar asked Apr 06 '12 07:04

Rahul Patwa


People also ask

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

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.

What type of data can be stored 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.

What type of files does Azure blob supports?

Blobs include images, text files, videos and audios. There are three types of blobs in the service offered by Windows Azure namely block, append and page blobs. Block blobs are collection of individual blocks with unique block ID.

What is the difference between blob and file storage?

In summary, the difference between the two storage services is that Azure Blob Storage is a store for objects capable of storing large amounts of unstructured data. On the other hand, Azure File Storage is a distributed, cloud-based file system.


2 Answers

This should work:

var storageAccount = CloudStorageAccount.Parse("YOURCONNECTIONSTRING"); var blobClient = storageAccount.CreateCloudBlobClient();  var blobs = blobClient     .GetContainerReference("thecontainer")     .ListBlobs(useFlatBlobListing: true)     .OfType<CloudBlockBlob>();  foreach (var blob in blobs) {     if (Path.GetExtension(blob.Uri.AbsoluteUri) == ".mp4")     {         blob.Properties.ContentType = "video/mp4";     }     // repeat ad nauseam     blob.SetProperties(); } 

Or set up a dictionary so you don't have to write a bunch of if statements.

like image 177
user94559 Avatar answered Sep 17 '22 00:09

user94559


Unfortunately, the accepted answer here is not currently working for the latest SDK (12.x.+)

With the latest SDK, the content type should be set via BlobHttpHeaders.

var blobServiceClient = new BlobServiceClient("YOURCONNECTIONSTRING"); var containerClient = blobServiceClient.GetBlobContainerClient("YOURCONTAINERNAME"); var blob = containerClient.GetBlobClient("YOURFILE.jpg");  var blobHttpHeader = new BlobHttpHeaders { ContentType = "image/jpeg" };   var uploadedBlob = await blob.UploadAsync(YOURSTREAM, new BlobUploadOptions { HttpHeaders = blobHttpHeader }); 

YOURSTREAM could be a new BinaryData(byte[])

like image 36
Mehmet Taha Meral Avatar answered Sep 21 '22 00:09

Mehmet Taha Meral