【问题标题】:why I can not upload a large file to azure storage blob though java?为什么我不能通过 java 将大文件上传到 azure storage blob?
【发布时间】:2021-08-11 08:28:44
【问题描述】:

这是我将文件上传到 azure 的代码

BlobAsyncClient blobAsyncClient = AzureUtils.getBlobAsyncClient(containerName, blobName);
long blockSize = 10 * 1024 * 1024;
ParallelTransferOptions parallelTransferOptions = new ParallelTransferOptions().setBlockSizeLong(blockSize)
            .setMaxConcurrency(10)
            .setProgressReceiver(bytesTransferred -> System.out.println("uploaded:" + bytesTransferred));
Flux<ByteBuffer> data = Flux.just(ByteBuffer.wrap(IOUtils.toByteArray(fileStream)));
blobAsyncClient.upload(data, parallelTransferOptions, true).block();

我上传181M大小的文件还是可以的,但是当我尝试上传498M大小的文件时,上传到160M就被阻塞了,一直没有成功。@ 987654321@

我该怎么办?

如果您有任何建议,非常感谢。

【问题讨论】:

  • 您是否收到任何错误消息?请编辑您的问题并将其包括在内。
  • 根据上传文件的日志,什么不起作用。
  • @GauravMantri 不,有线的事情是什么都没有,没有信息,没有警告,没有错误。它只是停在'uploaded:167772160'并且在我停止程序之前永远不会更新状态
  • @Riven 可以添加 fileStream 来自哪里的完整示例?

标签: java azure-blob-storage


【解决方案1】:

如果您想从本地路径上传,请尝试以下代码:

public static void uploadFilesByChunk() {
                String connString = "<conn str>";
                String containerName = "<container name>";
                String blobName = "<your-blob-name>";
                String filePath = "<your-file-path>" + blobName;

                BlobServiceClient client = new BlobServiceClientBuilder().connectionString(connString).buildClient();
                BlobClient blobClient = client.getBlobContainerClient(containerName).getBlobClient(blobName);
                long blockSize = 2 * 1024 * 1024; //2MB
                ParallelTransferOptions parallelTransferOptions = new ParallelTransferOptions()
                                .setBlockSizeLong(blockSize).setMaxConcurrency(2)
                                .setProgressReceiver(new ProgressReceiver() {
                                        @Override
                                        public void reportProgress(long bytesTransferred) {
                                                System.out.println("uploaded:" + bytesTransferred);
                                        }
                                });

                BlobHttpHeaders headers = new BlobHttpHeaders().setContentLanguage("en-US").setContentType("binary");

                blobClient.uploadFromFile(filePath, parallelTransferOptions, headers, null, AccessTier.HOT,
                                new BlobRequestConditions(), Duration.ofMinutes(30));
        }

如果你想从流中上传,使用这个:

BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient("myblockblob").getBlockBlobClient();
String dataSample = "samples";
try (ByteArrayInputStream dataStream = new ByteArrayInputStream(dataSample.getBytes())) {
    blockBlobClient.upload(dataStream, dataSample.length());
} catch (IOException e) {
    e.printStackTrace();
}

参考:

https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/storage/azure-storage-blob#examples

https://stackoverflow.com/a/65403169/13586071

【讨论】:

    猜你喜欢
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 2021-10-08
    • 2017-11-24
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多