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

33 Views Asked by At

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. The request isn't making it to the createDoucment middleware so I'm pretty certain something is wrong with uploadFile, but I'm at a loss for how debug this. All of the guides that I have found online are using the (soon to be deprecated) SDKv2 and I'm trying to use V3.

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);
1

There are 1 best solutions below

0
Brenden Baio On

I figured it out, it works after removing the acl: "public-read" line since I didn't have acl configured in my bucket