简体   繁体   English

列出 Azure 中的 Blob 存储和读取流 Node.js

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

here is my code how I list the blobs.这是我列出 Blob 的代码。

EDITED!!!已编辑!!!

------------------------------------EDIT------------------------------------------ - - - - - - - - - - - - - - - - - - 编辑 - - - - - - - -----------------------------------------

I edited the code and rewrite in this TypeScript:我在这个 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错误错误:未捕获(承诺中):TypeError:无法读取未定义的属性“on” TypeError:无法读取未定义的属性“on”

Download the blob by calling the download method.通过调用download方法下载 blob。 The example code includes a helper function called streamToString , which is used to read a Node.js readable stream into a string .示例代码包括一个名为streamToString的助手 function ,用于将 Node.js可读 stream 读入字符串

Add this code to the end of the main function:将此代码添加到主 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:在主 function 之后添加这个助手 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 .更多细节,你可以参考这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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