Java 如何在Azure blob下载中获得blob下载进度
我想从Azure blob存储下载一个大尺寸blob。假设我的blob大小为2GB。我想使用Java 如何在Azure blob下载中获得blob下载进度,java,azure,azure-storage,azure-java-sdk,Java,Azure,Azure Storage,Azure Java Sdk,我想从Azure blob存储下载一个大尺寸blob。假设我的blob大小为2GB。我想使用javasdk获得下载进度的百分比,以便显示一些漂亮的进度条。我使用以下代码下载blob BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString("connectionString").buildClient(); BlobContaine
javasdk
获得下载进度的百分比,以便显示一些漂亮的进度条。我使用以下代码下载blob
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString("connectionString").buildClient();
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("cloudDirectoryName");
BlobClient blobClient = blobContainerClient.getBlobClient(fileName);
BlobRange range = new BlobRange(1024, 2048L);
DownloadRetryOptions options = new DownloadRetryOptions().setMaxRetryRequests(5);
blobClient.downloadToFileWithResponse(filePath, null, new ParallelTransferOptions(4 * Constants.MB, null, null),
options, null, false, null, null);
如果您想获得下载进度,请参考以下代码
com.azure
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
<version>12.7.0</version>
</dependency>
public class App
{
public static void main( String[] args )
{
String accountName="blobstorage0516";
String accountKey ="";
StorageSharedKeyCredential creds = new StorageSharedKeyCredential(accountName,accountKey);
String endpoint = String.format(Locale.ROOT, "https://%s.blob.core.windows.net", accountName);
BlobServiceClient blobServiceClient =new BlobServiceClientBuilder()
.endpoint(endpoint)
.credential(creds)
.buildClient();
BlobContainerClient blobContainerClient =blobServiceClient.getBlobContainerClient("image");
BlobClient blobClient= blobContainerClient.getBlobClient("test.jpg");
String filePath="D:\\test\\test.jpg";
ProgressReceiver reporter = new FileDownloadReporter();
ParallelTransferOptions options= new ParallelTransferOptions(4 * Constants.MB,null,reporter);
Response<BlobProperties> repose= blobClient.downloadToFileWithResponse(filePath,new BlobRange(0),options,null,null,false, null,Context.NONE);
System.out.println(repose.getStatusCode());
}
}
class FileDownloadReporter implements ProgressReceiver {
@Override
public void reportProgress(long bytesTransferred) {
System.out.println(String.format("You have download %d bytes", bytesTransferred));
}
}