Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get a blob's ContentType in Azure.Storage.Blobs?

I'm switching to the newer Azure.Storage.Blobs and want to serve up a blob as a file via API to a client.

In the now-deprecated Microsoft.Azure.Storage and Microsoft.Azure.Storage.Blob, I was able to do it like this:

using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;

    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = cloudBlobClient.GetContainerReference("foo");
    CloudBlockBlob blob = container.GetBlockBlobReference(blobId);
    var bar = blob.Properties.ContentType;

Now that I'm in Azure.Storage.Blobs, the setup looks the same, but I'm missing the punchline:

using Azure.Storage.Blobs;
    BlobContainerClient containerClient = new BlobContainerClient(storageConnectionString, containerName);
    BlobClient blobClient = containerClient.GetBlobClient("foo");

There's no analogous blockblob in the newer library, I can read the blobClient into a stream, but where do I get its content type without resorting to older libraries?

I'm hoping to be able to hit a FileResult, returning something that matches this signature:

File(Stream fileStream, string contentType, string fileName);
like image 544
Blue Avatar asked Oct 16 '25 23:10

Blue


1 Answers

You can fetch blob’s properties using GetBlobProperties and that will give you the content type of the blob. Something like (untested code):

var getBlobPropertiesResult = blobClient.GetProperties();
var contentType = getBlobPropertiesResult.Value.ContentType;
like image 77
Gaurav Mantri Avatar answered Oct 18 '25 13:10

Gaurav Mantri