【问题标题】:Unable to read readableStreamBody from downloaded blob无法从下载的 blob 中读取 readableStreamBody
【发布时间】:2019-10-21 06:50:20
【问题描述】:

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

我有一个文本文件作为 blob 存储在 azure-storage 上。当我下载 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

}

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

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

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

【问题讨论】:

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


    【解决方案1】:

    根据您的代码,我看到您使用的是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
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2012-08-03
      • 2019-09-05
      • 1970-01-01
      • 2018-09-27
      • 2013-05-11
      • 1970-01-01
      • 1970-01-01
      • 2019-09-25
      • 2019-01-22
      相关资源
      最近更新 更多