无法使用Multer S3 NodeJ将大文件上传到AWS

时间:2019-06-18 00:41:32

标签: node.js amazon-s3 multer bucket multer-s3

我无法使用multer-s3将大文件上传到AWS。我正在使用以下代码:

const upload = multer({
  storage: multerS3({
    s3,
    bucket: 'welive-inc',
    acl: 'public-read',
    metadata: function (req, file, cb) {
      cb(null, {fieldName: 'TESTING_META_DATA!'});
    },
    key: function (req, file, cb) {
      cb(null, Date.now().toString() + randtoken.uid(16))
    }
  })
})

const singleUpload = upload.single('file');

router.post('/image-upload', (req, res) =>{
  singleUpload(req, res, function(err) {
    if (err) {
      console.log(err)
      return res.status(422).send({errors: [{title: 'File Upload Error', detail: err.message}] });
    }
    return res.json({'imageUrl': req.file.location});
  });
});

此代码适用于较小的文件(图像或很小的视频),但是相对较大的文件则无效。并且console.log(err)返回此错误:

{ Error: write EPIPE
    at WriteWrap.onWriteComplete [as oncomplete] (internal/stream_base_commons.js:66:16)
  message: 'write EPIPE',
  errno: 'EPIPE',
  code: 'NetworkingError',
  syscall: 'write',
  region: 'eu-west-3',
  hostname: 'welive-inc.s3.eu-west-3.amazonaws.com',
  retryable: true,
  time: 2019-06-17T21:15:46.958Z,
  statusCode: 400,
  storageErrors: [] }

前端甚至不等待响应,并在几分钟后返回此错误

net::ERR_EMPTY_RESPONSE

1 个答案:

答案 0 :(得分:1)

尝试覆盖默认的上传大小

var limits = {
files: 1, // allow only 1 file per request
fileSize: <Mbs allowed> * 1024 * 1024, // (replace MBs allowed with your desires)
};

然后将其应用于您的multer实例

const upload = multer({
  limits: limits,
  storage: multerS3({
    s3,
    bucket: 'welive-inc',
    acl: 'public-read',
    metadata: function (req, file, cb) {
      cb(null, {fieldName: 'TESTING_META_DATA!'});
    },
    key: function (req, file, cb) {
      cb(null, Date.now().toString() + randtoken.uid(16))
    }
  })
})