我是否可以将流上传到Azure blob存储而不预先指定其长度?

时间:2014-07-07 19:43:50

标签: java azure azure-storage

我不知道它是否相关,但我使用Java和azure-storage-android-0.2.0.aar进行上传。

我可以将文件上传到Microsoft Azure blob存储

CloudBlockBlob blob = container.getBlockBlobReference("filename.ext");
blob.upload(inputStream, n);

其中n是inputStream从文件派生时的长度。

这是我的问题:我想直接流式传输,例如从相机流式传输,这显然是不可能的,因为Azure需要上传的长度参数,这在流式传输时是未知的。

我需要指定长度吗? (MD5?)有没有一种方法可以在流生成时上传(这显然是Java中的InputStream的想法,这是InputStream没有长度属性的原因)?

2 个答案:

答案 0 :(得分:11)

我们将记录功能请求以启用从流上传而不指定长度。现在,您可能希望使用openOutputStream方法,该方法具有使用byte []或int的write方法。使用int方法的示例如下:

    CloudBlockBlob blockBlob = container.getBlockBlobReference(‘myblob’); // assuming container was already created

    BlobOutputStream blobOutputStream = blockBlob.openOutputStream();
    ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer); // assuming buffer is a byte[] with your data

    int next = inputStream.read();
    while (next != -1) {
          blobOutputStream.write(next);
          next = inputStream.read();
    }

    blobOutputStream.close();

答案 1 :(得分:0)

我使用 BlobAsyncClient 做到了,下面是代码:

BlobServiceAsyncClient blobServiceAsyncClient = blobServiceClientBuilder.buildAsyncClient();

BlobContainerAsyncClient blobContainerAsyncClient =
    blobServiceAsyncClient.getBlobContainerAsyncClient("container name");

BlobAsyncClient blobAsyncClient =
    blobContainerAsyncClient.getBlobAsyncClient("blob name");

ParallelTransferOptions parallelTransferOptions = new ParallelTransferOptions()
    .setBlockSizeLong(blockSize).setMaxConcurrency(5)
    .setProgressReceiver(bytesTransferred -> log.info("Uploaded bytes:{}", bytesTransferred));

Flux<ByteBuffer> data = Flux.just(ByteBuffer.wrap(byteArray));

blobAsyncClient.upload(data, parallelTransferOptions, true)
    .doOnError(throwable -> log.error("Error occurred while uploading:{}",
        throwable.getMessage()))
    .doOnSuccess(success -> {
      log.info("Blob is uploaded successfully!!:{}", blobAsyncClient.getBlobUrl());
    })
    .subscribe();