Typescript Multer Error
Multer ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉํ๋ ํ๋ก์ ํธ์ AWS์ multerS3 ๋ฅผ ์ด์ฉํ๋ค๋ฉด ๋ง์ฃผ์น #typescript ์๋ฌ๋ค.
์ฐพ๋ ๋ฐฉ๋ฒ์ ํ์ ์คํฌ๋ฆฝํธ๊ฐ ์ด๋ป๊ฒ ์ ์๋ฌ๋์ง ์ฐพ์๋ณด๋ฉด ๋๋ค.
const upload = multer({
storage: multerS3({
s3: new AWS.S3(),
bucket: "project",
key(req, file, cb) {
cb(null, `original/${Date.now()}_${path.basename(file.originalname)}`);
},
}),
limits: { fileSize: 20 * 1024 * 1024 },
});
router.post("/icon", upload.single("image"), async (req, res, next) => {
User.update(
{ icon: req.file.location.replace(/\/original\//, "/thumb/") },
// ์ด๋ถ๋ถ์์ ์๋ฌ๊ฐ ๋๊ฒ๋๋๋ฐ file์ ํ์
์ด ์ด๋ป๊ฒ ์ ์๋์๋์ง ํ์ธํด๋ณด์
{ where: { id: req.body.id } }
);
res.json(req.file.location.replace(/\/original\//, "/thumb/"));
});
ctrl + ํด๋ฆญํด์ ํ์ ์ ์๋ฅผ ๋ณด๋ฌ๊ฐ์
Request๋ Express์ ํ์ ์ ์์ด๊ณ ๊ทธ์์ file ์ธ์๋ Multer๋ก ์ ์๋์ด์๋ค (IDE๊ฐ ๋น์ฅฌ์ผ์คํ๋์ค๋ผ๋ฉด ๊ฐ๋ค ๋๊ธฐ๋งํด๋ ์ถ๋ก ์ด ๊ฐ๋ฅํ๋ค.)
๋๋ upload ๋ถ๋ถ์์ ๋ถ๋ช ํ Express.Multer๋ฅผ multerS3๋ก ํ์ฅํ๋๋ฐ ํ์ ์คํฌ๋ฆฝํธ๋ ๊ทธ๋ถ๋ถ๊น์ง ์ถ๋ก ํด์ฃผ์ง ๋ชปํ๊ฒ์ด๋ค.
ํด๊ฒฐ๋ฐฉ๋ฒ
router.post("/icon", upload.single("image"), async (req, res, next) => {
User.update(
{ icon: (req.file as Express.MulterS3.File).location.replace(/\/original\//, "/thumb/") },
// req์ file ํ์
์ MulterS3.File๋ก ์ ์ํด์ฃผ์.
{ where: { id: req.body.id } }
);
res.json((req.file as Express.MulterS3.File).location.replace(/\/original\//, "/thumb/"));
});
๋น๋จ Multer๋ฟ๋ง ์๋ ์๋ฌ๋ผ ํญ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ type ํ์ผ์ ๊น๊ฒ ๋ค์ฌ๋ค๋ณด๋ฉฐ
ํ์ ์ ์๋ฅผ ํ์ ํ๊ณ ํ์ํ๋ค๋ฉด ์์ ํด ํ์ ๋ถ์ผ์น ์ค๋ฅ๋ฅผ ๋ฏธ์ฐ์ ๋ฐฉ์งํ๋ ์ต๊ด์ด ์ค์ํ๋ค
Comments (0)