如何在 Azure blob 下载中获取 blob 下载进度
Posted
技术标签:
【中文标题】如何在 Azure blob 下载中获取 blob 下载进度【英文标题】:How to get blob Download Progress in Azure blob download 【发布时间】:2020-10-14 22:37:38 【问题描述】:我想从 Azure blob 存储下载一个大尺寸的 blob。假设我的 blob 大小为 2 GB。我想使用java-sdk
以百分比形式获取下载进度,以便显示一些漂亮的进度条。我正在使用以下代码下载 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);
【问题讨论】:
【参考方案1】:如果要获取下载进度,请参考以下代码
-
SDK
<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));
【讨论】:
以上是关于如何在 Azure blob 下载中获取 blob 下载进度的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 C# 中的 Azure.Storage.Blobs 从 Azure 存储 Blob 以 ByteArray 格式获取文件
如何使用javascript从stringstream内容azure blob存储中下载png文件