【问题标题】:Get CloudBlockBlob metadata key not present in dictionary获取字典中不存在的 CloudBlockBlob 元数据键
【发布时间】:2017-06-20 16:55:38
【问题描述】:

我正在尝试根据我的存储帐户中公共 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);
                }
            }
        }

【问题讨论】:

  • 您必须调用 FetchAttributes 才能填充元数据。如果 FetchAttributes 返回 404,则表示您正在尝试访问不存在的 blob。您的问题中没有 FetchAttributes 代码,因此很难看出您可能做错了什么,但这就是您应该开始的地方。 Fiddler 是确保您尝试获取您认为正在访问的 blob 属性的好方法。
  • 我刚刚也发现 blob 不存在!您知道获取 blob 名称的任何好方法(在 GetBlockBlobReference 中)吗?
  • 没关系,解析 URI。谢谢,kwill!

标签: c# azure azure-storage


【解决方案1】:

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

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

【讨论】:

  • 这太完美了,塞尔达!进行了更改,并且效果也很好。谢谢!
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 2014-10-29
    相关资源
    最近更新 更多