-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupload.js
More file actions
33 lines (28 loc) · 923 Bytes
/
upload.js
File metadata and controls
33 lines (28 loc) · 923 Bytes
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
var AWS = require("aws-sdk");
var multer = require("multer");
var multerS3 = require("multer-s3");
var path = require("path");
require("dotenv").config();
AWS.config.update({
region: "ap-northeast-2",
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
});
const s3 = new AWS.S3();
const allowedExtensions = [".png", ".jpg", ".jpeg", ".bmp"];
const imageUploader = multer({
storage: multerS3({
s3: s3,
bucket: process.env.BUCKET_NAME,
key: (req, file, callback) => {
const uploadDirectory = req.query.directory ?? "";
const extension = path.extname(file.originalname);
if (!allowedExtensions.includes(extension)) {
return callback(new Error("wrong extension"));
}
callback(null, `${uploadDirectory}/${Date.now()}_${file.originalname}`);
},
acl: "public-read-write",
}),
});
module.exports = imageUploader;