-
Notifications
You must be signed in to change notification settings - Fork 47
/
s3ListKeys.js
74 lines (62 loc) · 1.64 KB
/
s3ListKeys.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
var _ = require('lodash');
function listKeys(client, options, callback) {
var keys = [];
function listKeysRecusively(marker) {
options.marker = marker;
listKeyPage(
client,
options,
function (error, nextMarker, keyset) {
if (error) {
return callback(error, keys);
}
keys = keys.concat(keyset);
if (nextMarker) {
listKeysRecusively(nextMarker);
} else {
callback(null, keys);
}
}
);
}
listKeysRecusively();
}
function listKeyPage(client, options, callback) {
var params = {
Bucket : options.bucket,
Delimiter: options.delimiter,
Marker : options.marker,
MaxKeys : options.maxKeys || 1000,
Prefix : options.prefix
};
client.listObjects(params, function (error, response) {
if (error) {
return callback(error);
} else if (response.err) {
return callback(new Error(response.err));
}
var keys;
if (options.delimiter) {
keys = _.map(response.CommonPrefixes, function (item) {
return item.Prefix;
});
} else {
keys = _.map(response.Contents, function (item) {
return item;
});
}
var nextMarker;
if (response.IsTruncated) {
if (options.delimiter) {
// If specifying a delimiter, the response.NextMarker field exists.
nextMarker = response.NextMarker;
} else {
// For normal listing, there is no response.NextMarker
// and we must use the last key instead.
nextMarker = keys[keys.length - 1].Key;
}
}
callback(null, nextMarker, keys);
});
}
module.exports = listKeys;