-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.js
66 lines (46 loc) · 1.51 KB
/
upload.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
import dotenv from 'dotenv';
import express from 'express';
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
import cors from 'cors';
dotenv.config();
const app = express();
app.use(express.json());
app.use(cors({
origin: 'http://localhost:5173',
}));
const port = 3002;
const accessKeyId = process.env.AWS_ACCESS_KEY_ID;
const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY;
const bucketName = process.env.AWS_BUCKET;
const region = process.env.AWS_REGION;
const s3Client = new S3Client({
region: region,
credentials: {
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
},
});
app.post('/api/upload', async (req, res) => {
const { fileName, fileType } = req.body;
// const fileKey = `uploads/${Date.now()}-${fileName}`; This will put , inside a folder
const fileKey = `aws+${Date.now()}-${fileName}`;
const command = new PutObjectCommand({
Bucket: bucketName,
Key: fileKey,
ContentType: fileType,
});
try {
const uploadURL = await getSignedUrl(s3Client, command, { expiresIn: 3600 });
res.json({ uploadURL, fileKey });
} catch (error) {
console.error('Error generating signed URL:', error);
res.status(500).json({ error: 'Failed to generate upload URL' });
}
});
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});