java - 如何获取 Azure blob 下载中的 blob 下载进度

我想从 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>
  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));
    }
}

https://stackoverflow.com/questions/62558951/

相关文章:

python - 实现函数的优化和非优化版本的最简洁方法?

c# - SqlBulkTools - 更新

postgresql - 在typeorm中过滤多对多关系

c# - 如何以编程方式授予虚拟用户对文件夹(或文件)的权限

python - CondaEnvException : Pip failed. 尝试使用 .yml

angular-cli - 每当我运行 npm run ng build-hin 时,我都会遇到错误

javascript - 为什么外部 javascript 文件不工作 Rails 6?

javascript - 为什么 useState 不在单元测试(Jest、Enzyme)中重新渲染

node.js - 如何在 Strapi 中创建不同的管理员角色

flutter - 无法在 flutter 中使用wifi包连接到wifi