Skip to content

Commit e944782

Browse files
Migrate aws sdk from v2 to v3 for CRR
Issue: S3UTILS-199 update workflow for ssh to https refactor crr tests
1 parent d00cb07 commit e944782

File tree

6 files changed

+1292
-135
lines changed

6 files changed

+1292
-135
lines changed

CRR/ReplicationStatusUpdater.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ const {
22
doWhilst, eachSeries, eachLimit, waterfall, series,
33
} = require('async');
44
const { ObjectMD } = require('arsenal').models;
5-
65
const { setupClients } = require('./clients');
6+
const {
7+
ListObjectVersionsCommand,
8+
GetBucketReplicationCommand
9+
} = require('@aws-sdk/client-s3');
710

811
const LOG_PROGRESS_INTERVAL_MS = 10000;
912

@@ -26,6 +29,7 @@ class ReplicationStatusUpdater {
2629
* @param {number} [params.maxScanned] - (Optional) Maximum number of items to scan.
2730
* @param {string} [params.keyMarker] - (Optional) Key marker for resuming object listing.
2831
* @param {string} [params.versionIdMarker] - (Optional) Version ID marker for resuming object listing.
32+
* @param {boolean} [params.currentVersionOnly] - (Optional) Whether to process only the current version of objects.
2933
*/
3034
constructor(params, log) {
3135
const {
@@ -266,13 +270,15 @@ class ReplicationStatusUpdater {
266270
* @returns {void}
267271
*/
268272
_listObjectVersions(bucket, VersionIdMarker, KeyMarker, cb) {
269-
return this.s3.listObjectVersions({
273+
this.s3.send(new ListObjectVersionsCommand({
270274
Bucket: bucket,
271275
MaxKeys: this.listingLimit,
272276
Prefix: this.targetPrefix,
273277
VersionIdMarker,
274278
KeyMarker,
275-
}, cb);
279+
}))
280+
.then(data => cb(null, data))
281+
.catch(err => cb(err));
276282
}
277283

278284
/**
@@ -284,15 +290,17 @@ class ReplicationStatusUpdater {
284290
* @returns {void}
285291
*/
286292
_markPending(bucket, versions, cb) {
287-
const options = { Bucket: bucket };
288293
waterfall([
289-
next => this.s3.getBucketReplication(options, (err, res) => {
290-
if (err) {
291-
this.log.error('error getting bucket replication', { error: err });
292-
return next(err);
293-
}
294-
return next(null, res.ReplicationConfiguration);
295-
}),
294+
next => {
295+
this.s3.send(new GetBucketReplicationCommand(
296+
{ Bucket: bucket },
297+
))
298+
.then(res => next(null, res.ReplicationConfiguration))
299+
.catch(err => {
300+
this.log.error('error getting bucket replication', { error: err });
301+
next(err);
302+
});
303+
},
296304
(repConfig, next) => {
297305
const { Rules } = repConfig;
298306
const storageClass = this.siteName || Rules[0].Destination.StorageClass;

CRR/clients.js

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
const AWS = require('aws-sdk');
1+
const { S3Client } = require('@aws-sdk/client-s3');
2+
const { NodeHttpHandler } = require('@aws-sdk/node-http-handler');
3+
const { ConfiguredRetryStrategy } = require('@smithy/util-retry');
24
const http = require('http');
35
const BackbeatClient = require('../BackbeatClient');
46

57
const AWS_SDK_REQUEST_DELAY_MS = 30;
68
const AWS_SDK_REQUEST_RETRIES = 100;
7-
const LOG_PROGRESS_INTERVAL_MS = 10000;
89

910
/**
1011
* Sets up and configures AWS S3 and Backbeat clients.
@@ -27,7 +28,30 @@ function setupClients({
2728
secretKey,
2829
endpoint,
2930
}, log) {
30-
const awsConfig = {
31+
const awsv3Config = {
32+
region: 'us-east-1',
33+
credentials: {
34+
accessKeyId: accessKey,
35+
secretAccessKey: secretKey,
36+
},
37+
endpoint,
38+
forcePathStyle: true,
39+
requestHandler: new NodeHttpHandler({
40+
httpAgent: new http.Agent({ keepAlive: true }),
41+
requestTimeout: 0,
42+
}),
43+
retryStrategy: new ConfiguredRetryStrategy(
44+
AWS_SDK_REQUEST_RETRIES, // maxAttempts
45+
attempt => {
46+
log.error('aws sdk request error', { retryCount: attempt });
47+
// The delay is not truly exponential; it resets to the minimum after every 10 calls,
48+
// with a maximum delay of 15 seconds.
49+
return AWS_SDK_REQUEST_DELAY_MS * (2 ** (attempt % 10));
50+
}
51+
)
52+
};
53+
54+
const awsv2Config = {
3155
accessKeyId: accessKey,
3256
secretAccessKey: secretKey,
3357
endpoint,
@@ -43,32 +67,10 @@ function setupClients({
4367
},
4468
};
4569

46-
/**
47-
* Custom backoff strategy for AWS SDK requests.
48-
* @param {number} retryCount - The current retry attempt.
49-
* @param {Error} error - The error that caused the retry.
50-
* @returns {number} The delay in milliseconds before the next retry.
51-
*/
52-
function customBackoffStrategy(retryCount, error) {
53-
this.log.error('aws sdk request error', { error, retryCount });
54-
// The delay is not truly exponential; it resets to the minimum after every 10 calls,
55-
// with a maximum delay of 15 seconds.
56-
return AWS_SDK_REQUEST_DELAY_MS * (2 ** (retryCount % 10));
57-
}
58-
59-
// Specific options for S3 requests
60-
const s3SpecificOptions = {
61-
maxRetries: AWS_SDK_REQUEST_RETRIES,
62-
customBackoff: customBackoffStrategy,
70+
return {
71+
s3: new S3Client(awsv3Config),
72+
bb: new BackbeatClient(awsv2Config),
6373
};
64-
65-
// Create an S3 client instance
66-
const s3 = new AWS.S3({ ...awsConfig, ...s3SpecificOptions });
67-
68-
// Create a BackbeatClient instance
69-
const bb = new BackbeatClient(awsConfig);
70-
71-
return { s3, bb };
7274
}
7375

7476
module.exports = { setupClients };

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
},
2727
"homepage": "https://github.com/scality/s3utils#readme",
2828
"dependencies": {
29+
"@aws-sdk/client-s3": "^3.873.0",
30+
"@aws-sdk/node-http-handler": "^3.374.0",
2931
"@senx/warp10": "^2.0.3",
32+
"@smithy/util-retry": "^4.0.7",
3033
"JSONStream": "^1.3.5",
3134
"arsenal": "git+https://github.com/scality/arsenal#8.2.26",
3235
"async": "^3.2.6",

0 commit comments

Comments
 (0)