Setting Metadata On A Container And Blobs - Understanding Blob Storage - Part Five

Azure blob and containers allow you to set your own custom metadata. You can do the same either via a portal or through the C# application.

In this article, we will see how to do it using both methods.

Step 1

For containers, right-click the container and click on Edit Metadata which will open a window to add key-value pairs. Add your custom key-value pairs and click on Save.


Step 2

For Blobs, double-click on the blob or right-click on Blob properties, scroll down, and you can see a provision for adding key-value pairs; once added, click on Save to save the metadata.


Step 3

Add the following method to your class and call the method inside your main function to set the Metadata for the container. Here, we are creating static method called SetMetaData which will take in a CloudBlobContainer object through container reference. First, we are clearing out any existing key-value pairs with Metadata.Clear(), and in this code sample, we’re going to add a key-value pair, the owner as Mohammed Ramees and Updated as Current system time.

  1. static void SetMetaData(CloudBlobContainer container) {  
  2.     container.Metadata.Clear();  
  3.     container.Metadata.Add("Owner""Mohammed Ramees");  
  4.     container.Metadata["LastUpdated"] = DateTime.Now.ToString();  
  5.     container.SetMetadata();  
  6. }  

Step 4

To list the metadata within the application, add the following method to the class and call the same in the main method after the call for SetMetaData. This method is accepting a CloudBlobContainer object, with which we call the FetchAtttributes method to kind of load those key-value pairs into this object, and then looping over them and get the information out.
  1. static void GetMetaData(CloudBlobContainer container) {  
  2.     container.FetchAttributes();  
  3.     Console.WriteLine("Container MetaData: \n");  
  4.     foreach(var item in container.Metadata) {  
  5.         Console.WriteLine(string.Format("{0}: {1}", item.Key, item.Value));  
  6.     }  
  7. }  

The complete main method code should look like this.

  1. {  
  2.     CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnection"));  
  3.     CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();  
  4.     CloudBlobContainer container = blobClient.GetContainerReference("images");  
  5.     container.CreateIfNotExists(BlobContainerPublicAccessType.Blob);  
  6.     SetMetaData(container);  
  7.     GetMetaData(container);  
  8.     Console.ReadKey();  
  9. }  
  10. static void GetMetaData(CloudBlobContainer container) {  
  11.     container.FetchAttributes();  
  12.     Console.WriteLine("Container MetaData: \n");  
  13.     foreach(var item in container.Metadata) {  
  14.         Console.WriteLine(string.Format("{0}: {1}", item.Key, item.Value));  
  15.     }  
  16. }  
  17. static void SetMetaData(CloudBlobContainer container) {  
  18.     container.Metadata.Clear();  
  19.     container.Metadata.Add("Owner""Mi");  
  20.     container.Metadata["LastUpdated"] = DateTime.Now.ToString();  
  21.     container.SetMetadata();  
  22. }  
Step 5

Run the application and you can get the new metadata listed that we added.