【问题标题】:How to get blob Download Progress in Azure blob download如何在 Azure blob 下载中获取 blob 下载进度
【发布时间】: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);

【问题讨论】:

    标签: java azure azure-storage azure-java-sdk


    【解决方案1】:

    如果要获取下载进度,请参考以下代码

    1. SDK
     <dependency>
          <groupId>com.azure</groupId>
          <artifactId>azure-storage-blob</artifactId>
          <version>12.7.0</version>
      </dependency>
    
    1. 代码
    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));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-16
      • 1970-01-01
      • 2021-02-17
      • 2012-12-08
      • 2014-05-10
      • 2021-07-26
      • 2019-12-19
      相关资源
      最近更新 更多