A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://stackoverflow.com/questions/78792328/azure-blobcontainerclient-vs-blobcontainerasyncclient below:

Azure BlobContainerClient vs BlobContainerAsyncClient - Stack Overflow

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.

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:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4