Anyway to determine if an Azure BlockBlob with uncommitted blocks exists?

Viewed 1125

I am unable to determine if a BlockBlob does not exist at all or contains uncommitted blocks.

Please examine the following code:

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() returns false even if there is data stored as uncommitted blocks.

To determine that a "non-existing" Blob actually exists and has uncommitted blocks, DownloadBlockList() can be used.

I do not like this solution as it is inefficient and relies on an exception.

I think that the issue is that a Blob made of uncommitted blocks is in a kind of zombie state both existing and not existing in respect of the Azure Storage API!

Ideally I would like to know if a blob:

  • Does not exist at all
  • Has uncommitted blocks
  • Exists & blocks are committed

Any idea how to achieve that?

1 Answers

Any reason you particularly want to determine if an Azure BlockBlob with uncommitted blocks exists?

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://docs.microsoft.com/en-us/rest/api/storageservices/put-block

Related