Blue
Blue

Reputation: 217

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

Upvotes: 4

Views: 2667

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136226

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;

Upvotes: 5

Related Questions