14
14
import java .util .function .Supplier ;
15
15
import org .apache .logging .log4j .LogManager ;
16
16
import org .apache .logging .log4j .Logger ;
17
+ import software .amazon .awssdk .auth .credentials .AwsBasicCredentials ;
18
+ import software .amazon .awssdk .auth .credentials .AwsCredentials ;
19
+ import software .amazon .awssdk .regions .Region ;
20
+ import software .amazon .awssdk .services .s3 .S3AsyncClient ;
17
21
18
22
// S3Client provides a wrapper to the regular AmazonS3 object. The goal is to ensure that the
19
23
// AmazonS3 object is
@@ -33,8 +37,10 @@ public class S3Client {
33
37
private final S3Config s3Config ;
34
38
35
39
private volatile AmazonS3 s3Client ;
40
+ private volatile S3AsyncClient asyncClient ;
36
41
private volatile AtomicInteger referenceCounter ;
37
42
private volatile AWSCredentials awsCredentials ;
43
+ private volatile AwsCredentials v2Credentials ;
38
44
39
45
public S3Client (S3Config s3Config ) throws ModelDBException {
40
46
this .s3Config = s3Config ;
@@ -89,7 +95,7 @@ private AmazonS3 buildEnvironmentClient(Regions awsRegion) {
89
95
*/
90
96
private void scheduleRefresh (Supplier <AmazonS3 > s3 ) {
91
97
CommonUtils .scheduleTask (
92
- () -> refreshS3Client (awsCredentials , s3 .get ()),
98
+ () -> refreshS3Client (awsCredentials , s3 .get (), v2Credentials ),
93
99
0L ,
94
100
s3Config .getRefreshIntervalSeconds (),
95
101
TimeUnit .SECONDS );
@@ -98,6 +104,7 @@ private void scheduleRefresh(Supplier<AmazonS3> s3) {
98
104
private void initializeMinioClient (
99
105
String cloudAccessKey , String cloudSecretKey , Regions awsRegion , String minioEndpoint ) {
100
106
this .awsCredentials = new BasicAWSCredentials (cloudAccessKey , cloudSecretKey );
107
+ this .v2Credentials = AwsBasicCredentials .create (cloudAccessKey , cloudSecretKey );
101
108
var clientConfiguration = new ClientConfiguration (defaultClientConfig );
102
109
clientConfiguration .setSignerOverride ("VertaSignOverrideS3Signer" );
103
110
SignerFactory .registerSigner ("VertaSignOverrideS3Signer" , SignOverrideS3Signer .class );
@@ -120,6 +127,7 @@ private AmazonS3 buildMinioClient(
120
127
private void initializeS3ClientWithAccessKey (
121
128
String cloudAccessKey , String cloudSecretKey , Regions awsRegion ) {
122
129
this .awsCredentials = new BasicAWSCredentials (cloudAccessKey , cloudSecretKey );
130
+ this .v2Credentials = AwsBasicCredentials .create (cloudAccessKey , cloudSecretKey );
123
131
this .s3Client = buildAccessKeyClient (awsRegion );
124
132
scheduleRefresh (() -> buildAccessKeyClient (awsRegion ));
125
133
}
@@ -133,7 +141,7 @@ private AmazonS3 buildAccessKeyClient(Regions awsRegion) {
133
141
}
134
142
135
143
public RefCountedS3Client getRefCountedClient () {
136
- return new RefCountedS3Client (awsCredentials , s3Client , referenceCounter );
144
+ return new RefCountedS3Client (awsCredentials , s3Client , asyncClient , referenceCounter );
137
145
}
138
146
139
147
private void initializeWithWebIdentity (Regions awsRegion ) {
@@ -152,7 +160,13 @@ private void initializeWithWebIdentity(Regions awsRegion) {
152
160
TimeUnit .SECONDS );
153
161
}
154
162
155
- void refreshS3Client (AWSCredentials awsCredentials , AmazonS3 s3Client ) {
163
+ void refreshS3Client (
164
+ AWSCredentials awsCredentials , AmazonS3 s3Client , AwsCredentials v2Credentials ) {
165
+ var s3AsyncClient =
166
+ S3AsyncClient .builder ()
167
+ .credentialsProvider (() -> v2Credentials )
168
+ .region (Region .of (s3Config .getAwsRegion ()))
169
+ .build ();
156
170
// Once we get to this point, we know that we have a good new s3 client, so it's time to swap
157
171
// it. No fail can happen now
158
172
LOGGER .debug ("Replacing S3 Client" );
@@ -163,7 +177,9 @@ void refreshS3Client(AWSCredentials awsCredentials, AmazonS3 s3Client) {
163
177
// Swap the references
164
178
this .referenceCounter = new AtomicInteger (1 );
165
179
this .awsCredentials = awsCredentials ;
180
+ this .v2Credentials = v2Credentials ;
166
181
this .s3Client = s3Client ;
182
+ this .asyncClient = s3AsyncClient ;
167
183
LOGGER .debug ("S3 Client replaced" );
168
184
// At the end of the try, the reference counter will be decremented again and shutdown will
169
185
// be called
0 commit comments