-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
61 lines (45 loc) · 1.29 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
import multer from "multer";
import express from "express";
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
import {randomBytes} from 'crypto';
const app= express();
import env from 'dotenv';
env.config();
const storage= multer.memoryStorage();
const upload= multer({storage: storage});
const bucketName = process.env.AWS_BUCKET_NAME;
const bucketRegion = process.env.AWS_BUCKET_REGION;
const accessKey = process.env.AWS_ACCESS_KEY;
const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY;
const s3 = new S3Client({
credentials: {
accessKeyId: accessKey,
secretAccessKey: secretAccessKey,
},
region: bucketRegion,
});
app.use(express.static('public'));
function generateRandomHash() {
const rawBytes = randomBytes(16);
const hash = rawBytes.toString("hex");
return hash;
}
app.get('/',(req,res)=>{
res.send('index.html');
})
app.post('/upload-file',upload.single("file"),async (req,res)=>{
const uniquehash= generateRandomHash();
const params = {
Bucket: bucketName,
Key: uniquehash,
Body: req.file.buffer,
ContentType: req.file.mimetype,
};
const command= new PutObjectCommand(params);
await s3.send(command);
res.send("Alright");
// console.log(`File`)
})
app.listen(3000,()=>{
console.log("Server running");
})