【问题标题】:Azure Blob Storage - How to determine if a specified container contains any blobs?Azure Blob 存储 - 如何确定指定容器是否包含任何 Blob?
【发布时间】:2016-11-24 10:54:03
【问题描述】:

我正在尝试使用 Azure 存储 SDK 并尝试确定是否有一种方法可以指定容器并查找其中包含的 blob 数量。到目前为止我看到的帖子只提到通过 blob 的名称进行检查,这不符合我的需要。

如果我执行以下操作:

CloudBlobContainer blobContainer = blobClient.GetContainerReference("my-container");
var blobCount = blobContainer.ListBlobs().Count();

然后我遇到了 HTTP 404 异常。

有没有办法解决这个问题?

【问题讨论】:

  • 我想知道为什么你得到 HTTP 404 异常。代码应该运行良好。你能把 StorageException.ToString() 的值贴在这里吗?

标签: c# asp.net-mvc http-status-code-404 azure-storage azure-blob-storage


【解决方案1】:

您可以使用以下代码检查计数:

CloudBlobContainer blobContainer = blobClient.GetContainerReference("my-container");
blobContainer.FetchAttributes();
string count = blobContainer.Metadata["ItemCount"];
int ItemCount;
if(int.Tryparse(count ,out ItemCount))
{
   if(ItemCount>0)
    // Container is not Empty
   else
    // Container is Empty  
}
else
{
  // Conversion failed;
}

【讨论】:

  • 不幸的是,blobContainer.FetchAttributes() 行没有返回任何内容。元数据字典返回空。
  • 指定的容器是空的吗?
猜你喜欢
  • 2019-12-25
  • 2015-12-07
  • 1970-01-01
  • 1970-01-01
  • 2020-04-09
  • 1970-01-01
  • 2017-05-20
  • 2016-01-27
  • 1970-01-01
相关资源
最近更新 更多