dinotom
dinotom

Reputation: 5162

Accessing metadata from azure storage blob

I can't seem to find a method to access individual blob metadata for blobs in Azure Storage.

FetchAttributes only works on the whole container. My method returns a list of blobs which match the parameters I set. I then need to iterate through that list and retrieve some metadata from each of those blobs but I am not finding any methods to do so.

It seems like a lot of overhead, but should I be Fetching those attributes when I create the container object, and then filtering for the blob list?

So, I figured I'd try that,

 public static IEnumerable<GalleryPhoto> GetGalleryPhotos(string folderPath)
 {

        var container = CreateAzureContainer(containerName, false);
        container.FetchAttributes();
        var blobDirectory = container.GetDirectoryReference(folderPath);
        var photoGalleries = new List<GalleryPhoto>();
        var blobs = blobDirectory.ListBlobs().ToList();

       ...rest of code
  }

The blob objects in blobs, show 0 for the metadata count. Each of the items HAVE metadata, verified by looking at the properties in Azure Storage Explorer for each blob.

Any help appreciated.

Upvotes: 9

Views: 10570

Answers (2)

Gaurav Mantri
Gaurav Mantri

Reputation: 136369

It is entirely possible to fetch the metadata in result when listing blobs. What you would need to do is specify BlobListingDetails parameter in ListBlobs method call and specify BlobListingDetails.Metadata there. What this will do is include metadata for each blob in the response. So your code would be:

public static IEnumerable<GalleryPhoto> GetGalleryPhotos(string folderPath)
 {

        var container = CreateAzureContainer(containerName, false);
        container.FetchAttributes();
        var blobDirectory = container.GetDirectoryReference(folderPath);
        var photoGalleries = new List<GalleryPhoto>();
        var blobs = blobDirectory.ListBlobs(false, BlobListingDetails.Metadata).ToList();

       ...rest of code
  }

Do give this a try. It should work.

Upvotes: 13

user803952
user803952

Reputation: 894

var blobs = container.ListBlobs().OfType<CloudBlockBlob>().ToList();
foreach (var blob in blobs)
{
    blob.FetchAttributes(); //Now the metadata will be populated
}

See https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.blob.cloudblockblob.fetchattributes%28v=azure.10%29.aspx?f=255&MSPPError=-2147217396

Upvotes: 5

Related Questions