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.
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 asynchronous
client 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.
BlobContainerAsyncClient
is a better choice.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: