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' ) ;
24const http = require ( 'http' ) ;
5+ const https = require ( 'https' ) ;
36const BackbeatClient = require ( '../BackbeatClient' ) ;
47
58const AWS_SDK_REQUEST_DELAY_MS = 30 ;
69const AWS_SDK_REQUEST_RETRIES = 100 ;
7- const LOG_PROGRESS_INTERVAL_MS = 10000 ;
810
911/**
1012 * Sets up and configures AWS S3 and Backbeat clients.
@@ -27,7 +29,37 @@ function setupClients({
2729 secretKey,
2830 endpoint,
2931} , log ) {
30- const awsConfig = {
32+ const awsv3Config = {
33+ region : 'us-east-1' ,
34+ credentials : {
35+ accessKeyId : accessKey ,
36+ secretAccessKey : secretKey ,
37+ } ,
38+ endpoint,
39+ forcePathStyle : true ,
40+ requestHandler : new NodeHttpHandler ( {
41+ httpAgent : new http . Agent ( { keepAlive : true } ) ,
42+ httpsAgent : new https . Agent ( {
43+ keepAlive : true ,
44+ // With SDK v2, SSL was disabled. Here we added this
45+ // to be able to run the script locally on a lab for testing,
46+ // so https will work but there will be no security enforced
47+ rejectUnauthorized : false
48+ } ) ,
49+ requestTimeout : 60000 ,
50+ } ) ,
51+ retryStrategy : new ConfiguredRetryStrategy (
52+ AWS_SDK_REQUEST_RETRIES , // maxAttempts
53+ attempt => {
54+ log . error ( 'aws sdk request error' , { retryCount : attempt } ) ;
55+ // The delay is not truly exponential; it resets to the minimum after every 10 calls,
56+ // with a maximum delay of 15 seconds.
57+ return AWS_SDK_REQUEST_DELAY_MS * ( 2 ** ( attempt % 10 ) ) ;
58+ }
59+ )
60+ } ;
61+
62+ const awsv2Config = {
3163 accessKeyId : accessKey ,
3264 secretAccessKey : secretKey ,
3365 endpoint,
@@ -38,37 +70,15 @@ function setupClients({
3870 signatureVersion : 'v4' ,
3971 signatureCache : false ,
4072 httpOptions : {
41- timeout : 0 ,
73+ timeout : 60000 ,
4274 agent : new http . Agent ( { keepAlive : true } ) ,
4375 } ,
4476 } ;
4577
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 ,
78+ return {
79+ s3 : new S3Client ( awsv3Config ) ,
80+ bb : new BackbeatClient ( awsv2Config ) ,
6381 } ;
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 } ;
7282}
7383
7484module . exports = { setupClients } ;
0 commit comments