Java – Download files from Azure Blob Storage No.27

In this blog, How to download files from Azure Blob Storage using Java is shown.

▼1. List file names and download files from Azure Blob Storage

Using IntelliJ IDEA 2021.1.2 (Community Edition) on Ubuntu , Java application lists files on Azure Blob Storage and downloads these files.


▼2. Prerequisites

2-1. Installing Java 8 JDK

Azul Zulu OpenJDK Java 8 (LTS) is supportable on Azure Service. steps of installation are shown in this document. Download Azul Zulu Builds of OpenJDK | Azul

e.g) JAVA_HOME
$echo $JAVA_HOME
/usr/lib/jvm/zulu-8-azure-jdk_8.54.0.21-8.0.292-linux_x64/

2-2. Free trial Azure account for Azure Blob Storage

Azure free account FAQ

2-3. Connecting to Azure Blob Storage using Azure Storage Explorer

Creating Container and putting the file “abc.txt” into Azure Blob Storage


▼3. List files and download these files from Azure Blob Storage

3-1. Starting IntelliJ IDEA and confirming JDK version

Starting IntelliJ IDEA using idea.sh command. after that, we can confirm JDK version from Menu -> [File] -> [New] -> [Project] -> [Maven]

(e.g)
~/Downloads/idea-IC-211.7142.45/bin/idea.sh

3-2. Updating pom.xml for operations of Azure Blob Storage

This dependency is added in pom.xml

    <dependencies>
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-storage-blob</artifactId>
            <version>12.11.1</version>
        </dependency>
    </dependencies>

Sync is executed automatically by clicking the following putton.

update-dependencies-automatically

3-3. Creating Java Code

A Java code is created to download files from Azure Blob Storage. at first, in the project, we do right click [src]-[main]-[java] and choose [New] – [java Class]. after that, name of this code needs to be entered. this code implement the following steps.

  • List files on Azure Blob Storage
  • Download these files from Azure Blob Storage
e.g)

import com.azure.storage.blob.*;
import com.azure.storage.blob.models.BlobItem;

public class Sampleblacc {
    public static void main(String[] args) {
        String connectStr = "<ConnectionString>";
        String containerName = "files";
        String downloadFileName = "abc.txt";
        String localPath = "/home/user/";

        // Create a BlobServiceClient object which will be used to create a container client
        BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectStr).buildClient();

        BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
        System.out.println("\nListing blobs...");

        // List the blob(s) in the container.
        for (BlobItem blobItem : containerClient.listBlobs()) {
            System.out.println("\t" + blobItem.getName());
        }

        //Get a reference to a blob
        BlobClient blobClient = containerClient.getBlobClient(downloadFileName);

        //Download the blob to a local file
        System.out.println("\nDownloading blob to \n\t " + localPath + downloadFileName);

        blobClient.downloadToFile(localPath + downloadFileName);
    }
}

3-4. Results of this code

Build this code from [Build] menu -> [Build Project] and Run code by clicking [Run] in the menue -> [Run]]

e.g) a part of output

Listing blobs...
README.md
abc.txt

Downloading blob to
 /home/user/abc.txt

▼4. Reference

That’s all. Have a nice day ahead !!!

Leave a Reply

Your email address will not be published. Required fields are marked *