最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

无法通过Multer S3 NodeJs将大文件上传到AWS

IT培训 admin 4浏览 0评论

无法通过Multer S3 NodeJs将大文件上传到AWS

我无法使用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',
  retryable: true,
  time: 2019-06-17T21:15:46.958Z,
  statusCode: 400,
  storageErrors: [] }

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

net::ERR_EMPTY_RESPONSE
回答如下:

multter将最大文件大小默认为1mb。

因此,请尝试覆盖它

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))
    }
  })
})

无法通过Multer S3 NodeJs将大文件上传到AWS

我无法使用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',
  retryable: true,
  time: 2019-06-17T21:15:46.958Z,
  statusCode: 400,
  storageErrors: [] }

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

net::ERR_EMPTY_RESPONSE
回答如下:

multter将最大文件大小默认为1mb。

因此,请尝试覆盖它

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))
    }
  })
})
发布评论

评论列表 (0)

  1. 暂无评论