gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 06:37:37 26 4
gpt4 key购买 nike

我无法确定 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com