Get Azure BlockBlob Content Type
Asked Answered
B

3

5

I am trying to get "Content Type" from Azure BlockBlob. It's seems like not work.

enter image description here

This file's "Content Type" is "image/jpeg" as you see.

            var cloudConn = System.Configuration.ConfigurationManager.ConnectionStrings["StoreAccount"].ConnectionString;

            var storageAccount = CloudStorageAccount.Parse(cloudConn);
            var blobClient = storageAccount.CreateCloudBlobClient();

            var container = blobClient.GetContainerReference("containername");

            var blocBlob = container.GetBlockBlobReference("009fc790-2e8e-4b59-bbae-3b5e2e845a3b");

And it always returns empty as you see in this picture:

enter image description here

Blest answered 18/11, 2016 at 14:51 Comment(1)
See this post (RE: FetchAttributes): #22063148Tonjatonjes
S
10
var blocBlob = container.GetBlockBlobReference("009fc790-2e8e-4b59-bbae-3b5e2e845a3b");

Code above simply creates an instance of CloudBlockBlob and initializes it with default properties. You would need to fetch the blob properties (as mentioned in the answer included in the comment above) and then you will see the properties filled up. To fetch blob properties, you would need to call FetchAttributes() method.

var blocBlob = container.GetBlockBlobReference("009fc790-2e8e-4b59-bbae-3b5e2e845a3b");
blocBlob.FetchAttributes();

Then you should be able to see the content type property of the blob.

Settlings answered 18/11, 2016 at 15:45 Comment(1)
Great answer. For clarity's sake, after calling FetchAttributes(), you will then have access to the Properties collection and can get the Content Type by simply calling blocBlob.Properties.ContentType.Cacie
N
5

To get the blob properties you have to first fetch the attributes:

blob.FetchAttributes()

Then you'll be able to get the content type through:

blob.Properties.ContentType
Nursling answered 18/11, 2016 at 15:46 Comment(0)
T
0

Another way to get it is GetBlobReferenceFromServer or GetBlobReferenceFromServerAsync. This returns an ICloudBlob, which you can do:

var blob = container.GetBlobReferenceFromServer("009fc790-2e8e-4b59-bbae-3b5e2e845a3b");
string contentType = blob.Properties.ContentType;

Note, this makes a round trip to the server and if the blob does not exist, it will throw an exception.

Tautologism answered 5/4, 2019 at 18:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.