-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (59 loc) · 1.75 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
/**
* @author janak
* @2022
*/
const HTTPStatus = require('http-status');
const express = require('express'),
aws = require('aws-sdk'),
bodyParser = require('body-parser'),
multer = require('multer'),
multerS3 = require('multer-s3');
aws.config.update({
secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
accessKeyId: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
region: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx'
});
const app = express(),
s3 = new aws.S3();
app.use(bodyParser.json());
const uploadFileHelper = multer({
storage: multerS3({
s3: s3,
bucket: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXx',
key: (req, file, cb) => {
console.log(file);
cb(null, Date.now()+'_'+file.originalname);
}
})
});
app.post('/upload-file', uploadFileHelper.single('file'), (req, res, next) => {
try{
let responseData = null;
let response = null;
if(req){
response = res.send(req.file);
responseData = {
fileUrl : response.location
}
if(responseData){
return res.status(HTTPStatus.OK).json({
status: HTTPStatus.Ok,
err: false,
success: true,
message: "File Upload Successfully",
data : responseData
})
}
return res.status(HTTPStatus.BAD_REQUEST).json({
err: true,
success: false,
status: HTTPStatus.BAD_REQUEST,
message: 'File was not uploaded'
})
}
}catch (error) {
return next(error);
}
});
app.listen(3001)
console.log('Server is running on port: 3001');