-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
62 lines (50 loc) · 1.54 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import express from "express";
import multer from "multer";
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
import dotenv from "dotenv";
import cors from "cors";
dotenv.config();
const app = express();
const port = process.env.PORT || 5000;
// Enable CORS
app.use(cors());
// Parse JSON request bodies
app.use(express.json());
// Create an S3 client
const s3Client = new S3Client({
region: process.env.AWS_ACCOUNT_REGION,
credentials: {
accessKeyId: process.env.AWS_ACCOUNT_ACCESS_KEY,
secretAccessKey: process.env.AWS_ACCOUNT_SECRET_ACCESS_KEY,
},
});
// Configure multer for file uploads
const storage = multer.memoryStorage();
const upload = multer({
storage,
limits: {
fileSize: 25 * 1024 * 1024, // 25MB in bytes
},
});
app.post("/api/upload", upload.single("file"), async (req, res) => {
try {
const file = req.file;
const contentType = file.mimetype;
const params = {
Bucket: process.env.AWS_BUCKET_NAME,
Key: file.originalname,
Body: file.buffer,
ContentType: contentType,
};
const response = await s3Client.send(new PutObjectCommand(params));
console.log("File uploaded to S3:", response);
const fileUrl = `https://${process.env.AWS_BUCKET_NAME}.s3.amazonaws.com/${file.originalname}`;
res.status(200).json({ fileUrl });
} catch (error) {
console.error("Failed to upload file to S3:", error);
res.status(500).json({ error: "Failed to upload file to S3" });
}
});
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});