Download a blob with Java

This article shows how to download a blob using the Azure Storage client library for Java. You can download blob data to various destinations, including a local file path, stream, or text string. You can also open a blob stream and read from it.

Prerequisites

Set up your environment

If you don't have an existing project, this section shows you how to set up a project to work with the Azure Blob Storage client library for Java. For more information, see Get started with Azure Blob Storage and Java.

To work with the code examples in this article, follow these steps to set up your project.

Note

This article uses the Maven build tool to build and run the example code. Other build tools, such as Gradle, also work with the Azure SDK for Java.

Install packages

Open the pom.xml file in your text editor. Install the packages by including the BOM file, or including a direct dependency.

Add import statements

Add the following import statements:

import com.azure.storage.blob.*;
import com.azure.storage.common.*;
import com.azure.storage.blob.options.BlobDownloadToFileOptions;
import com.azure.storage.blob.specialized.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

Authorization

The authorization mechanism must have the necessary permissions to perform a download operation. For authorization with Microsoft Entra ID (recommended), you need Azure RBAC built-in role Storage Blob Data Reader or higher. To learn more, see the authorization guidance for Get Blob (REST API).

Create a client object

To connect an app to Blob Storage, create an instance of BlobServiceClient.

The following example uses BlobServiceClientBuilder to build a BlobServiceClient object using DefaultAzureCredential, and shows how to create container and blob clients, if needed:

// Azure SDK client builders accept the credential as a parameter
// TODO: Replace <storage-account-name> with your actual storage account name
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
        .endpoint("https://<storage-account-name>.blob.core.windows.net/")
        .credential(new DefaultAzureCredentialBuilder().build())
        .buildClient();

// If needed, you can create a BlobContainerClient object from the BlobServiceClient
BlobContainerClient containerClient = blobServiceClient
        .getBlobContainerClient("<container-name>");

// If needed, you can create a BlobClient object from the BlobContainerClient
BlobClient blobClient = containerClient
        .getBlobClient("<blob-name>");

To learn more about creating and managing client objects, see Create and manage client objects that interact with data resources.

Download a blob

You can use any of the following methods to download a blob:

Download to a file path

The following example downloads a blob to a local file path:

public void downloadBlobToFile(BlobClient blobClient) {
    blobClient.downloadToFile("filepath/local-file.png");
}

Download to a stream

The following example downloads a blob to an OutputStream object:

public void downloadBlobToStream(BlobClient blobClient) {
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        blobClient.downloadStream(outputStream);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Download to a string

The following example assumes that the blob is a text file, and downloads the blob to a String object:

public void downloadBlobToText(BlobClient blobClient) {
    String content = blobClient.downloadContent().toString();
    System.out.printf("Blob contents: %s%n", content);
}

Download from a stream

The following example downloads a blob by opening a BlobInputStream and reading from the stream:

public void readBlobFromStream(BlobClient blobClient) {
    // Opening a blob input stream allows you to read from a blob through a normal
    // stream interface

    try (BlobInputStream blobStream = blobClient.openInputStream()) {
        blobStream.read();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Download a block blob with configuration options

You can define client library configuration options when downloading a blob. These options can be tuned to improve performance and enhance reliability. The following code examples show how to use BlobDownloadToFileOptions to define configuration options when calling a download method.

Specify data transfer options on download

You can configure values in ParallelTransferOptions to improve performance for data transfer operations. The following values can be tuned for downloads based on the needs of your app:

  • blockSize: The maximum block size to transfer for each request. You can set this value by using the setBlockSizeLong method.
  • maxConcurrency: The maximum number of parallel requests issued at any given time as a part of a single parallel transfer. You can set this value by using the setMaxConcurrency method.

Add the following import directive to your file to use ParallelTransferOptions for a download:

import com.azure.storage.common.*;

The following code example shows how to set values for ParallelTransferOptions and include the options as part of a BlobDownloadToFileOptions instance. The values provided in this sample aren't intended to be a recommendation. To properly tune these values, you need to consider the specific needs of your app.

public void downloadBlobWithTransferOptions(BlobClient blobClient) {
    ParallelTransferOptions parallelTransferOptions = new ParallelTransferOptions()
            .setBlockSizeLong((long) (4 * 1024 * 1024)) // 4 MiB block size
            .setMaxConcurrency(2);

    BlobDownloadToFileOptions options = new BlobDownloadToFileOptions("<localFilePath>");
    options.setParallelTransferOptions(parallelTransferOptions);

    blobClient.downloadToFileWithResponse(options, null, null);
}

To learn more about tuning data transfer options, see Performance tuning for uploads and downloads with Java.

Resources

To learn more about how to download blobs using the Azure Blob Storage client library for Java, see the following resources.

Code samples

REST API operations

The Azure SDK for Java contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar Java paradigms. The client library methods for downloading blobs use the following REST API operation:

Client library resources

  • This article is part of the Blob Storage developer guide for Java. To learn more, see the full list of developer guide articles at Build your Java app.