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달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인