Set Content-Type of Media Files Stored on Blob

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.

Uploading blockblob and setting contenttype

Actually you don't have to call SetProperties method. In order to set content type while uploading the blob, just set the ContentType property before calling the upload method. So your code should be:

// Save image
CloudBlockBlob blockBlob = container.GetBlockBlobReference("blah.jpg");
blockBlob.Properties.ContentType = "image/jpg";
blockBlob.UploadFromByteArray(byteArrayThumbnail, 0, byteArrayThumbnail.Length);

and that should do the trick.

Preserve content type of a blob while copying from one container to another

You can use the set_blob_properties method:

from azure.storage.blob import BlockBlobService, ContentSettings

accountName="xxx"
accountKey="xxxx"

services = BlockBlobService(account_name=accountName,account_key=accountKey)

#set the content_type to whatever you need
settings = ContentSettings(content_type='application/pdf')

services.set_blob_properties(container_name,blob_name,content_settings=settings)

By the way, I use the python sdk azure-storage-blob==1.4.0, and when use copy_blob method, the content-type is also copied as "application/pdf"

can not able to set Content type

You are currently using

var postedFileExtension = Path.GetExtension(op.FileName);
var img = $@"{Guid.NewGuid()}" + postedFileExtension;

CloudBlockBlob blockBlob = container.GetBlockBlobReference(img);
CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(img);
cloudBlockBlob.Properties.ContentType = op.ContentType;

await blockBlob.UploadFromStreamAsync(op.OpenReadStream());
var blob = container.GetBlockBlobReference(img);

You are setting the ContentType property of a variable but not committing this change to Azure. Update the code to:

var postedFileExtension = Path.GetExtension(op.FileName);
var img = $@"{Guid.NewGuid()}" + postedFileExtension;

CloudBlockBlob blockBlob = container.GetBlockBlobReference(img);
blockBlob.Properties.ContentType = op.ContentType;

await blockBlob.UploadFromStreamAsync(op.OpenReadStream());
var blob = container.GetBlockBlobReference(img);

Console.WriteLine(blob.Properties.ContentType); // Output the contentType

Content Type being overwritten when sending to Azure Blob container

Thank you Gaurav Mantri. Adding your comment section suggestion to answer to help other community members.

Change the content-type header in the request from state.file.type to x-ms-blob-content-type

Set content type when uploading to Azure Blob Storage

Looking at the code here, one of the parameters to this method is content_settings which is of type ContentSettings. You can define content_type there.



Related Topics



Leave a reply



Submit