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
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.
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.
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.
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.
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.
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[])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With