Robby
Robby

Reputation: 207

How retrieve Metadata from each blob in my Azure container?

I am having trouble accessing the metadata from the Azure container that I have created. There are 2 keys with 2 values, the first metadata is {key: cellNumber value:12345678} the second one is {key: homeNumber value:098876543}. I am trying to access the information in the metadata section of my blobs container but is giving me nothing. I try to get properties from here link but following those steps still provides me no value or key.

 var blobServiceClient = new BlobServiceClient(System.Environment.GetEnvironmentVariable("BlobConnection"));

        var containerClient = blobServiceClient.GetBlobContainerClient("myContainer");

        var blobs = containerClient.GetBlobs(prefix: "myFile.txt");

        foreach (var item in blobs)
        {
            //This prints nothing and the count = to zero
            Console.WriteLine(item.Metadata.Key);
             if(item.Metadata.Key == "cellPhone" {
              //Here goes some logic for my code
             }


        }

Upvotes: 0

Views: 1165

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136306

To get the metadata, please change the following line of code:

var blobs = containerClient.GetBlobs(prefix: "myFile.txt");

to

var blobs = containerClient.GetBlobs(prefix: "myFile.txt", traits: BlobTraits.Metadata);

then you should see metadata populated for the blobs. You can learn more about BlobTraits here: https://learn.microsoft.com/en-us/dotnet/api/azure.storage.blobs.models.blobtraits?view=azure-dotnet.

Upvotes: 2

Related Questions