无法从下载的 blob 中读取 ulatedStreamBody

Nic*_*Lee 4 javascript azure azure-storage node.js azure-blob-storage

我可以检查内部缓冲区以查看我的文本数据是否存在?我是否正确使用了node.js的Stream.read()?

我有一个文本文件作为 blob 存储在 azure 存储上。当我下载 blob 时,我会获得可读流以及有关 blob 的信息。返回数据的 contentLength 为 11,这是正确的。

我无法阅读蒸汽。它总是返回 null。Node.js 文档说,

Readable.read() 方法从内部缓冲区中提取一些数据并将其返回。如果没有数据可供读取,则返回 null。

根据 Node.js,没有可用的数据。

async function downloadData(){
    const textfile = "name.txt"

    const containerURL = ContainerURL.fromServiceURL(serviceURL, "batches")
    const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, textfile );
    let baseLineImage = await blockBlobURL.download(aborter, 0)

    console.log(baseLineImage.readableStreamBody.read())
    return

}
Run Code Online (Sandbox Code Playgroud)

该方法blobBlobURL.download下载数据。更具体到Azure吧,

从系统读取或下载 blob,包括其元数据和属性。您还可以调用 Get Blob 来读取快照。

在 Node.js 中,数据以可读流 ReadableStreamBody 的形式返回 在浏览器中,数据以 Promise blobBody 的形式返回

Pet*_*Pan 5

根据您的代码,我看到您正在使用Azure Storage SDK V10 for JavaScript

在这个包的 npm 页面中@azure/storage-blob,示例代码中有一个名为 async 的函数streamToString,它可以帮助您从可读流中读取内容,如下所示。

// A helper method used to read a Node.js readable stream into 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);
  });
}
Run Code Online (Sandbox Code Playgroud)

然后,您的代码将如下所示编写。

async function downloadData(){
    const textfile = "name.txt"

    const containerURL = ContainerURL.fromServiceURL(serviceURL, "batches");
    const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, textfile );
    let baseLineImage = await blockBlobURL.download(aborter, 0);

    let content = await streamToString(baseLineImage.readableStreamBody);
    console.log(content)
    return content
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你。