快递中的multer-s3上传太慢了

时间:2019-04-19 22:24:52

标签: node.js express multer multer-s3

在我的快速应用中,通过multer-s3上传到S3的文件非常慢(200kb耗时10秒,延迟与文件大小有关)。问题是multer-s3,但有人可以告诉我如何进一步调试库中发生的情况吗?不再支持将onFileUploadStart等传递给multer。我不知道如何在multer-s3的工作内部达到峰值,以及在哪个阶段造成延迟。

问题不是AWS S3,因为从CLI上载速度非常快,也不是问题所在,因为上载到本地磁盘的速度也很快。任何直接的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.

1 个答案:

答案 0 :(得分:0)

获得洞察力的一种方法是在响应套接字上代理write函数,并检查其调用方式:

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!');
});