A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://learn.microsoft.com/en-us/azure/storage/blobs/storage-blob-download-javascript below:

Download a blob with JavaScript or TypeScript - Azure Storage

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 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 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);
}
async function downloadBlobToFile(
  containerClient: ContainerClient,
  blobName: string,
  filePath: string
): Promise<void> {

  const blobClient = containerClient.getBlobClient(blobName);

  await blobClient.downloadToFile(filePath);
}
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);
}
async function downloadBlobAsStream(
  containerClient: ContainerClient,
  blobName: string,
  writableStream: fs.WriteStream
) {
  
  const blobClient: BlobClient = containerClient.getBlobClient(blobName);

  const downloadResponse = await blobClient.download();

  if (!downloadResponse.errorCode && downloadResponse?.readableStreamBody) {
    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);
    });
}
async function downloadBlobToString(
  containerClient: ContainerClient,
  blobName: string
): Promise<void> {

  const blobClient: BlobClient = containerClient.getBlobClient(blobName);

  const downloadResponse: BlobDownloadResponseParsed =
    await blobClient.download();

  if (!downloadResponse.errorCode && downloadResponse.readableStreamBody) {
    const downloaded = await streamToBuffer(
      downloadResponse.readableStreamBody
    );
    if (downloaded) {
      console.log('Downloaded blob content:', downloaded.toString());
    }
  }
}

function streamToBuffer(readableStream) {
  return new Promise((resolve, reject) => {
    const chunks: Buffer[] = [];

    readableStream.on('data', (data) => {
      const content: Buffer = data instanceof Buffer ? data : Buffer.from(data);
      chunks.push(content);
    });
    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.

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):

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:

Client library resources

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4