multer s3 읽기 권한 문제

박진형·2021년 6월 2일
0

Web

목록 보기
5/8

진스타그램에서 프로필 사진을 변경할 때 multerS3을 통해서 이미지를 Object Storage에 저장을 한다.

const storage = multerS3({
  s3: s3,
  bucket: "버켓 이름",
  contentType: multerS3.AUTO_CONTENT_TYPE,
  overwite: true,
  ACL: "public-read",
  metadata: function (req, file, cb) {
    cb(null, { fieldName: file.fieldname });
  },
  key: function (req, file, cb) {
    cb(null, `파일명`);
  },
  limits: { fileSize: 5 * 1024 * 1024 },
  
});

문제점

어떤 이유에서인지 acl을 public-read 형식으로 읽기 권한을 공개형식으로 전달해줬지만 공개 형식으로 전달되지 않았다.

해결 방법

ACL을 대문자가아닌 소문자(acl)로 바꿔준다.

const storage = multerS3({
  s3: s3,
  bucket: "버켓 이름",
  contentType: multerS3.AUTO_CONTENT_TYPE,
  overwite: true,
  metadata: function (req, file, cb) {
    cb(null, { fieldName: file.fieldname });
  },
  key: function (req, file, cb) {
    cb(null, `파일명`);
  },
  limits: { fileSize: 5 * 1024 * 1024 },
   acl: "public-read",
});

아래와 같이 s3.copyObject() 함수를 사용할 때에는 아래의 params와 같이 대문자로 써주어 매개변수로 전달해줬을 땐 전혀 문제 없었지만. 이번에는 대소문자가 중요한것 같다.

params = {
    Bucket: "jinstagram",
    CopySource: "/jinstagram" + "/" + "userProfile" + "/" + "noprofile.jpg",
    Key: `userProfile/${newUser._id}.jpg`,
    ACL: "public-read",
  };

  s3.copyObject(params, function (err, data) {
    if (err) {
      console.log("err: ", err);
    }
    console.log("============");
    console.log("data: ", data);
  });
  res.writeHead(302, { Location: `/` });
  res.end();
});

0개의 댓글