Why won't my multer file uploads to AWS s3 work?

0

I was able to get my files to upload locally but am struggling to get it on AWS S3 Buckets. I'm receiving generic "cannot set headers after they are sent to the client" errors after submitting a request. All of the guides that I have found online are using the (soon to be deprecated) SDKv2 and I'm trying to use V3. I followed the multer-s3 documentation but I'm messing something up. I created a user on AWS with a custom policy that grants permission to write and read objects connected to my bucket as well.

controller:

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:

router.route("/").post(uploadFile, createDocument);
NEPTR
已提问 2 个月前262 查看次数
2 回答
1

Hello.

I set "acl: "public-read"" when uploading the object, but I don't think the S3 bucket allows "public-read" by default.
https://docs.aws.amazon.com/AmazonS3/latest/userguide/acls.html

ACL must be enabled to allow "public-read".
https://repost.aws/knowledge-center/read-access-objects-s3-bucket

I thought that if you don't need to use "public-read", you can upload by setting "acl" to "private".
https://www.npmjs.com/package/multer-s3

profile picture
专家
已回答 2 个月前
1

Maybe you can use req.file.key to reference the file stored in S3:

 const newDoc = await Model.create({
    title: req.body.title,
    file: req.file.key, // your forgot to use req.file.key (The name of the file)
  });

Sources:

Multer s3 File information

profile picture
专家
已回答 2 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则