简体   繁体   中英

List blob from Azure Blob Storage and Readstream in Node.js

here is my code how I list the blobs.

EDITED!!!

------------------------------------EDIT------------------------------------------

I edited the code and rewrite in this TypeScript:

    const blobServiceClient = new BlobServiceClient(`https://${accountName}.blob.core.windows.net?${sasToken}`,
      pipeline)
    const containerClient = blobServiceClient.getContainerClient(containerName)
    console.log(containerClient)
    if (!containerClient.exists()) {
      console.log("the container does not exit")
      await containerClient.create()

    }
    const client = containerClient.getBlockBlobClient(this.currentFile.name)

    //name of uploded blob
    console.log(this.currentFile.name)
    //metaata from the blob
    console.log(client)

    //List each blobs in the container
    for await (const blob of containerClient.listBlobsFlat()) {
      console.log('\t', blob.name);
      const blockBlobClient = containerClient.getBlockBlobClient(blob.name);
      const downloadBlockBlobResponse = await blockBlobClient.download(0);
      console.log('\nDownloaded blob content...');
      console.log('\t', await streamToString(downloadBlockBlobResponse.readableStreamBody));
      //end of loop
  }



  async function streamToString(readableStream) {
    return new Promise((resolve, reject) => {
      const chunks = [];
      readableStream.on("data", (data) => {
        chunks.push(data.toString());
      });
      readableStream.on("end", () => {
        resolve(chunks.join(""));
      });
      readableStream.on("error", reject);
    });
  }

But then I receive an error in the browser which called:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'on' of undefined TypeError: Cannot read property 'on' of undefined

Download the blob by calling the download method. The example code includes a helper function called streamToString , which is used to read a Node.js readable stream into a string .

Add this code to the end of the main function:

const blockBlobClient = containerClient.getBlockBlobClient(blob.name);
const downloadBlockBlobResponse = await blockBlobClient.download(0);
console.log('\nDownloaded blob content...');
console.log('\t', await streamToString(downloadBlockBlobResponse.readableStreamBody));

Add this helper function after the main function:

// A helper function used to read a Node.js readable stream into a string
async function streamToString(readableStream) {
  return new Promise((resolve, reject) => {
    const chunks = [];
    readableStream.on("data", (data) => {
      chunks.push(data.toString());
    });
    readableStream.on("end", () => {
      resolve(chunks.join(""));
    });
    readableStream.on("error", reject);
  });
}

For more details, you could refer to this article .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM