-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (36 loc) · 1.1 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
var Q = require('q');
var aws = require('aws-sdk');
var S3 = new aws.S3();
module.exports = function(options) {
var def = Q.defer();
if (!options) {
def.reject(new Error("expected options object with Bucket and Prefix. No options object"));
} else if (!options.Bucket) {
def.reject(new Error("expected options object with Bucket and Prefix. No options.Bucket"));
} else if (!options.Prefix) {
def.reject(new Error("expected options object with Bucket and Prefix. No options.Bucket"));
} else {
S3.listObjects({
Bucket: options.Bucket,
Prefix: options.Prefix
}, function(err, data) {
if (err) {
def.reject(err);
} else {
var keys = data.Contents.map(function(object) {
if (options.pattern) {
if (options.pattern.test(object.Key)) {
//TODO: pull in lambduh-validate api?
return object.Key;
}
} else {
return object.Key;
}
});
keys = keys.filter(function(v) { return v; });
def.resolve(keys);
}
});
}
return def.promise;
}