Skip to content

Commit d0ba580

Browse files
committed
S3UTILS-211: list replication status is now configurable
1 parent f2842fd commit d0ba580

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

listObjectsByReplicationStatus.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ const http = require('http');
55

66
const { Logger } = require('werelogs');
77

8-
const log = new Logger('s3utils:listFailedObjects');
8+
const log = new Logger('s3utils:listObjectsByReplicationStatus');
99

1010
// configurable params
1111
const BUCKETS = process.argv[2] ? process.argv[2].split(',') : null;
1212
const { ACCESS_KEY } = process.env;
1313
const { SECRET_KEY } = process.env;
1414
const { ENDPOINT } = process.env;
1515
const LISTING_LIMIT = 1000;
16+
let { REPLICATION_STATUS } = process.env;
1617

1718
if (!BUCKETS || BUCKETS.length === 0) {
1819
log.error('No buckets given as input! Please provide '
@@ -31,6 +32,21 @@ if (!SECRET_KEY) {
3132
log.error('SECRET_KEY not defined');
3233
process.exit(1);
3334
}
35+
if (!REPLICATION_STATUS) {
36+
REPLICATION_STATUS = 'FAILED';
37+
}
38+
39+
const replicationStatusToProcess = REPLICATION_STATUS.split(',');
40+
replicationStatusToProcess.forEach(state => {
41+
if (!['NEW', 'PENDING', 'COMPLETED', 'FAILED', 'REPLICA'].includes(state)) {
42+
log.error('invalid REPLICATION_STATUS environment: must be a '
43+
+ 'comma-separated list of replication statuses to list, '
44+
+ 'as NEW,PENDING,COMPLETED,FAILED,REPLICA.');
45+
process.exit(1);
46+
}
47+
});
48+
log.info('Objects with replication status '
49+
+ `${replicationStatusToProcess.join(' or ')} will be listed`);
3450

3551
const s3 = new S3Client({
3652
region: 'us-east-1',
@@ -69,7 +85,10 @@ function listBucket(bucket, cb) {
6985
const bucketName = bucket.trim();
7086
let VersionIdMarker = null;
7187
let KeyMarker = null;
72-
log.info('listing failed objects from bucket', { bucket });
88+
log.info('listing objects by replication status from bucket', {
89+
bucket,
90+
replicationStatus: replicationStatusToProcess.join(',')
91+
});
7392
async.doWhilst(
7493
done => _listObjectVersions(
7594
bucketName,
@@ -88,8 +107,12 @@ function listBucket(bucket, cb) {
88107
Key,
89108
VersionId,
90109
})).then(res => {
91-
if (res.ReplicationStatus === 'FAILED') {
92-
log.info('failed replication object found', { Key, ...res });
110+
if (replicationStatusToProcess.includes(res.ReplicationStatus)) {
111+
log.info('object with matching replication status found', {
112+
Key,
113+
ReplicationStatus: res.ReplicationStatus,
114+
...res
115+
});
93116
}
94117
return next();
95118
}).catch(next);
@@ -106,7 +129,7 @@ function listBucket(bucket, cb) {
106129
async () => {
107130
if (!VersionIdMarker || !KeyMarker) {
108131
log.debug(
109-
'completed listing failed objects for bucket',
132+
'completed listing objects by replication status for bucket',
110133
{ bucket },
111134
);
112135
return false;
@@ -122,7 +145,7 @@ async.mapSeries(
122145
(bucket, done) => listBucket(bucket, done),
123146
err => {
124147
if (err) {
125-
log.error('error occured while listing failed objects', {
148+
log.error('error occured while listing objects by replication status', {
126149
error: err,
127150
});
128151
}

0 commit comments

Comments
 (0)