簡體   English   中英

Azure blob 存儲 V12 - 使用專用類 BlockBlobStorage 的示例

[英]Azure blob storage V12 - Example of using specialized class BlockBlobStorage

我很難使用適用於 .NET 的新版Azure Storage Blob 客戶端庫

我需要的是創建流,我可以在其中寫入數據,假設流達到 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);
    }

}

對於未出現在示例文件夾中的 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

還要熟悉 REST API,客戶端庫是其包裝器。 您正在使用“較低級別”的 API 方法,該方法直接映射到Put BlockPut Block List REST API。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM