【问题标题】:Firebase and busboy, do I get the whole file in memory, or?Firebase 和 busboy,我是否将整个文件保存在内存中,或者?
【发布时间】:2019-05-12 00:05:23
【问题描述】:

我想在 node.js/Firebase 项目中上传相当大的文件。我正在尝试了解 Busboy 是否符合我的需要。

Busboy 文档中的示例以

结尾
req.pipe(busboy);

但这在 Firebase 中不起作用。看起来你应该改用

busboy.end(req.rawBody);

这对我有用(至少在本地,在firebase serve 下。但是也许有区别。在我上面提到的示例中,您有这个:

if (req.method === 'POST') {
    let busboy = new Busboy({ headers: req.headers });
    busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
      console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
      file.on('data', (data) => {
        console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
      });

我希望看到来自file.on("data", ...) 的多个输出,但是对于 100 MB 的文件只有一行。这是否意味着整个文件都保存在内存中?

编辑:busboy.end(req.rawBody) 来自https://cloud.google.com/functions/docs/writing/http#multipart_data

【问题讨论】:

    标签: firebase pipe streaming google-cloud-functions busboy


    【解决方案1】:

    Cloud Functions 输入和输出的整个请求和响应在传入和传出客户端之前完全保存在内存中。请求和响应的最大负载大小为 10MB,如documentation 所示。

    在处理大文件时,您应该考虑上传到 Cloud Functions,然后从那里触发一个函数。

    【讨论】:

    • 谢谢道格。在这种情况下,我的想法是通过 Busboy 使用管道将文件流式传输到 Google Cloud Storage。我非常不确定在这种情况下如何工作。 Busboy 文档中的示例看起来可以通过这种方式流式传输文件。我误解了那个例子吗?还是只是这不适用于 Firebase?
    • 正如我所说,Cloud Functions 实际上并不流式传输输入。调用函数的那一刻,所有内容都在内存中。流不会按照 Cloud Functions 所需的方式进行扩展。不过,您可以使用其节点 API 从 Cloud Storage 流式传输。
    • 这只是另一种尝试将大文件上传到 GCS 的方法。请在此处查看我的其他问题,您也回答了:stackoverflow.com/questions/56237102/…
    猜你喜欢
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 2017-03-29
    • 1970-01-01
    • 2016-12-24
    相关资源
    最近更新 更多