【问题标题】:Memory buffers fills up when I stream upload a file, though passing a disk storage location param当我流式上传文件时,内存缓冲区会填满,尽管传递了磁盘存储位置参数
【发布时间】:2023-03-30 17:30:01
【问题描述】:

尝试使用 skipper 和 skipper-s3 使用此代码将流式上传到 S3

image_upload : function  (req,res) {

    // depends on bodyparser:skipper in confing/http.js

    console.log("Uploading image .. ");

    req.file('image')
        .upload({
            adapter: require('skipper-s3'),
            key: KEY,
            secret: SECRET,
            bucket: 'mybucket',
            tmpdir: 'tmp/s3-multiparts'
        }, function whenDone(err, uploadedFiles) {
            if (err){
                return res.negotiate(err);
            } else {
                dict = {
                    files: uploadedFiles,
                    textParams: req.params.all()
                }
                return res.ok(dict);
            }
        }

    );
};

我有 2 个问题:

首先:当我尝试上传一些文件时,我可以看到这个文件在内存中缓冲,而不是只是缓冲到磁盘(路径添加到选项中)。

其实这是个大问题,当有很多用户同时上传时,我的内存会很快填满

第二次:当上传大于 5MB 时,我的应用程序崩溃,出现此错误

事件.js:85 投掷者; // 未处理的“错误”事件 ^ 错误:请求中止 在 IncomingMessage.onReqAborted (~Desktop/myApp/node_modules/sails/node_modules/skipper/node_modules/multiparty/index.js:175:17) 在 IncomingMessage.emit (events.js:104:17) 在 abortIncoming (_http_server.js:279:11) 在 Socket.serverSocketCloseListener (_http_server.js:292:5) 在 Socket.emit (events.js:129:20) 在 TCP.close (net.js:485:12)

【问题讨论】:

    标签: javascript node.js amazon-s3 sails.js


    【解决方案1】:

    第二个问题的解决方案: 您可以指定要上传的文件的最大大小。默认为 5 MB。

    req.file('some_file').upload({
        dirname: 'path to store the file',/* optional. defaults to assets/uploads I guess*/
        saveAs: 'new file name', /* optional. default file name */
        maxBytes: 5 * 1024 * 1024 //5 MB
    }, function(err, uploadedFiles) {
    
    });
    

    我不确定第一个问题。您无需在选项中指定 tempdir。 Skipper-S3 默认将文件写入<your project root>/.tmp/s3-upload-part-queue 以供上传。我不明白为什么内存应该被填满。您可以删除tempdir 并检查吗?

    【讨论】:

    • 感谢您的回答!我尝试了使用和不使用 tmpDir,它没有区别,它不断将上传的文件加载/缓冲到内存中
    • @securecurve 引用船长文档,不确定是否有帮助; >缓冲多部分请求可以休息的目录的路径。亚马逊要求发送到其分段上传 API 的“部分”大小至少为 5MB,因此该目录用于将数据块排队,直到积累了足够大的有效负载。默认为 .tmp/s3-upload-part-queue(从节点进程的当前工作目录解析 - 例如您的应用程序)
    • 也许,你可以避免使用skipper-s3并将文件写入磁盘,然后使用aws-sdk上传到S3
    • 是的,我在文档中阅读了这部分,这就是我打开磁盘缓冲的原因,这里的主要问题是内存缓冲正在发生,这是一个负面的事情,因为这意味着我会需要大内存,同时浪费已经用于此的磁盘空间
    • 顺便说一句,我试过你建议的解决方案maxBytes,但没有成功。可能这不适用于skipper-s3
    猜你喜欢
    • 2011-04-27
    • 2012-08-25
    • 2015-07-13
    • 2011-12-18
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 2012-06-24
    相关资源
    最近更新 更多