locked
setting and getting BLOB metadata RRS feed

  • Question

  • This seems straightforward, but it isn't working. I'm adding metadata to blobs, but then when I later try to get it from the blob, there's nothing there.

    Setting the metadata...

     

    var blob = blobContainer.GetBlobReference(Guid.NewGuid().ToString()

    );

    blob.UploadFromStream(fs);

    blob.Metadata[

    "FileName"

    ] = fileName;

    blob.Metadata[

    "Submitter"] = name

    ;

    blob.Metadata[

    "DateTime"] = DateTime

    .Now.ToString();

    blob.SetMetadata();

     

    And then to get the metadata...

     

     

     

    var

    blobs = blobContainer.ListBlobs();

     

     

    var filesList = new List<FileEntry

    >();

     

     

    foreach (var blobItem in

    blobs)

    {

     

     

    var cloudBlob = blobContainer.GetBlobReference(blobItem.Uri.ToString());

    cloudBlob.FetchAttributes();

    filesList.Add(

     

    new FileEntry

    ()

    {

    FileUri = blobItem.Uri,

    FileName = cloudBlob.Metadata[

     

    "FileName"

    ],

    Submitter = cloudBlob.Metadata[

     

    "Submitter"

    ]

    });

    ...

    

    Why doesn't FetchAttributes find the metadata?

    Monday, June 20, 2011 2:29 AM

Answers

  • Hi NotTooCasual,

    The code is correct but I think there is an issue on your Azure SDK installation. Could you please use Azure Storage Explorer to check whether the blob is uploaded or not and the metadata is set or not? Also please try using an actual storage account to see if it works or not.

    If it is concluded to be a storage emulator issue, you may want to reinstall the Azure SDK from http://www.microsoft.com/windowsazure/sdk/. If it still does not work, I suggest you contact our azure support via http://www.microsoft.com/windowsazure/support/. Then our support engineer will work with you and look into your machine. This service is free.

    Thanks,


    Wengchao Zeng
    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
    • Marked as answer by Wenchao Zeng Monday, June 27, 2011 5:10 AM
    Tuesday, June 21, 2011 9:38 AM

All replies

  • Hi NotTooCasual,

    The code looks fine and works on my side. Here is the code I tested:

    class BlobMetadataTest3
    {
      public void Run()
      {
        FileStream fs = File.OpenRead("D:\\myconfig.txt");
        CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
        CloudBlobClient blobStorage = storageAccount.CreateCloudBlobClient();
        var blobContainer = blobStorage.GetContainerReference("test");
    
        var blob = blobContainer.GetBlobReference(Guid.NewGuid().ToString());
        blob.UploadFromStream(fs);
        blob.Metadata["FileName"] = "My File";
        blob.Metadata["Submitter"] = "Wenchao Zeng";
        blob.Metadata["DateTime"] = DateTime.Now.ToString();
        blob.SetMetadata();
    
        var blobs = blobContainer.ListBlobs(); 
        var filesList = new List<FileEntry>();
        foreach (var blobItem in blobs)
        {
          var cloudBlob = blobContainer.GetBlobReference(blobItem.Uri.ToString());
          cloudBlob.FetchAttributes();
          filesList.Add(new FileEntry()
          {
            FileUri = blobItem.Uri,
            FileName = cloudBlob.Metadata["FileName"],
            Submitter = cloudBlob.Metadata["Submitter"]
          });
        }
    
        foreach (FileEntry file in filesList)
        {
          Console.WriteLine("FileName: {0}, Submitter: {1}, FileUri: {2}", file.FileName, file.Submitter, file.FileUri);
        }
      }
    
      class FileEntry
      {
        public Uri FileUri { get; set; }
        public string FileName { get; set; }
        public string Submitter { get; set; }
      }
    }
    
    

    Could you please add a breakpoint to check if it really returns values or not? How do you output the data?

    Thanks,


    Wengchao Zeng
    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
    Monday, June 20, 2011 9:40 AM
  • When I run your code, I get the same error I've been seeing. cloudBlob.FetchAttributes() throws "the specified blob does not exist" which I've found is thrown when the metadata is empty

    What would prevent the metadata being written?

    • Edited by NotTooCasual Tuesday, June 21, 2011 12:12 AM clarification
    Monday, June 20, 2011 11:57 PM
  • I used the code posted by Wenchao Zeng and once I replaced:

    var cloudBlob = blobContainer.GetBlobReference(blobItem.Uri.ToString());

    with:

    var cloudBlob = blobContainer.GetBlockBlobReference(blobItem.Uri.ToString());

    everything worked fine.

     

    Tuesday, June 21, 2011 1:18 AM
    Answerer
  • Hi NotTooCasual,

    The code is correct but I think there is an issue on your Azure SDK installation. Could you please use Azure Storage Explorer to check whether the blob is uploaded or not and the metadata is set or not? Also please try using an actual storage account to see if it works or not.

    If it is concluded to be a storage emulator issue, you may want to reinstall the Azure SDK from http://www.microsoft.com/windowsazure/sdk/. If it still does not work, I suggest you contact our azure support via http://www.microsoft.com/windowsazure/support/. Then our support engineer will work with you and look into your machine. This service is free.

    Thanks,


    Wengchao Zeng
    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
    • Marked as answer by Wenchao Zeng Monday, June 27, 2011 5:10 AM
    Tuesday, June 21, 2011 9:38 AM
  • Azure Storage Viewer shows that the metadata is being added properly. However, after re-installing the SDK, the code is still failing to read the metadata.

    I'm going to try on a different system and with an actual storage account.

    Wednesday, June 22, 2011 12:37 AM
  • Hi NotTooCasual,

    > I'm going to try on a different system and with an actual storage account.

    If it does not work after using an actual storage account, could you please use Fiddler (with Decrypting HTTPS-protected traffic) to capture the raw http requests and responses then share them with us?

    Thanks,


    Wengchao Zeng
    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
    Thursday, June 23, 2011 5:04 AM
  • I got it working on another machine and when I came back to the original environment it was working. I don't know why.

    It may be that re-installing the SDK and a reboot resolved it. The easiest way to repro this behaviour is to use an invalid container name, but I eliminated that at the start.

    Thanks everyone for your help.

    Saturday, June 25, 2011 1:16 AM