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