gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 07:06:13 25 4
gpt4 key购买 nike

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

    enter image description here

    关于java - 如何获取 Azure blob 下载中的 blob 下载进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62558951/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com