How to set Metadata on BlockBlob via REST API using C#

Sathish Kumar Rajendiran 1 Reputation point
2022-02-21T08:48:31.783+00:00

I want to set custom metadata for Blob on Azure using the Azure REST API. I have tried to add below header in http request, but its failing.
request.Headers.Add("x-ms-meta-project", "TestMeta123");

176277-image.png

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,786 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. shiva patpi 13,251 Reputation points Microsoft Employee
    2022-02-21T20:18:04.057+00:00

    Hello @Sathish Kumar Rajendiran ,
    I just tried the same piece of code which went through successfully.
    Where exactly you are getting the error and what is the failure error message? For me It went past through that debug point. (see below)

    176543-image.png

    Below is Non-REST API Code:

    static void SetCustomMetaDataOnBlockBlob()
    {
    string connectionString = "DefaultEndpointsProtocol=https;AccountName=<straccount>;AccountKey=<key>==;EndpointSuffix=core.windows.net";
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = cloudBlobClient.GetContainerReference("test2");
    CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference("6.txt");
    MemoryStream msWrite = new MemoryStream(Encoding.UTF8.GetBytes("custommetadata"));
    msWrite.Position = 0;
    cloudBlockBlob.Metadata["test"] = "TestMeta123";
    using (msWrite)
    {
    cloudBlockBlob.UploadFromStream(msWrite);
    }
    }

    If you are still facing the error , please post the complete piece of code after removing the secrets so that we can debug further !


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.