Azure blob storage V12 - Example of using specialized class BlockBlobStorage

Viewed 3065

I have hard time with new version of Azure Storage Blobs client library for .NET.

What I need is create stream where I can write data and let's say, after stream reach size of 4MB, then I need to upload it. I found BlockBlobClient. There are two methods CommitBlockListAsync and StageBlockAsync. This methods looks like what I need, but I can't find some examples of usage.

Do you know about some scenario similar to my needs? Or can you someone help me understand this client?

Something like this I need, Every 4MB stage, clear stream and continue to write:

public class MyStreamWrapper : Stream
{
    readonly BlockBlobClient _blockBlobClient;
    readonly Stream _wrappedStream;
    bool _isCommited;
    readonly List<string> _blockIds;

    public MyStreamWrapper (BlockBlobClient blockBlobClient)
    {
        _wrappedStream = new MemoryStream();
        _blockBlobClient = blockBlobClient;
        _isCommited = false;
        _blockIds = new List<string>();
    }

    public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
    {
        if ((_wrappedStream.Length + buffer.Length) / 1024 > 4) // check size if 
        {
           int byteCount = (int)(_wrappedStream.Length - buffer.Length);
           if (byteCount > 0)
           {
              _wrappedStream.Write(buffer, offset, byteCount);
              offset += byteCount;
           }

           string base64Id = Convert.ToBase64String(buffer);
           _blockIds.Add(base64Id);
           _blockBlobClient.StageBlock(base64Id, _wrappedStream);
           _wrappedStream.Flush();
        }

        await _wrappedStream.WriteAsync(buffer, offset, count, cancellationToken);
    }

}
1 Answers

For the APIs that don't appear in the samples folder, look at the tests.

eg

[Test]
public async Task CommitBlockListAsync()
{
    await using DisposingContainer test = await GetTestContainerAsync();

    // Arrange
    BlockBlobClient blob = InstrumentClient(test.Container.GetBlockBlobClient(GetNewBlobName()));
    var data = GetRandomBuffer(Size);
    var firstBlockName = GetNewBlockName();
    var secondBlockName = GetNewBlockName();
    var thirdBlockName = GetNewBlockName();

    // Act
    // Stage blocks
    using (var stream = new MemoryStream(data))
    {
        await blob.StageBlockAsync(ToBase64(firstBlockName), stream);
    }
    using (var stream = new MemoryStream(data))
    {
        await blob.StageBlockAsync(ToBase64(secondBlockName), stream);
    }

    // Commit first two Blocks
    var commitList = new string[]
    {
            ToBase64(firstBlockName),
            ToBase64(secondBlockName)
    };

    await blob.CommitBlockListAsync(commitList);

    // Stage 3rd Block
    using (var stream = new MemoryStream(data))
    {
        await blob.StageBlockAsync(ToBase64(thirdBlockName), stream);
    }

    // Assert
    Response<BlockList> blobList = await blob.GetBlockListAsync(BlockListTypes.All);
    Assert.AreEqual(2, blobList.Value.CommittedBlocks.Count());
    Assert.AreEqual(ToBase64(firstBlockName), blobList.Value.CommittedBlocks.First().Name);
    Assert.AreEqual(ToBase64(secondBlockName), blobList.Value.CommittedBlocks.ElementAt(1).Name);
    Assert.AreEqual(1, blobList.Value.UncommittedBlocks.Count());
    Assert.AreEqual(ToBase64(thirdBlockName), blobList.Value.UncommittedBlocks.First().Name);
}

https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/storage/Azure.Storage.Blobs/tests/BlockBlobClientTests.cs

Also familiarize yourself with the REST API, for which the client libraries are wrappers. You are using a "lower level" API approach, that maps directly to Put Block and Put Block List REST APIs.

Related