-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
227 lines (206 loc) · 6.13 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
'use strict';
const AWS = require('aws-sdk');
const fs = require('fs');
const path = require('path');
const isGlob = require('is-glob');
const globToRegExp = require('glob-to-regexp');
const Executor = require('@runnerty/module-core').Executor;
class s3Executor extends Executor {
constructor(process) {
super(process);
}
exec(params) {
if (!params.bucket) {
const endOptions = {
end: 'error',
messageLog: 'S3 bucket is not defined.',
err_output: 'S3 bucket is not defined.'
};
this.end(endOptions);
} else {
const awsS3Config = {
apiVersion: params.apiVersion,
accessKeyId: params.accessKeyId,
secretAccessKey: params.secretAccessKey,
bucket: params.bucket,
method: params.method,
region: params.region
};
const s3 = new AWS.S3(awsS3Config);
switch (params.method) {
case 'upload':
this.upload(s3, params);
break;
case 'delete':
this.delete(s3, params);
break;
case 'download':
this.download(s3, params);
break;
default:
const endOptions = {
end: 'error',
messageLog: `S3 method not accepted: ${awsS3Config.method}`,
err_output: `S3 method not accepted: ${awsS3Config.method}`
};
this.end(endOptions);
}
}
}
upload(s3, params) {
// call S3 to retrieve upload file to specified bucket
const fileStream = fs.createReadStream(params.local_file);
fileStream.on('error', err => {
this.logger.log('error', 'S3 upload error reading local file', params.local_file, err);
});
const uploadParams = {
Bucket: params.bucket,
Key: params.remote_file,
Body: fileStream,
ACL: params?.ACL || undefined,
ContentType: params?.ContentType || undefined
};
s3.upload(uploadParams, (err, data) => {
if (err) {
const endOptions = {
end: 'error',
messageLog: `S3 upload file Error: ${err}`,
err_output: `S3 upload file Error: ${err}`
};
this.end(endOptions);
} else {
const endOptions = {
end: 'end',
data_output: data
};
this.end(endOptions);
}
});
}
delete(S3, params) {
let dirName = '';
// Check Glob, if exists set dirName
if (isGlob(params.remote_path)) {
dirName = path.dirname(params.remote_path);
if (isGlob(dirName)) {
const endOptions = {
end: 'error',
messageLog: 'Glob only applicable to filenames.',
err_output: 'Glob only applicable to filenames.'
};
this.end(endOptions);
} else {
// Get files from dirName:
const listParams = { Bucket: params.bucket, Prefix: dirName ? dirName : params.remote_path };
S3.listObjects(listParams, (err, data) => {
if (err) {
const endOptions = {
end: 'error',
messageLog: `S3 delete Error: ${err}`,
err_output: `S3 delete Error: ${err}`
};
this.end(endOptions);
} else {
const patternGlob = path.basename(params.remote_path);
const valRegExp = globToRegExp(patternGlob);
const matchFiles = [];
data.Contents.map(file => {
if (valRegExp.test(file.Key)) {
matchFiles.push(file.Key);
}
});
this.deleteS3Async(S3, params.bucket, matchFiles)
.then(res => {
const endOptions = {
end: 'end',
data_output: res
};
this.end(endOptions);
})
.catch(err => {
const endOptions = {
end: 'error',
messageLog: `S3 delete Error: ${err}`,
err_output: `S3 delete Error: ${err}`
};
this.end(endOptions);
});
}
});
}
} else {
// Delete one file no glob
this.deleteS3Async(S3, params.bucket, params.remote_path)
.then(res => {
const endOptions = {
end: 'end',
data_output: res
};
this.end(endOptions);
})
.catch(err => {
const endOptions = {
end: 'error',
messageLog: `S3 delete Error: ${err}`,
err_output: `S3 delete Error: ${err}`
};
this.end(endOptions);
});
}
}
deleteS3Async(s3, bucket, files) {
return new Promise((resolve, reject) => {
if (files.constructor !== Array) {
files = [files];
}
const filesObj = [];
files.map(file => {
filesObj.push({ Key: file });
});
if (filesObj.length) {
const deleteParams = { Bucket: bucket, Delete: { Objects: filesObj } };
s3.deleteObjects(deleteParams, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
} else {
resolve();
}
});
}
download(s3, params) {
const fileStream = fs.createWriteStream(params.local_file);
const s3Stream = s3.getObject({ Bucket: params.bucket, Key: params.remote_file }).createReadStream();
// Listen for errors returned by the service
s3Stream.on('error', err => {
// NoSuchKey: The specified key does not exist
const endOptions = {
end: 'error',
messageLog: `S3 download file Error: ${err}`,
err_output: `S3 download file Error: ${err}`
};
this.end(endOptions);
});
s3Stream
.pipe(fileStream)
.on('error', err => {
// capture any errors that occur when writing data to the file
const endOptions = {
end: 'error',
messageLog: `S3 download file Error: ${err}`,
err_output: `S3 download file Error: ${err}`
};
this.end(endOptions);
})
.on('close', () => {
const endOptions = {
end: 'end'
};
this.end(endOptions);
});
}
}
module.exports = s3Executor;