-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
63 lines (55 loc) · 1.76 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
var AWS = require('aws-sdk');
function listAllObjects(options, callback) {
var s3 = new AWS.S3(options.s3options || {});
if (!options) {
callback(new TypeError('Missing options parameter.'));
return;
}
if (!options.progress && !callback) {
callback(new TypeError('Either callback or options.progress must be a function.'));
return;
}
if (!options.bucket) {
callback(new TypeError('Missing options.bucket parameter.'));
return;
}
var params = { Bucket: options.bucket };
if (options.prefix) {
params.Prefix = options.prefix;
}
var allFiles = [];
var batchNr = 0;
function load(){
if (allFiles.length > 0){
params.Marker = allFiles[allFiles.length-1].Key;
}
s3.listObjects(params, function(err, data) {
if (err) {
if (options.progress) {
options.progress(err, null);
}
if (callback) {
return callback(err);
}
return;
}
allFiles.push.apply(allFiles, data.Contents);
batchNr++;
if (data.IsTruncated) {
if (options.progress) {
options.progress(undefined, { batchNr : batchNr, finalBatch : false, data : data.Contents });
}
load();
} else {
if (options.progress) {
options.progress(undefined, { batchNr : batchNr, finalBatch : true, data : data.Contents });
}
if (callback) {
callback(undefined, allFiles);
}
}
});
}
load();
}
module.exports = listAllObjects;