3

I want to download a large size blob from Azure blob storage. Let's say my blob size is 2 GB. I want to get the progress of the download in percentage using java-sdk so that I can show some pretty progress bar. I am using following code to download the 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);
CodeTalker
  • 1,683
  • 2
  • 21
  • 31

1 Answers1

0

If you want to get download progress, please refer to the following code

  1. SDK
 <dependency>
      <groupId>com.azure</groupId>
      <artifactId>azure-storage-blob</artifactId>
      <version>12.7.0</version>
  </dependency>
  1. Code
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

Jim Xu
  • 21,610
  • 2
  • 19
  • 39