-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (54 loc) · 2.26 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
const aws = require("aws-sdk");
/**
* @static
* @default
* @async
* @public
* @throws awsError
* @param {{aws:{accessKeyId:String,secretAccessKey:String},buckets:[{bucket:String,objects:[{Key:String}]}]}} args
* @param {boolean} meta retain S3 Object's meta data like LastModified ,ContentLength
* @param {boolean} parse pre-parse byte array to string or json
**/
module.exports = async (args, meta = false, parse = true) => {
let s3_data = {};
const s3 = new aws.S3({...args.aws});
let promises = [];
args.buckets.forEach((Bucket) => {
s3_data[Bucket.bucket] = [];
Bucket.objects.forEach((object) => {
promises.push(new Promise((resolve, reject) => {
s3.getObject({Bucket: Bucket.bucket, ...object}, (err, data) => {
if (err) {
console.error(`⚠️ Error fetching S3 Object ${object.Key} from ${Bucket.bucket}`);
reject(err);
} else {
console.log(`✔ Successfully fetched S3 Object ${object.Key} from ${Bucket.bucket}`);
s3_data[Bucket.bucket].push(
parse ?
meta ? (() => {
data.Body = data.ContentType === "application/json" ?
JSON.parse(data.Body.toString())
: data.Body.toString();
return data;
})()
: (() => {
return data.ContentType === "application/json" ?
JSON.parse(data.Body.toString())
: data.Body.toString();
})()
: meta ? data
: data.Body
)
resolve();
}
});
}));
});
});
let AllPromised = Promise.all(promises);
return new Promise(((resolve, reject) => {
AllPromised.then((value => {
resolve(s3_data)
}))
}));
};