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 個月前檢視次數 261 次
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 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南