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
feita há 2 meses261 visualizações
2 Respostas
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
ESPECIALISTA
respondido há 2 meses
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
ESPECIALISTA
respondido há 2 meses

Você não está conectado. Fazer login para postar uma resposta.

Uma boa resposta responde claramente à pergunta, dá feedback construtivo e incentiva o crescimento profissional de quem perguntou.

Diretrizes para responder a perguntas