-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathapp.js
60 lines (43 loc) · 1.37 KB
/
app.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
require("dotenv").config()
const express = require('express')
const app = express();
app.listen(3001);
const aws = require('aws-sdk')
const multer = require('multer')
const multerS3 = require('multer-s3');
aws.config.update({
secretAccessKey: process.env.ACCESS_SECRET,
accessKeyId: process.env.ACCESS_KEY,
region: process.env.REGION,
});
const BUCKET = process.env.BUCKET
const s3 = new aws.S3();
const upload = multer({
storage: multerS3({
s3: s3,
acl: "public-read",
bucket: BUCKET,
key: function (req, file, cb) {
console.log(file);
cb(null, file.originalname)
}
})
})
app.post('/upload', upload.single('file'), async function (req, res, next) {
res.send('Successfully uploaded ' + req.file.location + ' location!')
})
app.get("/list", async (req, res) => {
let r = await s3.listObjectsV2({ Bucket: BUCKET }).promise();
let x = r.Contents.map(item => item.Key);
res.send(x)
})
app.get("/download/:filename", async (req, res) => {
const filename = req.params.filename
let x = await s3.getObject({ Bucket: BUCKET, Key: filename }).promise();
res.send(x.Body)
})
app.delete("/delete/:filename", async (req, res) => {
const filename = req.params.filename
await s3.deleteObject({ Bucket: BUCKET, Key: filename }).promise();
res.send("File Deleted Successfully")
})