无法从下载的Blob中读取readyStreamBody

时间:2019-06-05 18:13:43

标签: javascript node.js azure azure-storage azure-storage-blobs

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

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

我看不懂蒸汽。它始终返回null。 node.js文档说,

  

visible.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

}

方法blobBlobURL.download下载数据。专门针对Azure it

  

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

     

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

1 个答案:

答案 0 :(得分:2)

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

在此程序包@azure/storage-blob的npm页面中,示例代码中有一个名为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);
  });
}

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

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
}

希望有帮助。