Search code examples
c#.netazureblobblock

Can I get a specific Block from an Azure Block Blob?


I want to get a specific block from an azure block blob with the blockId, is this even possible? something like

var blockBlob = new BlockBlobClient(connectionString, containerName, blobName);
var blocklist = await GetBlobBlockList(blobName, cancellationToken);
var firstBlock = blocklist.First();
var memStream = new MemoryStream();
await blockBlob.DownloadStreamingAsync(memStream, firstBlock.Name);

Solution

  • I want to get a specific block from an azure block blob with the blockId, is this even possible?

    It should be possible to do so however it won't be as simple as you mentioned in your sample code.

    Here's what you would need to do:

    1. Fetch list of blocks. Each element in the list will have a block id and the size of the block.
    2. Assuming you want to get data for block "n", what you will do is iterate over the list from 0 to n - 1 block and add the size of each block.
    3. Next you would need to call DownloadRangeToStreamAsync(Stream, Nullable<Int64>, Nullable<Int64>), where your offset value will be the sum of the size of each block calculated in step 2 and length value will be the size of the block you wish to download.