【问题标题】:multer-s3 upload within express is ridiculously slowexpress 中的 multer-s3 上传速度非常慢
【发布时间】:2019-09-10 02:46:14
【问题描述】:

在我的 express 应用程序中通过 multer-s3 将文件上传到 S3 速度非常慢(200kb 需要 10 秒,延迟与文件大小相关)。问题是 multer-s3 但有人可以告诉如何进一步调试库中发生的事情吗?不再支持将 onFileUploadStart 等传递给 multer。我不知道如何在 multer-s3 正在做的事情中达到峰值,以及延迟是在哪个阶段引起的。

问题不在于 AWS S3,因为从 CLI 上传速度非常快,也不是 multer,因为上传到本地磁盘也很快。任何直接的 S3 javascript 操作也可以按预期快速运行。

尝试关闭 multer 的内容自动输入功能,但效果不大。

var upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: bucketName,
    acl: 'public-read',
    contentType: multerS3.AUTO_CONTENT_TYPE,
    metadata: function (req, file, cb) {
      cb(null, {fieldName: file.fieldname});
    },
    key: function (req, file, cb) {
      cb(null, Date.now().toString() + "-" + file.originalname);
    }
  },)
})

router.post('/', upload.array('files'), function(req, res, next) {
  console.log('Successfully uploaded ' + req.files.length + ' files!');
});

Want to have sub-second latency as I do with direct AWS CLI upload or using multer to store to local disk, takes 10 seconds to minute or more on sub-MB files.

【问题讨论】:

    标签: node.js express multer multer-s3


    【解决方案1】:

    获得洞察力的一种方法是在响应套接字上代理写入函数,并检查它是如何被调用的:

    var upload = multer({
      storage: multerS3({
        s3: s3,
        bucket: bucketName,
        acl: 'public-read',
        contentType: multerS3.AUTO_CONTENT_TYPE,
        metadata: function (req, file, cb) {
          cb(null, {fieldName: file.fieldname});
        },
        key: function (req, file, cb) {
          cb(null, Date.now().toString() + "-" + file.originalname);
        }
      },)
    })
    
    router.post('/'
    (req, res, next) => { // debug middleware
      const write = res.socket.write;
      res.socket.write = (function(...args) {
        // inspect args, look at the time between invocations of write
        // also check the size of the chunks being passed to write
        return write(...args)
      }).bind(res.socket);
      next();
    }
    , upload.array('files'), function(req, res, next) {
      console.log('Successfully uploaded ' + req.files.length + ' files!');
    });
    

    【讨论】:

    • 谢谢,但是 10 秒的延迟恰好在调用 upload.array() 函数之间和第一个 res.socket.write() 之间,所以这没有帮助。延迟在 multer-s3 库中,我不知道如何调试。
    猜你喜欢
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 2021-12-04
    相关资源
    最近更新 更多