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 fromstate.file.type
tox-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
Wcf - Design Parameter Decision
How to Click on the Radio Button Through the Element Id Attribute Using Selenium and C#
Difference Between Icomparable and Icomparer
Generate Class from Database Table
Using Iconfiguration in C# Class Library
Use Different Name for Serializing and Deserializing with JSON.Net
Nullable Object Must Have a Value
Extract Thumbnail for Any File in Windows
Print Rdlc Report Without Showing Reportviewer Control
Why Does the Is Operator Return False When Given Null
Invalid Cast from 'System.Int32' to 'System.Nullable'1[[System.Int32, Mscorlib]]
How to Use Custom Authorize Attribute for Roles as Well as a Specific User
The Requested Resource Does Not Support Http Method 'Get'