-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlistObjectsByReplicationStatus.js
More file actions
240 lines (222 loc) · 7.86 KB
/
listObjectsByReplicationStatus.js
File metadata and controls
240 lines (222 loc) · 7.86 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
const async = require('async');
const { S3Client, ListObjectVersionsCommand, HeadObjectCommand } = require('@aws-sdk/client-s3');
const { NodeHttpHandler } = require('@aws-sdk/node-http-handler');
const http = require('http');
const { Logger } = require('werelogs');
const LISTING_LIMIT = 1000;
const VALID_REPLICATION_STATUSES = ['NEW', 'PENDING', 'COMPLETED', 'FAILED', 'REPLICA'];
// Error messages
const ERR_NO_BUCKETS = 'No buckets given as input! Please provide a comma-separated list of buckets';
const ERR_NO_ENDPOINT = 'ENDPOINT not defined!';
const ERR_NO_ACCESS_KEY = 'ACCESS_KEY not defined';
const ERR_NO_SECRET_KEY = 'SECRET_KEY not defined';
const ERR_REPLICATION_STATUS_NOT_DEFINED = `REPLICATION_STATUS not defined! Please provide a comma-separated list of replication statuses: ${VALID_REPLICATION_STATUSES.join(',')}.`;
const ERR_INVALID_REPLICATION_STATUS = `invalid REPLICATION_STATUS: must be a comma-separated list of replication statuses: ${VALID_REPLICATION_STATUSES.join(',')}.`;
/**
* List object versions from a bucket
* @private
*/
function _listObjectVersions(s3, bucket, VersionIdMarker, KeyMarker, cb) {
s3.send(new ListObjectVersionsCommand({
Bucket: bucket,
MaxKeys: LISTING_LIMIT,
VersionIdMarker,
KeyMarker,
})).then(data => cb(null, data)).catch(cb);
}
/**
* Extract keys and version IDs from version list
* @private
*/
function _getKeys(list) {
return list.map(v => ({
Key: v.Key,
VersionId: v.VersionId,
IsLatest: v.IsLatest,
}));
}
/**
* List objects in a bucket by replication status
* @private
*/
function _listBucket(s3, log, replicationStatusToProcess, bucket, cb) {
const bucketName = bucket.trim();
let VersionIdMarker = null;
let KeyMarker = null;
log.info('listing objects by replication status from bucket', {
bucket,
replicationStatus: replicationStatusToProcess.join(',')
});
async.doWhilst(
done => _listObjectVersions(
s3,
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, IsLatest } = k;
s3.send(new HeadObjectCommand({
Bucket: bucketName,
Key,
VersionId,
})).then(res => {
if (replicationStatusToProcess.includes(res.ReplicationStatus)) {
log.info('object with matching replication status found', {
Key,
VersionId,
IsLatest,
ReplicationStatus: res.ReplicationStatus,
...res,
bucketName
});
}
return next();
}).catch(err => {
log.error('error getting object metadata', {
bucketName,
Key,
VersionId,
IsLatest,
error: err
});
return next();
});
}, err => {
if (err) {
log.error('error processing batch of objects', {
error: err,
bucketName
});
return done(err);
}
VersionIdMarker = data.NextVersionIdMarker;
KeyMarker = data.NextKeyMarker;
return done();
});
},
),
async () => {
if (!VersionIdMarker || !KeyMarker) {
log.debug(
'completed listing objects by replication status for bucket',
{ bucket },
);
return false;
}
return true;
},
cb,
);
}
/**
* Main function to list objects by replication status
* @param {Object} options - Configuration options
* @param {string} options.buckets - Comma-separated list of buckets
* @param {string} options.accessKey - AWS access key
* @param {string} options.secretKey - AWS secret key
* @param {string} options.endpoint - S3 endpoint
* @param {string} options.replicationStatus - Comma-separated replication statuses (required)
* @param {Object} [options.logger] - Logger instance
* @returns {Promise<void>}
*/
function listObjectsByReplicationStatus(options) {
const {
buckets,
accessKey,
secretKey,
endpoint,
replicationStatus,
logger,
} = options;
const log = logger || new Logger('s3utils:listObjectsByReplicationStatus');
// Validate inputs
if (!buckets || buckets.trim().length === 0) {
return Promise.reject(new Error(ERR_NO_BUCKETS));
}
if (!endpoint) {
return Promise.reject(new Error(ERR_NO_ENDPOINT));
}
if (!accessKey) {
return Promise.reject(new Error(ERR_NO_ACCESS_KEY));
}
if (!secretKey) {
return Promise.reject(new Error(ERR_NO_SECRET_KEY));
}
if (!replicationStatus) {
return Promise.reject(new Error(ERR_REPLICATION_STATUS_NOT_DEFINED));
}
const bucketList = buckets.split(',');
const replicationStatusToProcess = replicationStatus.split(',');
// Validate replication statuses
for (const state of replicationStatusToProcess) {
if (!VALID_REPLICATION_STATUSES.includes(state)) {
return Promise.reject(new Error(ERR_INVALID_REPLICATION_STATUS));
}
}
log.info('Objects with replication status '
+ `${replicationStatusToProcess.join(' or ')} will be listed`);
const s3 = new S3Client({
region: 'us-east-1',
credentials: {
accessKeyId: accessKey,
secretAccessKey: secretKey,
},
endpoint,
forcePathStyle: true,
tls: false,
requestHandler: new NodeHttpHandler({
httpAgent: new http.Agent({ keepAlive: true }),
requestTimeout: 60000,
}),
});
return new Promise((resolve, reject) => {
async.mapSeries(
bucketList,
(bucket, done) => _listBucket(s3, log, replicationStatusToProcess, bucket, done),
err => {
if (err) {
return reject(err);
}
// Cleanup S3 client
if (s3 && typeof s3.destroy === 'function') {
s3.destroy();
}
return resolve();
},
);
});
}
module.exports = {
listObjectsByReplicationStatus,
ERR_NO_BUCKETS,
ERR_NO_ENDPOINT,
ERR_NO_ACCESS_KEY,
ERR_NO_SECRET_KEY,
ERR_REPLICATION_STATUS_NOT_DEFINED,
ERR_INVALID_REPLICATION_STATUS,
};
if (require.main === module) {
const log = new Logger('s3utils:listObjectsByReplicationStatus');
const BUCKETS = process.argv[2] || null;
const { ACCESS_KEY, SECRET_KEY, ENDPOINT, REPLICATION_STATUS } = process.env;
listObjectsByReplicationStatus({
buckets: BUCKETS,
accessKey: ACCESS_KEY,
secretKey: SECRET_KEY,
endpoint: ENDPOINT,
replicationStatus: REPLICATION_STATUS,
logger: log,
}).then(() => {
log.info('Completed successfully');
process.exit(0);
}).catch(err => {
log.error('Failed with error', { error: err.message });
process.exit(1);
});
}