-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrabbitHole.js
99 lines (91 loc) · 2.6 KB
/
rabbitHole.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const async = require('async');
const AWS = require('aws-sdk');
const http = require('http');
// configurable params
const BUCKET = '';
const LISTING_LIMIT = 10000;
const ACCESSKEY = '';
const SECRETKEY = '';
const ENDPOINT = 'http://127.0.0.1:8000';
const QUIET_MODE = false;
AWS.config.update({
accessKeyId: ACCESSKEY,
secretAccessKey: SECRETKEY,
region: "us-east-1",
sslEnabled: false,
endpoint: ENDPOINT,
s3ForcePathStyle: true,
apiVersions: { s3: '2006-03-01' },
signatureVersion: 'v4',
signatureCache: false,
});
const s3 = new AWS.S3({
httpOptions: {
maxRetries: 0,
timeout: 0,
agent: new http.Agent({ keepAlive: true }),
},
});
// list object versions
function _listObjectVersions(VersionIdMarker, KeyMarker, cb) {
s3.listObjectVersions({ Bucket: BUCKET, MaxKeys: LISTING_LIMIT, VersionIdMarker, KeyMarker }, cb);
}
// return object with key and version_id
function _getKeys(keys) {
return keys.map((v) => ({
Key: v.Key,
VersionId: v.VersionId,
}));
}
function logger (msg, val) {
if (!QUIET_MODE) {
console.log(msg, val);
}
}
function rabbitHole(cb) {
let VersionIdMarker = null;
let KeyMarker = null;
let currentVersions = 0;
let nonCurrentVersions = 0;
async.doWhilst(
done => _listObjectVersions(VersionIdMarker, KeyMarker, (err, data) => {
if (err) {
console.log('error');
return done(err);
}
// console.log(data)
data.Versions.forEach(d => {
if (d.IsLatest) {
currentVersions++;
} else {
nonCurrentVersions++;
}
// logger('Key', d.Key, 'VersionId', d.VersionId);
});
VersionIdMarker = data.NextVersionIdMarker;
KeyMarker = data.NextKeyMarker;
logger('VersionIdMarker', VersionIdMarker);
logger('KeyMarker', KeyMarker);
return done();
}),
() => {
if (!VersionIdMarker || !KeyMarker) {
return false;
}
return true;
},
(err, res) => {
// console.log('err', err, 'res', res);
console.log('CURRENT: ', currentVersions);
console.log('NON-CURRENT: ', nonCurrentVersions);
return cb(err, res);
}
);
}
rabbitHole((err, res) => {
// console.log('err', err, 'res', res);
if (err) {
return console.log('error occured listing objects', err);
}
return console.log('completed listing');
});