[英]why I can not upload a large file to azure storage blob though java?
这是我将文件上传到 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就被阻塞了,一直没有完成。 这是日志
我应该怎么办?
如果您有任何建议,非常感谢。
如果您想从本地路径上传,请尝试以下代码:
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));
}
如果您想从 stream 上传,请使用以下命令:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.