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
- This article assumes you already have a project set up to work with the Azure Blob Storage client library for Java. To learn about setting up your project, including package installation, adding
import
directives, and creating an authorized client object, see Get Started with Azure Storage and Java. - The authorization mechanism must have permissions to perform a download operation. To learn more, see the authorization guidance for the following REST API operation:
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.
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:
- Get Blob (REST API)
Code samples
Client library resources
Feedback
Submit and view feedback for