Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest Fabric, Power BI, and SQL learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
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.
Open the pom.xml
file in your text editor. Install the packages by including the BOM file, or including a direct dependency.
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;
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).
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.
You can use any of the following methods to download a blob:
The following example downloads a blob to a local file path:
public void downloadBlobToFile(BlobClient blobClient) {
blobClient.downloadToFile("filepath/local-file.png");
}
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();
}
}
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);
}
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();
}
}
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.
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.
To learn more about how to download blobs using the Azure Blob Storage client library for Java, see the following resources.
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:
Events
Mar 31, 11 PM - Apr 2, 11 PM
The biggest Fabric, Power BI, and SQL learning event. March 31 – April 2. Use code FABINSIDER to save $400.
Register todayTraining
Learning path
Expand the capabilities for Java apps on Azure - Training
Start here and learn how you can get the full power of Azure with your Java apps - use idiomatic libraries to connect and interact with your preferred cloud services, including Azure SQL and NoSQL databases, messaging and eventing systems, Redis cache, storage and directory services. As always, use tools and frameworks that you know and love – Spring, Tomcat, WildFly, JBoss, WebLogic, WebSphere, Maven, Gradle, IntelliJ, Eclipse, Jenkins, Terraform and more.
Documentation
Upload a blob with Java - Azure Storage
Learn how to upload a blob to your Azure Storage account using the Java client library.
Get started with Azure Blob Storage client library for Java - Azure Storage
Get started developing a Java application that works with Azure Blob Storage. This article helps you set up a project and authorize access to an Azure Blob Storage endpoint.
Quickstart: Azure Blob Storage library - Java
In this quickstart, you learn how to use the Azure Blob Storage client library for Java to create a container and a blob in Blob (object) storage. Next, you learn how to download the blob to your local computer, and how to list all of the blobs in a container.