获取字典中不存在的CloudBlockBlob元数据键

时间:2015-01-21 22:17:01

标签: c# azure azure-storage

我尝试从我的存储帐户中的公共blob的元数据创建VideoBlob对象列表(标题,说明,路径)。问题是,当我尝试使变量等于blob的元数据时("标题"在blob的元数据中),我得到了

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

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

我也试过加入

blob.FetchAttributes();

但是这给了我404错误。关于如何获取元数据的任何建议?

以下是目前代码的样子:

 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);
                }
            }
        }

2 个答案:

答案 0 :(得分:2)

您不需要调用FetchAttributes,但是您应该传递BlobListingDetails。列表中应该包含元数据以指定元数据。

您只需将转换为CloudBlockBlob对象,而不是调用GetBlockBlobReference。

答案 1 :(得分:0)

看起来我传递了错误的论点!我添加了一个新方法并修复了GetBlockBlobReference参数:

  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);  
        }
相关问题