Skip to content

Commit 8a83a0e

Browse files
Started migration for buckerVersionStats
Issue : S3UTILS-200
1 parent e944782 commit 8a83a0e

File tree

2 files changed

+40
-43
lines changed

2 files changed

+40
-43
lines changed

bucketVersionsStats.js

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const fs = require('fs');
22
const { http, https } = require('httpagent');
33

4-
const AWS = require('aws-sdk');
4+
const { S3Client } = require('@aws-sdk/client-s3');
5+
const { NodeHttpHandler } = require('@aws-sdk/node-http-handler');
6+
const { ConfiguredRetryStrategy } = require('@smithy/util-retry');
57
const { doWhilst } = require('async');
68

79
const { Logger } = require('werelogs');
@@ -97,38 +99,32 @@ if (s3EndpointIsHttps) {
9799
agent = new http.Agent({ keepAlive: true });
98100
}
99101

100-
const options = {
101-
accessKeyId: ACCESS_KEY,
102-
secretAccessKey: SECRET_KEY,
102+
const s3 = new S3Client({
103+
credentials: {
104+
accessKeyId: ACCESS_KEY,
105+
secretAccessKey: SECRET_KEY,
106+
},
103107
endpoint: ENDPOINT,
104108
region: 'us-east-1',
105-
sslEnabled: s3EndpointIsHttps,
106-
s3ForcePathStyle: true,
107-
apiVersions: { s3: '2006-03-01' },
108-
signatureVersion: 'v4',
109-
signatureCache: false,
110-
httpOptions: {
111-
timeout: 0,
112-
agent,
113-
},
114-
};
115-
/**
116-
* Options specific to s3 requests
117-
* `maxRetries` & `customBackoff` are set only to s3 requests
118-
* default aws sdk retry count is 3 with an exponential delay of 2^n * 30 ms
119-
*/
120-
const s3Options = {
121-
maxRetries: AWS_SDK_REQUEST_RETRIES,
122-
customBackoff: (retryCount, error) => {
123-
log.error('aws sdk request error', { error, retryCount });
124-
// retry with exponential backoff delay capped at 1mn max
125-
// between retries, and a little added jitter
126-
return Math.min(AWS_SDK_REQUEST_INITIAL_DELAY_MS
127-
* 2 ** retryCount, 60000)
128-
* (0.9 + Math.random() * 0.2);
129-
},
130-
};
131-
const s3 = new AWS.S3(Object.assign(options, s3Options));
109+
forcePathStyle: true,
110+
tls: s3EndpointIsHttps,
111+
requestHandler: new NodeHttpHandler({
112+
httpAgent: agent,
113+
httpsAgent: agent,
114+
requestTimeout: 0,
115+
}),
116+
retryStrategy: new ConfiguredRetryStrategy(
117+
AWS_SDK_REQUEST_RETRIES,
118+
// eslint-disable-next-line arrow-body-style
119+
attempt => {
120+
// Custom backoff with exponential delay capped at 1mn max
121+
// between retries, and a little added jitter
122+
return Math.min(AWS_SDK_REQUEST_INITIAL_DELAY_MS
123+
* 2 ** attempt, 60000)
124+
* (0.9 + Math.random() * 0.2);
125+
}
126+
),
127+
});
132128

133129
const stats = {
134130
current: {

utils/safeList.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const xml2js = require('xml2js');
2+
const { ListObjectVersionsCommand } = require('@aws-sdk/client-s3');
23

34
const recoverableErrors = new Set([
45
'TimestampParserError',
@@ -261,23 +262,23 @@ function recoverListing(resp, cb) {
261262
*
262263
* Values that fail to parse will be returned as the raw string value present in the xml.
263264
*
264-
* @param {AWS.S3} s3 - S3 client instance
265+
* @param {S3Client} s3 - S3 client instance
265266
* @param {object} params - listObjectVersions params
266267
* @param {function} cb - callback
267268
* @returns {undefined}
268269
*/
269270
function safeListObjectVersions(s3, params, cb) {
270-
const req = s3.listObjectVersions(params);
271-
req.on('complete', resp => {
272-
if (resp.error) {
273-
if (recoverableErrors.has(resp.error.code)) {
274-
return recoverListing(resp, cb);
275-
}
276-
return cb(resp.error);
277-
}
278-
return cb(null, resp.data);
279-
});
280-
req.send();
271+
const command = new ListObjectVersionsCommand(params);
272+
273+
s3.send(command)
274+
.then(data => cb(null, data))
275+
.catch(error => {
276+
// AWS SDK v3 generally handles parsing better than v2,
277+
// so for now we just pass through errors. If we encounter
278+
// specific recoverable errors like TimestampParserError,
279+
// we can implement XML recovery later.
280+
return cb(error);
281+
});
281282
}
282283

283284
module.exports = {

0 commit comments

Comments
 (0)