Set Content-type of media files stored on Blob Set Content-type of media files stored on Blob azure azure

Set Content-type of media files stored on Blob


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();blobHttpHeader.ContentType = "image/png"; var uploadedBlob = await blob.UploadAsync(YOURSTREAM, blobHttpHeader);


This is work example to upload video to Azure Blob Storage with right Content-Type:

public static String uploadFile(     CloudBlobContainer container,String blobname, String fpath) {    CloudBlockBlob blob;    try {        blob = container.getBlockBlobReference(blobname);        File source = new File(fpath);        if (blobname.endsWith(".mp4")) {            System.out.println("Set content-type: video/mp4");            blob.getProperties().setContentType("video/mp4");        }        blob.upload(new FileInputStream(source), source.length());        return blob.getUri().toString();    } catch (URISyntaxException e) {        // TODO Auto-generated catch block        e.printStackTrace();    } catch (StorageException e) {        // TODO Auto-generated catch block        e.printStackTrace();    } catch (FileNotFoundException e) {        // TODO Auto-generated catch block        e.printStackTrace();    } catch (IOException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }    return null;}