-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
80 lines (49 loc) · 1.48 KB
/
index.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
require('dotenv').config();
const S3 = require('aws-sdk/clients/s3');
const moment = require('moment');
const express = require('express')
const fileUpload = require('express-fileupload');
const app = express()
app.use(fileUpload({
limits: { fileSize: 50 * 1024 * 1024 },
}));
const s3 = new S3({
accessKeyId: process.env.ACCESS_ID,
secretAccessKey: process.env.ACCESS_KEY
});
app.get('/info', function (req, res) {
res.send({
status:true,
version: "1.0.0",
name:"s3_upload_express_server"
})
});
app.post('/upload', async (req, res) => {
uploadRequestFile(req,res);
});
const uploadRequestFile = async (req,res) => {
// s3 upload config params
const params = {
Bucket: process.env.BUCKET,
Key: `${process.env.BUCKET_PATH}/${moment().unix()}_${req.files.file.name}`, // file name with timestamp
Body: req.files.file.data,
ACL:'public-read', // public read policy
ContentType:req.files.file.mimetype
};
// Uploading files to the bucket
s3.upload(params, (err, data) => {
if (err) {
res.json({
status: false,
'error':err
});
}
console.log(`Video File uploaded successfully. ${data.Location}`);
res.json({
status: true,
video_url:data.Location
});
});
};
app.listen(process.env.PORT);
console.log("Server started at ===> ", process.env.PORT)