LatentDenis
LatentDenis

Reputation: 2991

Getting all values from BlockBlob Metadata starting with specific key string

I have a BlockBlob image with multiple metadata attributes. Some metadata attributes are unique. Others start with a key identifier (in this case "Tag").

How would I get all the values for metadata properties starting with a specific string?

Here's an example of a BlockBlob image:

BlockBlob Image with Tag Metadata

There are multiple "Tag" elements starting with an index of 0.

Here's the manual way of doing this, but you would need to know each and every index that exists:

CloudBlobContainer container = await GetCloudBlobClientAsync();
CloudBlobDirectory directory = container.GetDirectoryReference(path);

// Get max of 100 blobs including their metadata properties
var blobs = await directory.ListBlobsSegmentedAsync(false, BlobListingDetails.Metadata, 100, null, null, null);
foreach (var blob in blobs.Results) {
    var imageBlob = new CloudBlockBlob(blob.Uri);
    var blockBlob = imageBlob.GetBlockBlobReference(imageBlob.Name);
    await blockBlob.DownloadTextAsync();

    // This is what I'm trying to do..
    var tagArray = [ blockBlob.Metadata["Tag0"], blockBlob.Metadata["Tag1"], ... ]
    // Returns ["outdoor", "nature", "man" ...]

    // Bonus if it included the key names as well..
    var tagArrayWithKeys = [ "Tag0": blockBlob.Metadata["Tag0"], ... ];
    // Returns [ "Tag0": "outdoor", "Tag1": "nature", "Tag2": "man", ...]
}

How would you do this dynamically?

Upvotes: 0

Views: 558

Answers (1)

Hury Shen
Hury Shen

Reputation: 15734

As @Gaurav said, if you want to get one blob's all metadata, you can do a loop to get them. For example

CloudBlobContainer container = await GetCloudBlobClientAsync();
CloudBlobDirectory directory = container.GetDirectoryReference(path);



// Get max of 100 blobs including their metadata properties
var blobs = await directory.ListBlobsSegmentedAsync(false, BlobListingDetails.Metadata, 100, null, null, null);
foreach (var blob in blobs.Results) {
    var imageBlob = new CloudBlockBlob(blob.Uri);
    var blockBlob = imageBlob.GetBlockBlobReference(imageBlob.Name);
    await blockBlob.DownloadTextAsync();
    foreach(var r in blockBlob.Metadata){
     Console.WriteLine("Key: " + r.Key + " value: " + r.Value);

}

}

Besides, if you want to get the metadata filter by the keys, please refer to the following code

CloudBlobContainer container = await GetCloudBlobClientAsync();
CloudBlobDirectory directory = container.GetDirectoryReference(path);



// Get max of 100 blobs including their metadata properties
var blobs = await directory.ListBlobsSegmentedAsync(false, BlobListingDetails.Metadata, 100, null, null, null);
foreach (var blob in blobs.Results) {
    var imageBlob = new CloudBlockBlob(blob.Uri);
    var blockBlob = imageBlob.GetBlockBlobReference(imageBlob.Name);
    await blockBlob.DownloadTextAsync();
    var result = blockBlob.Metadata.where(k => k.Key.StartsWith("Tag"))
    foreach(var r in result){
     Console.WriteLine("Key: " + r.Key + " value: " + r.Value);

}
}

Upvotes: 1

Related Questions