Azure Blob存储V12-使用特殊类BlockBlobStorage的示例

问题描述 投票:0回答:1

我在使用Azure Storage Blobs client library for .NET的新版本时遇到了困难。

我需要创建一个可以写入数据的流,并且说,在流达到4MB的大小之后,我需要上传它。我找到了BlockBlobClient。有两种方法CommitBlockListAsync和StageBlockAsync。这种方法看起来像我需要的,但是我找不到一些用法示例。

您知道与我的需求类似的情况吗?还是可以有人帮助我了解此客户?

我需要的东西,每4MB阶段,清除流并继续写入:

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

}
c# azure-storage-blobs
1个回答
0
投票

对于没有出现在示例文件夹中的API,请查看测试。

例如

[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

© www.soinside.com 2019 - 2024. All rights reserved.