0

I am just starting on code to download files from Azure blob using java SDK. while searching found two different clients for Blob. BlobContainerClient and BlobContainerAsyncClient. what's the difference between them? In my use case, I have to download different blobs parallely.

1 Answer 1

1

Azure BlobContainerClient vs BlobContainerAsyncClient

The BlobContainerClient and BlobContainerAsyncClient are part of the Azure Storage Blob SDK for Java to interact with Blob Containers.

BlobContainerClient:

It is a synchronous client that provides blocking methods to interact with Blob Containers. It is used for applications that need synchronous or blocking Input and output operations. When you need simple, straightforward interaction with the azure blob storage.

BlobContainerAsyncClient:

It is a asynchronousclient that provides blocking methods to interact with Blob Containers. It is used for applications that need asynchronous or non-blocking Input and Output operations. Perfect for high-performance applications that need to perform multiple Input and Output operations at the same time without blocking threads.

In my use case, I have to download different blobs parallely.

  • To download multiple blobs in parallel, the BlobContainerAsyncClient is a better choice.
  • It allows you to start multiple download operations at the same time and handle them efficiently.

Here is the sample code for downloading blobs asynchronously with BlobContainerAsyncClient using Azure Java SDK.

Code:

String connectionString = "DefaultEndpointsProtocol=https;AccountName=venkat326123;AccountKey=zzzzzzzz;EndpointSuffix=core.windows.net";
String containerName = "data";
String downloadDirectory = "C:\\folder1";

BlobContainerAsyncClient containerClient = new BlobServiceClientBuilder()
                .connectionString(connectionString)
                .buildAsyncClient()
                .getBlobContainerAsyncClient(containerName);

Flux<BlobItem> blobs = containerClient.listBlobs();

blobs.flatMap(blobItem -> {
         String blobName = blobItem.getName();
         Path downloadTo = Paths.get(downloadDirectory, blobName);
         try {
              Files.createDirectories(downloadTo.getParent());
             } catch (Exception e) {
                return Mono.error(e);
           }

         return containerClient.getBlobAsyncClient(blobName)
                    .downloadToFile(downloadTo.toString())
                    .then(Mono.just("Downloaded " + blobName + " to " + downloadTo));
        })
          .doOnNext(successMessage -> System.out.println(successMessage))
          .doOnError(error -> System.err.println("Error: " + error))
          .doOnComplete(() -> System.out.println("Download completed"))
          .blockLast(); 

Output:

Downloaded industry.csv to C:\folder1\industry.csv
Downloaded sample2.ps1 to C:\folder1\sample2.ps1
Downloaded industry.csv.gpg to C:\folder1\industry.csv.gpg
Download completed

Reference:

Sign up to request clarification or add additional context in comments.

2 Comments

To download the files from Blob we have to create a BlobClient for each file. So, will it work if I make a BlobClient from BlobContainerClient for each file and use multi-threading in Java for all BlobClients
Yes, you can use multithreading in Java to download multiple blobs concurrently with BlobContainerClient by creating a BlobClient for each blob and running the download operations in parallel. However, using BlobContainerAsyncClient with asynchronous operations is typically more efficient and scales better for high-concurrency scenarios.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.