-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
171 lines (156 loc) · 4.67 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const exec = require('shelljs.exec');
const shell = require('shelljs');
const action = require('./lib/action');
function KilatS3() { }
KilatS3.makeBucket = function makeBucket(bucketName) {
return new Promise((resolve, reject) => {
const results = exec(`s3cmd mb s3://${bucketName}`);
if (results.code === 0) {
resolve(results.stdout);
} else {
reject(new Error(results.error));
}
});
};
KilatS3.removeBucket = function removeBucket(bucketName) {
return new Promise((resolve, reject) => {
const results = exec(`s3cmd rb s3://${bucketName}`);
if (results.code === 0) {
resolve(results.stdout);
} else {
reject(new Error(results.error));
}
});
};
KilatS3.listBuckets = function listBuckets() {
return new Promise((resolve, reject) => {
const results = exec('s3cmd ls', { silent: true });
if (results.code === 0) {
const echo = results.stdout.split('\n');
resolve(echo);
} else {
reject(new Error(results.error));
}
});
};
KilatS3.listAllObject = function listAllObject() {
return new Promise((resolve, reject) => {
const results = exec('s3cmd la');
if (results.code === 0) {
const echo = results.stdout.split('\n');
resolve(echo);
} else {
reject(new Error(results.error));
}
});
};
KilatS3.putObjectPrivate = function putObjectPrivate(pathFile, bucketName) {
return new Promise((resolve, reject) => {
action.checkBucketPrefixes(bucketName)
.then((finalBucketName) => {
const results = exec(`s3cmd put -P ${pathFile} s3://${finalBucketName} --acl-private`);
if (results.code === 0) {
action.getPublicUrl(results.stdout, finalBucketName)
.then((publicUrl) => {
resolve(publicUrl);
});
} else {
reject(new Error(results.error));
}
});
});
};
KilatS3.putObjectPublic = function putObjectPublic(pathFile, bucketName) {
return new Promise((resolve, reject) => {
action.checkBucketPrefixes(bucketName)
.then((finalBucketName) => {
const results = exec(`s3cmd put -P ${pathFile} s3://${finalBucketName} --acl-public`);
if (results.code === 0) {
action.getPublicUrl(results.stdout, finalBucketName)
.then((publicUrl) => {
resolve(publicUrl);
});
} else {
reject(new Error(results.error));
}
});
});
};
KilatS3.syncFolder = function syncFolder(bucketPath, localDirPath) {
return new Promise((resolve, reject) => {
shell.exec(`s3cmd sync --acl-public --no-mime-magic ${localDirPath} s3://${bucketPath}`, (code, output, err) => {
if (code === 0) {
resolve();
} else {
reject(new Error(err));
}
});
});
};
KilatS3.downloadObject = function downloadObject(bucketPath, localDirPath) {
return new Promise((resolve, reject) => {
const results = exec(`s3cmd get s3://${bucketPath} ${localDirPath}`);
if (results.code === 0) {
resolve(results.stdout);
} else {
reject(new Error(results.error));
}
});
};
KilatS3.removeObject = function removeObject(bucketPath) {
return new Promise((resolve, reject) => {
const results = exec(`s3cmd del s3://${bucketPath}`);
if (results.code === 0) {
resolve(results.stdout);
} else {
reject(new Error(results.error));
}
});
};
KilatS3.listObject = function listObject(bucketName) {
return new Promise((resolve, reject) => {
const results = exec(`s3cmd ls s3://${bucketName}`);
if (results.code === 0) {
const echo = results.stdout.split('\n');
resolve(echo);
} else {
reject(new Error(results.error));
}
});
};
KilatS3.existsObject = function existsObject(bucketName, fileName) {
return new Promise((resolve, reject) => {
const results = exec(`s3cmd ls s3://${bucketName}/${fileName} | wc -l`);
if (results.code === 0) {
const echo = results.stdout.split('\n');
const check = echo[0].replace(/\s/g, '');
if (check === '0') {
resolve(false);
} else {
resolve(true);
}
} else {
reject(new Error(results.error));
}
});
};
KilatS3.diskUsage = function diskUsage(bucketName = null) {
return new Promise((resolve, reject) => {
if (bucketName === null) {
const results = exec('s3cmd du');
if (results.code === 0) {
resolve(results.stdout);
} else {
reject(new Error(results.error));
}
} else {
const results = exec(`s3cmd du s3://${bucketName}`);
if (results.code === 0) {
resolve(results.stdout);
} else {
reject(new Error(results.error));
}
}
});
};
module.exports = KilatS3;