forked from Ameen-Alam/NodeJS-File-Upload-AWS-S3-Bucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (50 loc) · 1.47 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
require('dotenv/config')
const express = require('express')
const multer = require('multer')
const AWS = require('aws-sdk')
const uuid = require('uuid').v4;
const app = express()
const port = process.env.PORT || 3000
const s3 = (process.env.NODE_ENV == "development") ?
new AWS.S3({
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
})
:
new AWS.S3()
const storage = multer.memoryStorage({
destination: function(req, file, callback) {
callback(null, '')
}
})
const upload = multer({storage}).single('image')
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.render('index');
});
app.post('/upload',upload,(req, res) => {
let myFile = req.file.originalname.split(".")
const fileType = myFile[myFile.length - 1]
const params = {
Bucket: process.env.AWS_BUCKET_NAME,
Key: `${uuid()}.${fileType}`,
Body: req.file.buffer
}
s3.upload(params, (error, data) => {
if(error){
res.status(500).send(error)
}
res.status(200).send(data)
})
var presignedGETURL = s3.getSignedUrl('getObject', {
Bucket: process.env.AWS_BUCKET_NAME,
Key: `${uuid()}.${fileType}`,
Expires: 1000*5 //time to expire in seconds
});
console.log(presignedGETURL, " presignedGETURL");
})
app.listen(port, () => {
console.log(`Server is up at ${port}`)
})