Search code examples
c#azureazure-storage

Setting and Retrieving metadata of blob in Azure Blob Storage


This link mentions about storing and retrieving metadata at container level. I was just wondering if its possible to set and retrieve metadata about a blob stored in a directory inside that container?


Solution

  • I was just wondering if its possible to set and retrieve metadata about a blob stored in a directory inside that container?

    Yes, you can certainly do so. Please see the sample code below:

            const string ConnectionString = "DefaultEndpointsProtocol=https;AccountName=account-name;AccountKey=account-key";
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);
    
            //Create the service client object for credentialed access to the Blob service.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    
            // Retrieve a reference to a container.
            CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
    
            CloudBlockBlob blob = container.GetBlockBlobReference("images/logo.png");
            blob.FetchAttributes();//Gets the properties & metadata for the blob.
            blob.Metadata.Add("Key", "Value");
            blob.SetMetadata();//Saves the metadata.