Skip to content

Commit 31c6457

Browse files
committed
Migrate from v1 to v2 of AWS SDK for Java
v1 stops getting updates as of Dec 2025
1 parent d44ae98 commit 31c6457

File tree

4 files changed

+32
-35
lines changed

4 files changed

+32
-35
lines changed

service/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ dependencies {
4949
implementation 'org.springframework.boot:spring-boot-starter-logging'
5050

5151
// Infrastructure
52-
implementation 'com.amazonaws:aws-java-sdk-s3:1.12.767'
52+
implementation 'software.amazon.awssdk:s3:2.27.17'
5353
implementation ('com.azure:azure-storage-blob:12.27.1')
5454
implementation 'org.apache.qpid:qpid-jms-client:2.5.0'
5555

service/src/main/java/uk/nhs/adaptors/gp2gp/common/configuration/CustomTrustStore.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package uk.nhs.adaptors.gp2gp.common.configuration;
22

3+
import java.net.URI;
34
import java.security.KeyStore;
45
import java.security.cert.CertificateException;
56
import java.security.cert.X509Certificate;
@@ -11,10 +12,11 @@
1112

1213
import org.springframework.beans.factory.annotation.Autowired;
1314
import org.springframework.stereotype.Component;
14-
15-
import com.amazonaws.services.s3.AmazonS3;
16-
import com.amazonaws.services.s3.AmazonS3URI;
17-
import com.amazonaws.services.s3.model.GetObjectRequest;
15+
import software.amazon.awssdk.core.ResponseInputStream;
16+
import software.amazon.awssdk.services.s3.S3Uri;
17+
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
18+
import software.amazon.awssdk.services.s3.S3Client;
19+
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
1820

1921
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2022
import lombok.NoArgsConstructor;
@@ -27,12 +29,13 @@
2729
@NoArgsConstructor
2830
public class CustomTrustStore {
2931
@Autowired(required = false)
30-
private AmazonS3 s3Client;
32+
private S3Client s3Client;
3133

3234
@SneakyThrows
3335
public void addToDefault(String trustStorePath, String trustStorePassword) {
3436
final X509TrustManager defaultTrustManager = getDefaultTrustManager();
35-
final X509TrustManager customTrustManager = getCustomDbTrustManager(new AmazonS3URI(trustStorePath), trustStorePassword);
37+
final var s3Uri = s3Client.utilities().parseUri(URI.create(trustStorePath));
38+
final X509TrustManager customTrustManager = getCustomDbTrustManager(s3Uri, trustStorePassword);
3639
X509TrustManager combinedTrustManager = new CombinedTrustManager(customTrustManager, defaultTrustManager);
3740

3841
SSLContext sslContext = SSLContext.getInstance("TLS");
@@ -56,15 +59,15 @@ private X509TrustManager getDefaultTrustManager() {
5659

5760
@SneakyThrows
5861
@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE")
59-
private X509TrustManager getCustomDbTrustManager(AmazonS3URI s3URI, String trustStorePassword) {
62+
private X509TrustManager getCustomDbTrustManager(S3Uri s3Uri, String trustStorePassword) {
6063
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
6164
trustManagerFactory.init((KeyStore) null);
6265

63-
LOGGER.info("Loading custom KeyStore from '{}'", s3URI.toString());
64-
try (var s3Object = s3Client.getObject(new GetObjectRequest(s3URI.getBucket(), s3URI.getKey()));
65-
var content = s3Object.getObjectContent()) {
66+
LOGGER.info("Loading custom KeyStore from '{}'", s3Uri.toString());
67+
final var getObjectRequest = GetObjectRequest.builder().bucket(s3Uri.bucket().orElseThrow()).key(s3Uri.key().orElseThrow()).build();
68+
try (ResponseInputStream<GetObjectResponse> s3Object = s3Client.getObject(getObjectRequest)) {
6669
KeyStore customKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
67-
customKeyStore.load(content, trustStorePassword.toCharArray());
70+
customKeyStore.load(s3Object, trustStorePassword.toCharArray());
6871
trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
6972
trustManagerFactory.init(customKeyStore);
7073
}

service/src/main/java/uk/nhs/adaptors/gp2gp/common/storage/S3StorageConnector.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,41 @@
22

33
import java.io.InputStream;
44

5-
import com.amazonaws.services.s3.AmazonS3;
6-
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
7-
import com.amazonaws.services.s3.model.ObjectMetadata;
8-
import com.amazonaws.services.s3.model.S3Object;
5+
import software.amazon.awssdk.core.sync.RequestBody;
6+
import software.amazon.awssdk.services.s3.S3Client;
7+
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
8+
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
9+
import software.amazon.awssdk.core.ResponseInputStream;
10+
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
911

1012
public class S3StorageConnector implements StorageConnector {
11-
private final AmazonS3 s3client;
13+
private final S3Client s3client;
1214
private final String bucketName;
1315

1416
protected S3StorageConnector(StorageConnectorConfiguration configuration) {
1517
this.bucketName = configuration.getContainerName();
16-
this.s3client = AmazonS3ClientBuilder
17-
.standard()
18-
.build();
18+
this.s3client = S3Client.builder().build();
1919
}
2020

2121
@Override
2222
public void uploadToStorage(InputStream is, long streamLength, String filename) throws StorageConnectorException {
2323
try {
24-
ObjectMetadata metadata = new ObjectMetadata();
25-
metadata.setContentLength(streamLength);
24+
final var putObjectRequest = PutObjectRequest.builder().bucket(bucketName).key(filename).build();
2625

2726
s3client.putObject(
28-
bucketName,
29-
filename,
30-
is,
31-
metadata
27+
putObjectRequest,
28+
RequestBody.fromInputStream(is, streamLength)
3229
);
3330
} catch (Exception exception) {
3431
throw new StorageConnectorException("Error occurred uploading to S3 Bucket", exception);
3532
}
3633
}
3734

3835
@Override
39-
public InputStream downloadFromStorage(String filename) throws StorageConnectorException {
36+
public ResponseInputStream<GetObjectResponse> downloadFromStorage(String filename) throws StorageConnectorException {
4037
try {
41-
S3Object s3Object = s3client.getObject(bucketName, filename);
42-
return s3Object.getObjectContent();
38+
final var request = GetObjectRequest.builder().bucket(bucketName).key(filename).build();
39+
return s3client.getObject(request);
4340
} catch (Exception exception) {
4441
throw new StorageConnectorException("Error occurred downloading from S3 Bucket", exception);
4542
}

service/src/main/java/uk/nhs/adaptors/gp2gp/common/storage/StorageConnectorConfiguration.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import org.springframework.context.annotation.Bean;
66
import org.springframework.context.annotation.Configuration;
77

8-
import com.amazonaws.services.s3.AmazonS3;
9-
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
8+
import software.amazon.awssdk.services.s3.S3Client;
109

1110
import lombok.Getter;
1211
import lombok.Setter;
@@ -25,11 +24,9 @@ public class StorageConnectorConfiguration {
2524
private String trustStorePassword;
2625

2726
@Bean
28-
@SuppressWarnings("unused")
29-
public AmazonS3 getS3Client() {
27+
public S3Client getS3Client() {
3028
if (StringUtils.isNotBlank(trustStoreUrl) && trustStoreUrl.startsWith(S3_PREFIX)) {
31-
return AmazonS3ClientBuilder.standard()
32-
.build();
29+
return S3Client.builder().build();
3330
}
3431

3532
return null;

0 commit comments

Comments
 (0)