This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
This article shows how to download a blob using theAzure Storage client library for JavaScript. You can download blob data to various destinations, including a local file path, stream, or text string.
You can use any of the following methods to download a blob:
The following example downloads a blob by using a file path with theBlobClient.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);}The following example downloads a blob by creating a Node.js writable stream object and then piping to that stream with theBlobClient.download method.
async function downloadBlobAsStream(containerClient, blobName, writableStream) { const blobClient = containerClient.getBlobClient(blobName); const downloadResponse = await blobClient.download(); downloadResponse.readableStreamBody.pipe(writableStream);}The following Node.js example downloads a blob to a string withBlobClient.download method. In Node.js, blob data returns in areadableStreamBody.
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 promiseblobBody. To learn more, see the example usage for browsers atBlobClient.download.
To learn more about how to download blobs using the Azure Blob Storage client library for JavaScript, see the following resources.
View code samples from this article (GitHub):
Download to file forJavaScript orTypeScript
Download to stream forJavaScript orTypeScript
Download to string forJavaScript orTypeScript
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:
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?