- multer์ AWS S3์ ์ฌ์ฉํด ์ฌ์ฉ์๊ฐ ํ ์คํธ์ ์ด๋ฏธ์ง๋ฅผ ํฌํจํ ๊ฒ์๋ฌผ์ ์ ๋ก๋ํ ์ ์๋ API๋ฅผ ๋ง๋ค์ด ๋ณด์.
- ์ด๋ฏธ์ง๋ AWS S3์ ์ ์ฅ๋๊ณ , ์ ์ฅ๋ ์ด๋ฏธ์ง์ URL์ MongoDB์ ํจ๊ป ์ ์ฅ๋จ.
- ์ฌ์ฉ์๋ ๋ก๊ทธ์ธ ํ ๊ฒ์๋ฌผ์ ์ ๋ก๋ํ ์ ์์!
- ์ฌ์ฉ์๋ ๊ฒ์๋ฌผ์ ์
๋ก๋ํ ๋ ์ต์ ํ ์ฅ์ ์ด๋ฏธ์ง๋ฅผ ํฌํจํด์ผ ํจ(ํ
์คํธ๋ ๋น์ด๋ ๊ด์ฐฎ์)!
1. ๊ฒ์๋ฌผ ์ ๋ก๋ ๋ผ์ฐํธ
- auth ๋ฏธ๋ค์จ์ด๋ฅผ ์ฌ์ฉํด ๋ก๊ทธ์ธ๋ ์ฌ์ฉ์๋ง ์ ๊ทผํ ์ ์๋๋ก ์ ํํจ.
- multer๋ฅผ ์ด์ฉํด์ ์ต๋ 10๊ฐ์ ์ด๋ฏธ์ง๋ฅผ ๋ฉ๋ชจ๋ฆฌ ์ ์ฅ์์ ์ ์ฅํ๊ณ AWS S3 ๋ฒํท์ ์ ๋ก๋ํจ.
- ๊ฐ ์ด๋ฏธ์ง๋ ๊ณ ์ ํ ํ์ผ ์ด๋ฆ์ ๊ฐ์ง๊ณ , S3์ ์ ์ฅ๋ ํ URL์ด ์์ฑ๋จ.
1) ์ฝ๋
// uploadPost.js
const express = require("express");
const router = express.Router();
router.use(express.json());
const { Post } = require("../../models/Post");
const { auth } = require("../auth");
const cookieParser = require("cookie-parser");
router.use(cookieParser());
const multer = require("multer");
const { PutObjectCommand } = require("@aws-sdk/client-s3");
const storage = multer.memoryStorage();
const upload = multer({ storage }).array("images", 10); // ์ต๋ 10์ฅ์ ์ด๋ฏธ์ง ์
๋ก๋
const s3 = require("../../config/s3"); // S3 ํด๋ผ์ด์ธํธ ๊ฐ์ ธ์ค๊ธฐ
// ๊ฒ์๋ฌผ ์
๋ก๋
router.post("/", auth, upload, async (req, res) => {
const { text } = req.body;
const userId = req.user._id; // ๋ก๊ทธ์ธ๋ ์ฌ์ฉ์ ID ๊ฐ์ ธ์ค๊ธฐ
if (!userId) {
return res.status(401).json({
message: "๋ก๊ทธ์ธ๋ ์ฌ์ฉ์๋ง ๊ฒ์๋ฌผ์ ์
๋ก๋ํ ์ ์์ต๋๋ค.",
});
}
// ์ด๋ฏธ์ง๊ฐ ์ต์ ํ ์ฅ ์ด์ ํฌํจ๋์๋์ง ํ์ธ
if (!req.files || req.files.length === 0) {
return res.status(400).json({
message: "์ต์ 1์ฅ์ ์ด๋ฏธ์ง๋ฅผ ํฌํจํด์ผ ๊ฒ์๋ฌผ์ ์
๋ก๋ํ ์ ์์ต๋๋ค.",
});
}
try {
// S3 ์
๋ก๋ํ ์ด๋ฏธ์ง URL ๋ฐฐ์ด ์์ฑ
const imageUrls = [];
for (let file of req.files) {
const filename = `${Date.now()}_${file.originalname}`;
const uploadParams = {
Bucket: "post-jae",
Key: filename,
Body: file.buffer,
ContentType: file.mimetype,
};
// S3์ ํ์ผ ์
๋ก๋
await s3.send(new PutObjectCommand(uploadParams));
const imageUrl = `https://${uploadParams.Bucket}.s3.${process.env.AWS_REGION}.amazonaws.com/${filename}`;
imageUrls.push(imageUrl);
}
// ์๋ก์ด ๊ฒ์๋ฌผ ์์ฑ ๋ฐ ์ ์ฅ
const newPost = new Post({
user: userId,
text: text || "",
images: imageUrls,
});
await newPost.save();
return res.status(201).json({
message: "๊ฒ์๋ฌผ์ด ์ฑ๊ณต์ ์ผ๋ก ์
๋ก๋๋์์ต๋๋ค.",
post: newPost,
});
} catch (err) {
return res.status(500).json({
message: "๊ฒ์๋ฌผ ์
๋ก๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.",
error: err.message,
});
}
});
module.exports = router;
// auth.js
const { User } = require("../models/User");
let auth = (req, res, next) => {
// ํด๋ผ์ด์ธํธ๋ก๋ถํฐ ์ฟ ํค ๊ฐ์ ธ์ค๊ธฐ
let token = req.cookies.x_auth;
console.log("Received token:", token); // ํ ํฐ ๋ก๊ทธ
// ํ ํฐ ๋ณตํธํ ํ user ์ฐพ๊ธฐ
User.findByToken(token)
.then((user) => {
if (!user) {
throw new Error("์ ํจํ์ง ์์ ํ ํฐ์
๋๋ค."); // ์ฌ์ฉ์ ์์
}
console.log("Authenticated user:", user); // ์ฌ์ฉ์ ๋ก๊ทธ
req.token = token;
req.user = user;
return next();
})
.catch((err) => {
console.error("Authentication error:", err.message); // ์๋ฌ ๋ก๊ทธ
return res.status(401).json({
isAuth: false,
message:
err.message === "์ ํจํ์ง ์์ ํ ํฐ์
๋๋ค."
? "๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค."
: err.message,
});
});
};
module.exports = { auth };
2) ๊ธฐ๋ฅ ์ธ๋ถ ํ๋ฆ
- multer์ memoryStorage๋ฅผ ์ด์ฉํด ์ด๋ฏธ์ง๋ฅผ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅํ๊ณ , ๋ฐฐ์ด ํํ๋ก ํ์ผ ๋ฐ์.
- ๋ก๊ทธ์ธ ์ฌ๋ถ๋ฅผ ํ์ธํ๊ธฐ ์ํด auth ๋ฏธ๋ค์จ์ด ํธ์ถํจ.
- ์ฌ์ฉ์๊ฐ ์ ๋ก๋ํ ๊ฐ ํ์ผ์ ๋ํด ๊ณ ์ ํ์ผ ์ด๋ฆ์ ์์ฑํ๊ณ , PutObjectCommand๋ฅผ ํตํด S3์ ์ ๋ก๋ํจ
- S3 ์
๋ก๋ ํ ๊ฐ ์ด๋ฏธ์ง URL์ ์์งํด MongoDB Post ์ปฌ๋ ์
์ ํจ๊ป ์ ์ฅํจ.
3) ๋ฐ์ดํฐ๋ฒ ์ด์ค์ AWS S3 ์ฐ๋
- MongoDB์ ์ ์ฅ๋๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ
- user: ๊ฒ์๋ฌผ ์์ฑ์์ ID
- text: ๊ฒ์๋ฌผ ๋ด์ฉ
- images: S3์ ์ ๋ก๋๋ ๊ฐ ์ด๋ฏธ์ง์ URL ๋ฐฐ์ด
- createdAt: ์์ฑ ์ผ์ (์๋ ์์ฑ)
# mongoDB์ ์ ์ฅ๋ ๊ฒ์๋ฌผ ๊ตฌ์กฐ
{
"_id": "๊ฒ์๋ฌผ ID",
"user": "์ฌ์ฉ์ ID",
"text": "๊ฒ์๋ฌผ ๋ด์ฉ",
"images": [
"https://post-jae.s3.ap-northeast-2.amazonaws.com/test.jpg"
],
"createdAt": "2024-11-02T06:59:29.107+00:00", # ๊ฒ์๋ฌผ ์์ฑ ์๊ฐ
}
2. ๊ฒฐ๊ณผ ๐
- Postman์ผ๋ก ๋ก๊ทธ์ธ ํ, POST ๋ณด๋ด๊ธฐ
1) POST ์คํจ
- ๋ก๊ทธ์ธ์ด ๋์ด ์์ง ์์ ๊ฒฝ์ฐ
- ์ด๋ฏธ์ง๊ฐ ์๋ ํ ์คํธ ๊ฒ์๋ฌผ์ ์ ๋ก๋ํ๋ ค๊ณ ํ๋ ๊ฒฝ์ฐ
2) POST ์ฑ๊ณต
- MongoDB
- AWS S3 ๋ฒํท