-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (47 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
const express = require('express');
const multer = require('multer');
require('dotenv').config()
const AWS = require('aws-sdk');
const app = express();
const bodyParser = require('body-parser');
const upload = multer();
const s3 = new AWS.S3({
endpoint: process.env.ENDPOINT,
credentials: {
accessKeyId: process.env.ACCESS_KEY,
secretAccessKey: process.env.SECRET_ACCESS_KEY,
},
});
app.use(
bodyParser.urlencoded({
extended: true,
}),
);
app.post('/upload', upload.none(), (req, res) => {
const buf = Buffer.from(
req.body.imageBinary.replace(/^data:image\/\w+;base64,/, ''),
'base64',
);
const params = {
Bucket: 'your-bucket-name', // pass your bucket name
Key: `${Math.floor(Date.now() / 1000)}.png`, //here is your file name
Body: buf,
ACL: 'public-read',
ContentEncoding: 'base64',
ContentType: 'image/png',
};
s3.upload(params, (s3Err, data) => {
if (s3Err) {
res.json({
success: false,
error: s3Err,
});
} else {
res.json({
success: true,
data: data.Location,
});
}
});
});
app.listen(3000);