Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing metadata from azure storage blob

Tags:

c#

metadata

azure

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.

like image 878
dinotom Avatar asked Apr 19 '17 17:04

dinotom


People also ask

Can we query data from blob storage?

The possibility to query information on blob storage and other sources easily with a Serverless Pool has many uses. One of them is ad-hoc analysis queries, and the UI feature to view the result in many different ways contributes even more to this.

How do I view the contents of Azure blob storage?

View a blob container's contentsOpen Storage Explorer. In the left pane, expand the storage account containing the blob container you wish to view. Expand the storage account's Blob Containers. Right-click the blob container you wish to view, and - from the context menu - select Open Blob Container Editor.

What is a blob storage container metadata?

The metadata is a set of key-value pairs which you specify for a blob storage resource. As the name suggests, the metadata is just for your purpose. It does not affect the behavior of the resource. The metadata key-value pairs are like HTTP headers, so all rules of HTTP headers name value pair apply here.


2 Answers

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.

like image 77
Gaurav Mantri Avatar answered Nov 05 '22 08:11

Gaurav Mantri


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

like image 26
user803952 Avatar answered Nov 05 '22 06:11

user803952