【问题标题】:How to validate if Blob is exists or not in deleted list如何验证 Blob 是否存在于已删除列表中
【发布时间】:2019-03-07 21:49:39
【问题描述】:

以下代码将能够查看 blob 是否存在。

var blob = client.GetContainerReference(containerName).GetBlockBlobReference(blobFileName);

if (blob.Exists())

如何验证 blob 是否存在于已删除列表中?

【问题讨论】:

    标签: azure azure-storage azure-blob-storage


    【解决方案1】:

    您可以使用blob.Exists() 来验证 blob 是否存在,然后使用 container.ListBlobs(useFlatBlobListing: true, blobListingDetails: BlobListingDetails.Deleted) 列出所有 blob,包括已删除 blob(容器中的所有软删除和活动 blob),验证 blob 是否存在于集合中.

    【讨论】:

      【解决方案2】:

      好问题!因此,如果一个 blob 被删除,并且如果您通过调用 Exists() 方法检查它的存在,它总是会告诉您该 blob 不存在。如果您尝试获取属性,您将收到 404 (NotFound) 错误。

      但是,您仍然可以查明 blob 是否处于已删除状态,但为此您需要列出容器中的 blob。由于一个 Blob 容器可能包含数千个 Blob,为了减少对存储服务的多次调用,您应该列出以 Blob 名称开头的 Blob 名称。

      示例代码如下:

          static void CheckForDeletedBlob()
          {
              var containerName = "container-name";
              var blobName = "blob-name";
              var storageCredetials = new StorageCredentials(accountName, accountKey);
              var storageAccount = new CloudStorageAccount(storageCredetials, true);
              var blobClient = storageAccount.CreateCloudBlobClient();
              var container = blobClient.GetContainerReference(containerName);
              var blob = container.GetBlockBlobReference(blobName);
              var exists = blob.Exists();
              if (!exists)
              {
                  var blobs = container.ListBlobs(prefix: blob.Name, useFlatBlobListing: true, blobListingDetails: BlobListingDetails.Deleted).ToList();
                  if (blobs.FirstOrDefault(b => b.Uri.AbsoluteUri == blob.Uri.AbsoluteUri) == null)
                  {
                      Console.WriteLine("Blob does not exist!");
                  }
                  else
                  {
                      Console.WriteLine("Blob exists but is in deleted state.");
                  }
              }
              else
              {
                  Console.WriteLine("Blob does not exist!");
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-29
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 2019-01-01
        • 2021-10-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多