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?