Home > Software design >  Losing content-type when persisting to Azure blob storage
Losing content-type when persisting to Azure blob storage

Time:09-18

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?

CodePudding user response:

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";

  • Related