-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3list.js
62 lines (54 loc) · 1.58 KB
/
s3list.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
"use strict";
var program = require("commander");
var AWS = require("aws-sdk");
var config = require("./config.json");
program
.version("0.0.1")
.description("List the AWS S3 bucket's items with the specified prefix.")
.option("--bucket [bucket]", "Optional S3 bucket name.")
.option("--prefix [prefix]", "Optional S3 item key prefix.")
.option("--b", "Bare: item key only, without index.")
.option("--al", "All object information in JSON.")
.parse(process.argv);
AWS.config.update({region: config.REGION});
var s3 = new AWS.S3();
var params = {
Bucket: program.bucket ? program.bucket : config.REPORTS_BUCKET,
Prefix: program.prefix ? program.prefix : null
};
var s3DataContents = []; // Single array of all combined S3 data.Contents
function s3Print() {
if (program.al) {
// --al: Print all objects
console.log(JSON.stringify(s3DataContents, null, " "));
} else {
// --b: Print key only, otherwise also print index
var i;
for (i = 0; i < s3DataContents.length; i++) {
var head = !program.b ? (i+1) + ": " : "";
console.log(head + s3DataContents[i].Key);
}
}
}
function s3ListObjects(params, cb) {
s3.listObjects(params, function(err, data) {
if (err) {
console.log("listS3Objects Error:", err);
} else {
var contents = data.Contents;
s3DataContents = s3DataContents.concat(contents);
if (data.IsTruncated) {
// Set Marker to last returned key
params.Marker = contents[contents.length-1].Key;
s3ListObjects(params, cb);
} else {
cb();
}
}
});
}
try {
s3ListObjects(params, s3Print);
} catch(e) {
console.log(e + "\n" + e.stack);
}