为什么我的 multer 文件无法上传到 AWS s3?

问题描述 投票:0回答:1

我能够在本地上传文件,但很难将其上传到 AWS S3 存储桶上。提交请求后,我收到通用的 Cannot set headers after they are sent to the client 错误。该请求没有发送到

createDoucment
中间件,所以我很确定
uploadFile
有问题,但我不知道如何调试它。 我在网上找到的所有指南都使用(即将弃用)SDKv2,而我正在尝试使用 V3。

控制器:

const s3 = new S3Client({
  credentials: {
    accessKeyId: "myid",
    secretAccessKey: "mykey",
  },
  region: "us-east-2",
});
const s3Storage = multerS3({
  s3: s3,
  bucket: "mybucket",
  acl: "public-read",
  metadata: (req, file, cb) => {
    cb(null, { fieldname: file.fieldname });
  },
  key: (req, file, cb) => {
    cb(null, Date.now().toString());
  },
});
const upload = multer({
  storage: s3Storage,
});
exports.uploadFile = upload.single("file");
exports.createDocument = catchAsync(async (req, res, next) => {
  const newDoc = await Model.create({
    title: req.body.title,
    file: req.file.filename,
  });
  res.status(201).json({
    status: "success",
    data: { data: newDoc },
  });
});

路由器:

router.route("/").post(uploadFile, createDocument);
node.js amazon-web-services amazon-s3 multer multer-s3
1个回答
0
投票

我发现了,删除

acl: "public-read"
行后它就可以工作了,因为我的存储桶中没有配置 acl

© www.soinside.com 2019 - 2024. All rights reserved.