Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Azure blob存储V12-使用专用类BlockBlob存储的示例_C#_Azure Storage Blobs - Fatal编程技术网

C# Azure blob存储V12-使用专用类BlockBlob存储的示例

C# Azure blob存储V12-使用专用类BlockBlob存储的示例,c#,azure-storage-blobs,C#,Azure Storage Blobs,我很难接受新版本的 我需要的是创建流,我可以在其中写入数据,比如说,流达到4MB大小后,我需要上传它。我找到了。有两种方法CommitBlockListAsync和StageBlockAsync。这个方法看起来像我需要的,但是我找不到一些用法的例子 你知道一些类似于我需求的场景吗?或者你能帮我了解一下这个客户吗 我需要这样的东西,每个4MB阶段,清除流并继续写: public class MyStreamWrapper : Stream { readonly BlockBlobClien

我很难接受新版本的

我需要的是创建流,我可以在其中写入数据,比如说,流达到4MB大小后,我需要上传它。我找到了。有两种方法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);
    }

}

对于没有出现在samples文件夹中的api,请查看测试

乙二醇


还要熟悉RESTAPI,客户机库是它的包装器。您使用的是一种较低级别的API方法,它直接映射到和REST 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);
}