首页 > 解决方案 > 在 Firebase 存储中为文件创建 readStream

问题描述

Firebase 存储的读取流:

我的 Google firebase 存储中有文件,我想为其创建读取流(使用 javascript/node js)。(然后我打算将此读取流通过管道传输到某个中间件,然后是写入流,但这对于这个问题并不重要。)代码片段显示了我在做什么,但是当我将 readStream 打印到控制台时,我得到了一个 DestroyableTransform 对象而不是 ReadableStream。我觉得我的代码与文档非常相似。有谁知道可能出了什么问题?

const filePath = 'image.png';
const getReadStream = (filePath) => {
    let file;
    try {
        file = admin
            .storage()
            .bucket()
            .file(filePath);
    } catch (err) {
        console.log(err);
    }
    const readStream = file.createReadStream()
        .on('error', (err) => {
            throw err;
        });
    console.log(readStream);
    return readStream;
};

标签: javascriptnode.jsfirebasefirebase-storage

解决方案


这是一个可能的答案。

const filePath = 'image.png';
const getReadStream = (filePath) => {
    let file;
    try {
        file = admin
            .storage()
            .bucket(filePath);
    } catch (err) {
        console.log(err);
    }
    const readStream = file.createReadStream()
        .on('error', (err) => {
            throw err;
        });
    console.log(readStream);
    return readStream;
};

您应该排除内部的“文件”句子。


推荐阅读