-
Notifications
You must be signed in to change notification settings - Fork 17
/
s3-upload.js
32 lines (27 loc) · 874 Bytes
/
s3-upload.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
const aws = require('aws-sdk');
const express = require('express');
const multer = require('multer');
const multerS3 = require('multer-s3');
const uuid = require('uuid').v4;
const path = require('path');
const app = express();
const s3 = new aws.S3({ apiVersion: '2006-03-01' });
// Needs AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
const upload = multer({
storage: multerS3({
s3,
bucket: '<your bucket name>',
metadata: (req, file, cb) => {
cb(null, { fieldName: file.fieldname });
},
key: (req, file, cb) => {
const ext = path.extname(file.originalname);
cb(null, `${uuid()}${ext}`);
}
})
});
app.use(express.static('public'))
app.post('/upload', upload.array('avatar'), (req, res) => {
return res.json({ status: 'OK', uploaded: req.files.length });
});
app.listen(3001);