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ヶ月前

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ