Mirko Lugano
Mirko Lugano

Reputation: 985

Set Azure Metadata on BlockBlob via REST API

I want to set some custom metadata property on a Blob on Azure using the Azure REST API. The upload of the file works ok (also done via REST API). To set my custom metadata I followed these instructions. A sample URL could be:

(PUT Method) https://myaccount.blob.core.windows.net/ph0wzj1k4dbuwfusyqw38z2jgfk5p56bfnlj5/a5bc77a8d4d34edeb6d3212fc6bbd8a9.jpg?sv=2014-02-14&sr=c&si=BlobContainer&sig=someSignaturecomp=metadata

with this request header:

x-ms-meta-logicalname:20141028_064011.jpg

Whether I run this url via jquery ajax or thru Fiddler I always get the same result: the call seems to be successful (I get a 200 status code) but if then I inspect the blob metadata (using C# code) the key is not found and the metadata is empty.

Instead, if I do everything with C#, I can set and get any metadata on my blob as I wish.

Sample C# code (now working):

var blobs = container.ListBlobs(null, true, BlobListingDetails.Metadata).Cast<CloudBlockBlob>();
foreach (var blob in blobs) {
    Console.WriteLine(blob.Metadata["logicalname"]);
}

As you can see I have entered my shared access signature in the url and not in the request headers as the instructions show, but I doubt that is the problem because on uploading and other operations (also on table storage) everything works fine.

Am I doing something wrong? (obviously yes, but what?... :) )

SOLVED:

Investigating with Azure Storage Explorer I saw that the metadata is correctly set and is there, it was just not retrieved via C#. Thanx to Gaurav's suggestion I have edited the C# code sample I had posted and am now retrieving the metadata that I had set via REST API.

Upvotes: 0

Views: 1380

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136196

The metadata is there but you're not able to see it because you haven't fetched it. What you could do is change the ListBlobs() call. Assuming you're using the latest version of storage client library, you can do something like:

var blobs = container.ListBlobs(null, true, BlobListingDetails.Metadata).Cast<CloudBlockBlob>();

This will populate the metadata along with other properties.

Upvotes: 1

Related Questions