Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Losing content-type when persisting to Azure blob storage

I’ve got an Azure function which persists files sent in the request to blob storage. For some reason — even though the files have a content-type — they’re being persisted with the application/octet-stream content-type, rather than say, image/jpeg.

var file = req.Form.Files["File"];
var blobClient = new BlobContainerClient(connection, containerName);
var blob = blobClient.GetBlobClient(fileName);
await blob.UploadAsync(file.OpenReadStream());

Any ideas why this is happening?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The file, when uploaded, will not naturally figure out the content type (or keep the existing one) – it needs to be manually set.

If you don’t manually set it at a blob level they will upload as a application/octet-stream as you’ve found.

Luckily it’s quite simple to do, access the properties and set it directly like:

blob.Properties.ContentType = "video/mp4";

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading