-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
45 lines (37 loc) · 964 Bytes
/
storage.js
File metadata and controls
45 lines (37 loc) · 964 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
34
35
36
37
38
39
40
41
42
43
44
45
const aws = require("aws-sdk");
const multer = require("multer");
const multerS3 = require("multer-s3");
require("dotenv").config();
const spacesEndpoint = new aws.Endpoint(process.env.AWS_ENDPOINT);
aws.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_ACCESS_KEY_SECRET,
});
const s3 = new aws.S3({
endpoint: spacesEndpoint,
});
class Storage {
constructor() {
this.init()
}
init () {
this._folder = this._folder ?? "test-uploads";
this._bucket = process.env.AWS_BUCKET_NAME + "/" + this._folder;
this.upload = multer({
storage: multerS3({
s3: s3,
bucket: this._bucket,
acl: "public-read",
key: function (request, file, cb) {
console.log(file);
cb(null, file.originalname);
},
}),
}).array("upload", 1)
}
set folder(folder) {
this._folder = folder;
this.init()
}
}
module.exports = Storage;