【问题标题】:Accessing metadata from azure storage blob从 Azure 存储 Blob 访问元数据
【发布时间】:2017-09-16 01:42:13
【问题描述】:

我似乎找不到访问 Azure 存储中 blob 的单个 blob 元数据的方法。

FetchAttributes 仅适用于整个容器。我的方法返回与我设置的参数匹配的 blob 列表。然后我需要遍历该列表并从每个 blob 中检索一些元数据,但我没有找到任何方法可以这样做。

这似乎有很多开销,但我是否应该在创建容器对象时获取这些属性,然后过滤 blob 列表?

所以,我想我会尝试,

 public static IEnumerable<GalleryPhoto> GetGalleryPhotos(string folderPath)
 {

        var container = CreateAzureContainer(containerName, false);
        container.FetchAttributes();
        var blobDirectory = container.GetDirectoryReference(folderPath);
        var photoGalleries = new List<GalleryPhoto>();
        var blobs = blobDirectory.ListBlobs().ToList();

       ...rest of code
  }

blob 中的 blob 对象,元数据计数显示为 0。 每个项目都有元数据,通过查看 Azure 存储资源管理器中每个 blob 的属性进行验证。

任何帮助表示赞赏。

【问题讨论】:

    标签: c# azure metadata


    【解决方案1】:

    在列出 blob 时,完全可以在 result 中获取元数据。您需要做的是在ListBlobs 方法调用中指定BlobListingDetails 参数并在那里指定BlobListingDetails.Metadata。这将在响应中包含每个 blob 的元数据。所以你的代码是:

    public static IEnumerable<GalleryPhoto> GetGalleryPhotos(string folderPath)
     {
    
            var container = CreateAzureContainer(containerName, false);
            container.FetchAttributes();
            var blobDirectory = container.GetDirectoryReference(folderPath);
            var photoGalleries = new List<GalleryPhoto>();
            var blobs = blobDirectory.ListBlobs(false, BlobListingDetails.Metadata).ToList();
    
           ...rest of code
      }
    

    请尝试一下。它应该可以工作。

    【讨论】:

      【解决方案2】:
      var blobs = container.ListBlobs().OfType<CloudBlockBlob>().ToList();
      foreach (var blob in blobs)
      {
          blob.FetchAttributes(); //Now the metadata will be populated
      }
      

      https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.blob.cloudblockblob.fetchattributes%28v=azure.10%29.aspx?f=255&MSPPError=-2147217396

      【讨论】:

        猜你喜欢
        • 2019-01-15
        • 2020-06-03
        • 2021-12-18
        • 2013-10-22
        • 1970-01-01
        • 2019-10-14
        • 2016-05-16
        • 2019-11-13
        • 2021-11-19
        相关资源
        最近更新 更多