Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This article shows how to download a blob using the Azure Storage client library for JavaScript. You can download blob data to various destinations, including a local file path, stream, or text string.
Prerequisites
- The examples in this article assume you already have a project set up to work with the Azure Blob Storage client library for JavaScript. To learn about setting up your project, including package installation, importing modules, and creating an authorized client object to work with data resources, see Get started with Azure Blob Storage and JavaScript.
- 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:
- BlobClient.download
- BlobClient.downloadToBuffer (only available in Node.js runtime)
- BlobClient.downloadToFile (only available in Node.js runtime)
Download to a file path
The following example downloads a blob by using a file path with the BlobClient.downloadToFile method. This method is only available in the Node.js runtime:
async function downloadBlobToFile(containerClient, blobName, localFilePath) {
const blobClient = containerClient.getBlobClient(blobName);
await blobClient.downloadToFile(localFilePath);
}
Download as a stream
The following example downloads a blob by creating a Node.js writable stream object and then piping to that stream with the BlobClient.download method.
async function downloadBlobAsStream(containerClient, blobName, writableStream) {
const blobClient = containerClient.getBlobClient(blobName);
const downloadResponse = await blobClient.download();
downloadResponse.readableStreamBody.pipe(writableStream);
}
Download to a string
The following Node.js example downloads a blob to a string with BlobClient.download method. In Node.js, blob data returns in a readableStreamBody.
async function downloadBlobToString(containerClient, blobName) {
const blobClient = containerClient.getBlobClient(blobName);
const downloadResponse = await blobClient.download();
const downloaded = await streamToBuffer(downloadResponse.readableStreamBody);
console.log('Downloaded blob content:', downloaded.toString());
}
function streamToBuffer(readableStream) {
return new Promise((resolve, reject) => {
const chunks = [];
readableStream.on('data', (data) => {
chunks.push(data instanceof Buffer ? data : Buffer.from(data));
});
readableStream.on('end', () => {
resolve(Buffer.concat(chunks));
});
readableStream.on('error', reject);
});
}
If you're working with JavaScript in the browser, blob data returns in a promise blobBody. To learn more, see the example usage for browsers at BlobClient.download.
Data transfer validation on download
Transfer validation with CRC64 provides client-level data integrity for Azure Blob Storage, allowing you to verify that the data sent by your application is the same data stored and read from Azure. When enabled, the Blob SDK computes and validates CRC64 checksums during upload and download operations, while the service independently computes and validates CRC64 checksums for the data it receives and returns. Validation is performed on each request and across the full data stream, ensuring that the entire blob is verified even when data is transferred in partitions such as block uploads or ranged reads. See Structured Body Format for more details.
Transfer validation options can be defined at the client level using BlobClientConfig, which applies validation options to all methods called from a BlobClient instance. Alternatively, you can override transfer validation options at the operation level via options, such as BlobDownloadOptions.
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
new DefaultAzureCredential(),
{
uploadContentChecksumAlgorithm: "StorageCrc64",
downloadContentChecksumAlgorithm: "StorageCrc64",
}
);
Resources
To learn more about how to download blobs using the Azure Blob Storage client library for JavaScript, see the following resources.
Code samples
View code samples from this article (GitHub):
Download to file for JavaScript or TypeScript
Download to stream for JavaScript or TypeScript
Download to string for JavaScript or TypeScript
REST API operations
The Azure SDK for JavaScript contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar JavaScript paradigms. The client library methods for downloading blobs use the following REST API operation:
- Get Blob (REST API)
Client library resources
Related content
- This article is part of the Blob Storage developer guide for JavaScript/TypeScript. To learn more, see the full list of developer guide articles at Build your JavaScript/TypeScript app.