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

Get CloudBlockBlob metadata key not present in dictionary

我正在尝试根据存储帐户中 public blob 的元数据创建 VideoBlob objects(标题、描述、路径)列表。问题是,当我尝试让一个变量等于 blob 的元数据("title" 在 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);
                }
            }
        }

看来我传入了错误的参数!我添加了一个新方法并修复了 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);  
        }

您不需要调用 FetchAttributes,但您应该将 BlobListingDetails.Metadata 传递给 ListBlobs 以指定在列出时应包含元数据。

并且您可以简单地将 item 转换为 CloudBlockBlob 对象,而不是调用 GetBlockBlobReference。