m00nbeam360
m00nbeam360

Reputation: 1377

Get CloudBlockBlob metadata key not present in dictionary

I'm trying to create a list of VideoBlob objects (title, description, path) from metadata of a public blob in my storage account. The problem is that when I try to have a variable equal the blob's metadata ("title" is in the blob's metadata), I get

An unhandled exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll

Additional information: The given key was not present in the dictionary.

I've also tried adding in

blob.FetchAttributes();

but that gives me a 404 error. Any suggestions on how I can get the metadata?

Here's what the code looks like so far:

 static void iterateThroughContainer(CloudBlobContainer container)
        {
            List<VideoBlob> blobs = new List<VideoBlob>();

            VideoBlob video;

            CloudBlockBlob blob;

            String tagsString;

            foreach (IListBlobItem item in container.ListBlobs(null, true))
            {
                if (item.GetType() == typeof(CloudBlockBlob))
                {
                    video = new VideoBlob();
                    blob = container.GetBlockBlobReference(item.Uri.ToString());
                    video.uri = "test";
                    Console.WriteLine(blob.Metadata["title"]);
                    video.title = blob.Metadata["title"];
                    video.description = blob.Metadata["description"];
                    video.path = blob.Metadata["path"];
                    blobs.Add(video);
                }
            }
        }

Upvotes: 1

Views: 3281

Answers (2)

m00nbeam360
m00nbeam360

Reputation: 1377

Looks like I was passing in the wrong argument! I added a new method and fixed the GetBlockBlobReference parameter:

  foreach (IListBlobItem item in container.ListBlobs(null, true, BlobListingDetails.Metadata))
        {
            if (item.GetType() == typeof(CloudBlockBlob))
            {
                video = new VideoBlob();
                blob = container.GetBlockBlobReference(getBlobName(item.Uri.ToString()));
                video.uri = item.Uri.ToString();
                video.title = blob.Metadata["title"];
                video.description = blob.Metadata["description"];
             }
        }

 private String getBlobName(String link)
        {
             //convert string to URI
             Uri uri = new Uri(link);
             //parse URI to get just the file name
             return System.IO.Path.GetFileName(uri.LocalPath);  
        }

Upvotes: 0

Serdar Ozler
Serdar Ozler

Reputation: 3802

You do not need to call FetchAttributes, but you should pass BlobListingDetails.Metadata in to ListBlobs to specify metadata should be included when listing.

And you can simply cast item to a CloudBlockBlob object instead of calling GetBlockBlobReference.

Upvotes: 4

Related Questions