为什么我无法通过 java 将大文件上传到 azure 存储 blob?

时间:2021-05-22 10:09:31

标签: java azure-storage-blobs

这是我将文件上传到 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时被阻止,并且从未完成。{ {3}}

我该怎么办?

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

1 个答案:

答案 0 :(得分:0)

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

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

相关问题