尝试将标头发送到S3时,无法设置标头发送错误

时间:2019-05-18 21:41:18

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

我正在尝试使s3工作。

我将文件上传到文件输入中:

input.uploadArtistPhoto-js.br3(type="file" name="photo" id="photo")

然后在控制器上,我上传并调整大小:

exports.upload = multer(multerOptions).single('photo')

exports.resize = async (req, res, next) => {
  // check if there is no new file to resize
  if (!req.file) {
    next(); // skip to the next middleware
    return;
  }
  const extension = req.file.mimetype.split('/')[1]
  req.body.photo = `${uuid.v4()}.${extension}`
  // now we resize
  const photo = await jimp.read(req.file.buffer)

  await photo.cover(300, 300);
  // await photo.resize(800, jimp.AUTO);

  await singleUpload(req, res, function (error) {

    return res.json({'imageUrl': req.file.buffer})
  })

  next()
};

这个文件是singleUpload:

// envs
aws.config.update({
    secretAccessKey: 'XXXXX',
    accessKeyId: 'YYYYY',
    region: 'us-east-1'
})

const s3 = new aws.S3()


const fileFilter = (req, file, cb) => {
  if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
      cb(null, true)
  } else {
      cb(new Error('Invalid Mime Type, only JPEG and PNG'), false);
  }
}

const upload = multer({
  fileFilter,
  storage: multerS3({
    s3: s3,
    bucket: 'images',
    ACL: 'public-read',
    metadata: function (req, file, cb) {
      cb(null, {fieldName: file.fieldname});
    },
    key: function (req, file, cb) {
      cb(null, Date.now().toString())
    }
  })
})

module.exports = upload

当我尝试上传文件时,出现错误:Can't set headers after they are sent

任何想法可能是什么问题?

我已经设置了凭据,还设置了一些cors配置和策略

0 个答案:

没有答案