-
Notifications
You must be signed in to change notification settings - Fork 2
/
listFailedObjects.js
135 lines (126 loc) · 3.71 KB
/
listFailedObjects.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const async = require('async');
const AWS = require('aws-sdk');
const { http } = require('httpagent');
const { Logger } = require('werelogs');
const log = new Logger('s3utils:listFailedObjects');
/* eslint-disable no-console */
// configurable params
const BUCKETS = process.argv[2] ? process.argv[2].split(',') : null;
const { ACCESS_KEY } = process.env;
const { SECRET_KEY } = process.env;
const { ENDPOINT } = process.env;
const LISTING_LIMIT = 1000;
if (!BUCKETS || BUCKETS.length === 0) {
log.error('No buckets given as input! Please provide '
+ 'a comma-separated list of buckets');
process.exit(1);
}
if (!ENDPOINT) {
log.error('ENDPOINT not defined!');
process.exit(1);
}
if (!ACCESS_KEY) {
log.error('ACCESS_KEY not defined');
process.exit(1);
}
if (!SECRET_KEY) {
log.error('SECRET_KEY not defined');
process.exit(1);
}
const s3 = new AWS.S3({
accessKeyId: ACCESS_KEY,
secretAccessKey: SECRET_KEY,
region: 'us-east-1',
sslEnabled: false,
endpoint: ENDPOINT,
s3ForcePathStyle: true,
apiVersions: { s3: '2006-03-01' },
signatureVersion: 'v4',
signatureCache: false,
httpOptions: {
maxRetries: 0,
timeout: 0,
agent: new http.Agent({ keepAlive: true }),
},
});
// list object versions
function _listObjectVersions(bucket, VersionIdMarker, KeyMarker, cb) {
s3.listObjectVersions({
Bucket: bucket,
MaxKeys: LISTING_LIMIT,
VersionIdMarker,
KeyMarker,
}, cb);
}
// return object with key and version_id
function _getKeys(list) {
return list.map(v => ({
Key: v.Key,
VersionId: v.VersionId,
}));
}
function listBucket(bucket, cb) {
const bucketName = bucket.trim();
let VersionIdMarker = null;
let KeyMarker = null;
log.info('listing failed objects from bucket', { bucket });
async.doWhilst(
done => _listObjectVersions(
bucketName,
VersionIdMarker,
KeyMarker,
(err, data) => {
if (err) {
log.error('error occured while listing', { error: err, bucketName });
return done(err);
}
const keys = _getKeys(data.Versions);
return async.mapLimit(keys, 10, (k, next) => {
const { Key, VersionId } = k;
s3.headObject(
{ Bucket: bucketName, Key, VersionId },
(err, res) => {
if (err) {
return next(err);
}
if (res.ReplicationStatus === 'FAILED') {
console.log({ Key, ...res });
}
return next();
},
);
}, err => {
if (err) {
return done(err);
}
VersionIdMarker = data.NextVersionIdMarker;
KeyMarker = data.NextKeyMarker;
return done();
});
},
),
() => {
if (!VersionIdMarker || !KeyMarker) {
log.debug(
'completed listing failed objects for bucket',
{ bucket },
);
return false;
}
return true;
},
cb,
);
}
async.mapSeries(
BUCKETS,
(bucket, done) => listBucket(bucket, done),
err => {
if (err) {
log.error('error occured while listing failed objects', {
error: err,
});
}
},
);
/* eslint-enable no-console */