-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
101 lines (93 loc) · 2.85 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const aws = require('aws-sdk');
const fs = require(`fs`);
class Storage {
/**
*Creates an instance of Storage.
* @param {Class} app The Instance of the App that is running
* @param {Object} [cdnOpts={}] The auth.cdnOptions Object with all configured options included.
* @memberof Storage
*/
constructor(cdnOpts = {}) {
this.opts = cdnOpts;
this.credentials = new aws.Credentials(this.opts.s3Options.accessKeyId, this.opts.s3Options.secretAccessKey);
this.client = new aws.S3({ credentials: this.credentials, endpoint: this.opts.s3Options.endpoint });
this.debug = cdnOpts.debug ? true : false;
if(this.debug) console.log(`S3 => Client Initialized, Connected to ${this.opts.s3Options.endpoint}/${this.opts.bucketName}`);
}
/**
* Uploads a file to the s3 bucket
* @param {String} localFile Path to local file
* @param {String} key Path on S3 that it should be uploaded to
*/
uploadToS3(key, item) {
return new Promise((resolve, reject) => {
var timeStart = Date.now();
let params = {
Bucket: this.opts.bucketName,
ACL: "public-read",
Key: key,
Body: item.Body,
ContentType: item.ContentType
};
let uploader = this.client.putObject(params, (err, data) => {
if (err) return resolve(console.error(err));
if(this.debug) console.log(`B2 => Uploaded file ${key} to CDN in ${Date.now() - timeStart}ms`);
resolve(key);
});
});
}
/**
* Removes any given file from S3
* @param {String} key The file name on S3
*/
deleteFromS3(key) {
return new Promise((resolve, reject) => {
let timeStart = Date.now();
let params = {
Bucket: this.opts.bucketName,
Delete: {
Objects: [
{ Key: key }
]
},
quiet: false
};
let remove = this.client.deleteObjects(params);
remove.on(`end`, () => {
if(this.debug) console.log(`CDN => Removed file ${key} from CDN`);
resolve();
});
});
}
getObject(key) {
return new Promise((resolve, reject) => {
let params = {
Bucket: this.opts.bucketName,
Key: key
};
let obj = this.client.getObject(params, (err, data) => {
if (err) resolve(false);
resolve(data);
});
});
}
listObjects() {
return new Promise((resolve, reject) => {
let items = this.client.listObjectsV2({ Bucket: this.opts.bucketName, MaxKeys: 100 }, (err, data) => {
if (err) return console.error(err);
return resolve(data.Contents);
});
});
}
/**
* Remove any given file from the Local system
* @param {String} localFile The path to the local File
*/
deleteLocalFile(localFile) {
fs.unlink(localFile, (err) => {
if (err) return console.error(err);
return true;
});
}
};
module.exports = Storage;