c# - 无论如何要确定是否存在具有未提交 block 的 Azure BlockBlob?

标签 c# azure azure-blob-storage

我无法确定 BlockBlob 是否根本不存在或包含未提交的 block 。

请检查以下代码:

private CloudBlobContainer _blobContainer;

private async Task BlobExistTest()
{
    // Get a reference to a brand new (non-existing) blob:
    CloudBlockBlob blob = _blobContainer.GetBlockBlobReference("Test.txt");

    // Query Azure about blob
    bool blobExists = await blob.ExistsAsync();
    try{ var blobBlockCount =  (await blob.DownloadBlockListAsync(BlockListingFilter.All, null, null, null)).Count();        }catch(Exception e){;}
    // Results: Says blob doesn't exist at all on Azure (expected)
    //   - blobExists: false
    //   - Exception on blob.DownloadBlockListAsync(): The remote server 

    // Add a block:
    MemoryStream blockData = new MemoryStream(Encoding.UTF8.GetBytes("Sample data for block"));
    string blockId = Convert.ToBase64String(BitConverter.GetBytes(0));
    await blob.PutBlockAsync(blockId, blockData, null);

    // Query Azure - Now what does Azure say?
    blobExists = await blob.ExistsAsync();
    blobBlockCount =  (await blob.DownloadBlockListAsync(BlockListingFilter.All, null, null, null)).Count();

    // Results:
    //   - blobExists : false
    //   - blobBlockCount : 1
}

CloudBlockBlob.Exists() 即使有数据存储为未提交的 block ,也会返回 false。

要确定“不存在”的 Blob 确实存在并且具有未提交的 block ,可以使用 DownloadBlockList()

我不喜欢这个解决方案,因为它效率低下并且依赖于异常。

我认为问题在于,由未提交的 block 组成的 Blob 处于一种僵尸状态,对于 Azure 存储 API 来说既存在又不存在!

理想情况下,我想知道是否有一个 Blob :

  • 根本不存在
  • 有未提交的 block
  • 存在且区 block 已提交

知道如何实现这一目标吗?

最佳答案

您特别想确定是否存在具有未提交 block 的 Azure BlockBlob 的任何原因?

After Put Block List is called, all uncommitted blocks specified in the block list are committed as part of the new blob. Any uncommitted blocks that were not specified in the block list for the blob will be garbage collected and removed from the Blob service. Any uncommitted blocks will also be garbage collected if there are no successful calls to Put Block or Put Block List on the same blob within a week following the last successful Put Block operation. If Put Blob is called on the blob, any uncommitted blocks will be garbage collected.

https://learn.microsoft.com/en-us/rest/api/storageservices/put-block

关于c# - 无论如何要确定是否存在具有未提交 block 的 Azure BlockBlob?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47368548/

相关文章:

c# - 对象列表,获取带有分隔符的属性

c# - 检测事件是否是从另一个类设置的

android - 如何使用mobileservice从sql azure获取时间戳(行版本)到android

azure - 我可以将 Azure Web 应用程序配置为不提示用户同意吗?

azure-storage - 访问 Windows Azure Blob 存储

c# - 如何验证 COM 对象可以在另一个用户下实例化?

c# - 使用 HtmlHelper 创建 jQuery UI 对话框

javascript - 每个连接请求 Node JS 和 SQL?

c# - Azure Blob 存储列表容器和 Blob

python - 如何从Azure Function Python动态读取blob文件