ํ์ฌ ํ๋ก์ ํธ์์ ์ ์ ์ธ ์ด๋ฏธ์งํ์ผ๋ค์
AWS S3
๋ฅผ ์ด์ฉํด์ ์ฒ๋ฆฌํฉ๋๋ค.
์ฒ์ S3.copyObject()
๋ฅผ ์ฌ์ฉํ ๋๋ Access Denied
๊ฐ ๋ฐ์ํ๊ธธ๋ ๋ฒํท ์ ์ฑ
์ด๋ ์ ๊ทผ ๊ถํ์ ์ ๋๋ก ๋ถ์ฌ ์ ํด์คฌ๋ค๊ณ ์ฐฉ๊ฐํด์ ๊ตฌ๊ธ๋ง์ ํ๋ฉด์ ์ด๊ฒ์ ๊ฒ ์
๋ ฅํด ๋ณด๋ฉด์ ๊ณ์ ํ
์คํธ๋ฅผ ํ์ต๋๋ค.
๋ญ ์
๋ ฅํด๋ ๊ณ์ Access Denied
๊ฐ ๋ฐ์ํ๊ธธ๋ ๋ญ๊ฐ ๋ฌธ์ ์ธ์ง ํ์ฐธ ์ฐพ๋ค๊ฐ ์ฌ๊ธฐ์์ ํํธ๋ฅผ ์ป์ด์ ํด๊ฒฐํ์ต๋๋ค.
Access Denied
๊ฐ ๋ฐ์ํ๋ ์ด์ ๊ฐ ๊ถํ ๋ฌธ์ ์ผ ์๋ ์์ง๋ง, ์๋ชป๋ ๊ฒฝ๋ก๋ฅผ ์
๋ ฅํ ๊ฒฝ์ฐ์๋ ๋ํ๋๋ ๊ฒ์ผ๋ก ์ดํดํ์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ ํํ ์ด์ ๋ ๋ชจ๋ฅด๊ฒ ์ง๋ง copyObject()
๋ฅผ ํ ๋๋ Key
์ ๋ฒํท๋ช
/
๋ ๋ถ์ฌ์ค์ผ ์ ์์ ์ผ๋ก ๊ฒฝ๋ก๋ฅผ ์ธ์ํฉ๋๋ค.
์ด ๋ถ๋ถ์ ๋ชฐ๋ผ์ ๋ค๋ฅธ ๋ฌธ์ ์ธ ์ค ์๊ณ ๋ฌธ์ ์์ธ์ ํ์ฐธ ์ฐพ์๋ค์...
์ฐธ๊ณ ๋ก ๋ฒํท ์ ์ฑ
์ "s3:GetObject"
์ "s3:PutObject"
๋ง ํ์ฉํ๊ณ , ์ ๊ทผ ๊ถํ์ ์ ๋ถ ๋ค ์ด์์ต๋๋ค.
export const copyPhoto = (originalSource: string) =>
S3.copyObject(
{
Bucket: "blemarket",
CopySource: originalSource,
Key:
originalSource.slice(0, originalSource.lastIndexOf("/")) +
"/remove" +
originalSource.slice(originalSource.lastIndexOf("/")),
},
(error, data) => {
if (error) {
console.error("error >> ", error);
}
}
);
export const copyPhoto = (originalSource: string) =>
S3.copyObject(
{
Bucket: "blemarket",
CopySource: "blemarket/" + originalSource,
Key:
originalSource.slice(0, originalSource.lastIndexOf("/")) +
"/remove" +
originalSource.slice(originalSource.lastIndexOf("/")),
},
(error, data) => {
if (error) {
console.error("error >> ", error);
}
}
);
import AWS from "aws-sdk";
import multer from "multer";
import multerS3 from "multer-s3";
// ํฌํผ ํจ์
export const getPhotoPath = (filename: string) =>
`images/${process.env.NODE_ENV}/${filename}_${Date.now()}`;
AWS.config.update({
region: process.env.BLEMARKET_AWS_REGION,
accessKeyId: process.env.BLEMARKET_AWS_ACCESS_KEY,
secretAccessKey: process.env.BLEMARKET_AWS_SECRET_KEY,
});
const S3 = new AWS.S3();
// multerS3๋ฅผ ์ด์ฉํด์ ์ด๋ฏธ์ง ์ถ๊ฐ ์ฒ๋ฆฌ
const upload = multer({
storage: multerS3({
s3: S3,
bucket: "blemarket",
key(req, file, cb) {
const filename = file.originalname.split(".")[0];
cb(null, getPhotoPath(filename));
},
}),
limits: { fileSize: 1024 * 1024 }, // 1mb
});
// ์ด๋ฏธ์ง ์ ๊ฑฐ ํจ์
export const deletePhoto = (photo: string) =>
S3.deleteObject(
{
Bucket: "blemarket",
Key: photo,
},
(error, data) => {
if (error) {
console.error("error >> ", error);
}
}
);
// ์ด๋ฏธ์ง ์ด๋ ํจ์
export const copyPhoto = (originalSource: string) =>
S3.copyObject(
{
Bucket: "blemarket",
CopySource: "blemarket/" + originalSource,
Key:
originalSource.slice(0, originalSource.lastIndexOf("/")) +
"/remove" +
originalSource.slice(originalSource.lastIndexOf("/")),
},
(error, data) => {
if (error) {
console.error("error >> ", error);
}
}
);