-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (53 loc) · 1.24 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
let express = require("express");
let app = express();
let cors = require("cors");
const AWS = require("aws-sdk");
const s3 = new AWS.S3();
AWS.config.update({
accessKeyId: "YOUR_ACCESS_KEY",
secretAccessKey: "YOUR_SECRET_KEY",
signatureVersion: "v4",
});
const myBucket = "YOUR_BUCKET_NAME";
const signedUrlExpireSeconds = 60 * 5;
app.use(cors());
app.get("/uploadFile", function (req, res) {
let myKey = req.data.myKey;
let mimeType = req.data.mimeType
let params = {
Bucket: myBucket,
Key: myKey,
ContentType: mimeType,
Expires: signedUrlExpireSeconds,
}
const url = s3.getSignedUrl("putObject",params);
res.end(url);
});
app.delete("/deleteFile", function (req, res) {
let myKey = req.data.myKey;
let params = {
Bucket: myBucket,
Delete: {
Objects: [
{
Key: myKey,
},
],
},
};
s3.deleteObjects(params, function (err, data) {
if (err) {
console.error(err, err.stack);
res.end(err);
}
else {
console.log(data);
res.end("deleted");
}
});
});
let server = app.listen(8080, function () {
let host = server.address().address;
let port = server.address().port;
console.log("Example app listening at http://%s:%s", host, port);
});