skip to Main Content

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);

2

Answers


  1. 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.
    Login or Signup to reply.
  2. You need to create a block blob below you can find the procedure:

    enter image description here

    you need to create a container under storage account as follows:

    enter image description here

    inside the container you can find the block blob under properties as shown below

    enter image description here

    To Get the block Id you can follow the code below:

    enter image description here

    code:

    using System;  
    using System.Threading.Tasks;  
    using Azure.Storage.Blobs;  
    using Azure.Storage.Blobs.Models;
    
    class Program  
    {  
    static async Task Main(string[] args)  
    {  
    string connectionString = "DefaultEndpointsProtocol=https;AccountName=<account-name>;AccountKey=<account-key>;EndpointSuffix=core.windows.net";  
    string containerName = "***";  
    string blobName = "***.txt";  
    BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);  
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);  
    BlobClient blobClient = containerClient.GetBlobClient(blobName);
    
      
    BlockList blockList = await blobClient.GetBlockListAsync(BlockListType.All)  
    string firstBlockId = blockList.Value.First().Name;  
    Console.WriteLine("First block ID: " + firstBlockId);  
    }  
    }
    

    After getting the Block Id now you can get the certain block using the code below:

    enter image description here

    code:

    using System;  
    using System.IO;  
    using System.Threading;  
    using System.Threading.Tasks;  
    using Azure.Storage.Blobs;  
    using Azure.Storage.Blobs.Models;
    
    class Program  
    {  
    static async Task Main(string[] args)  
    {  
    string connectionString = "DefaultEndpointsProtocol=https;AccountName=<account-name>;AccountKey=<account-key>;EndpointSuffix=core.windows.net";  
    string containerName = "mycontainer---";  
    string blobName = "myblob**";  
    string blockId = "1234567**";  
    BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);  
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);  
    BlobClient blobClient = containerClient.GetBlobClient(blobName);  
    MemoryStream blockData = new MemoryStream();  
    await blobClient.DownloadStreamingAsync(blockId, blockData, cancellationToken: CancellationToken.None);  
    Console.WriteLine("Block data:");  
    Console.WriteLine(Convert.ToBase64String(blockData.ToArray()));  
    }  
    }
    

    In the above code you need to replace connection string, container name, block Id, blob name as below:

    enter image description here

    By following the above procedure, I got successfully.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search