8
8
import software .amazon .awssdk .auth .credentials .AwsCredentials ;
9
9
import software .amazon .awssdk .regions .Region ;
10
10
import software .amazon .awssdk .services .s3 .S3Client ;
11
+ import software .amazon .awssdk .services .s3 .S3ClientBuilder ;
11
12
import software .amazon .awssdk .services .s3 .model .GetObjectRequest ;
12
13
13
14
import java .io .InputStream ;
@@ -54,10 +55,18 @@ public static ResourceFactory<InputStream> getInstance() {
54
55
@ Override
55
56
public Resource <InputStream > request (
56
57
AwsCredentials awsCredentials , ParameterSet parameterSet ) {
58
+ // Get S3 URL and region from the parameter set.
57
59
String s3Url = parameterSet .getRequired (S3_URL );
58
60
String region = parameterSet .getOptional (REGION );
59
61
60
- URI uri = null ;
62
+ // Create S3 client builder and specify the region if it's not null
63
+ S3ClientBuilder builder = S3Client
64
+ .builder ()
65
+ .credentialsProvider (() -> awsCredentials );
66
+ if (region != null ) builder .region (Region .of (region ));
67
+
68
+ // Create URI to get the bucket name and the object key
69
+ URI uri ;
61
70
try {
62
71
uri = new URI (s3Url );
63
72
} catch (URISyntaxException uriSyntaxException ) {
@@ -66,11 +75,7 @@ public Resource<InputStream> request(
66
75
uriSyntaxException );
67
76
}
68
77
69
- try (S3Client client = S3Client .builder ()
70
- .region (Region .of (region ))
71
- .credentialsProvider (() -> awsCredentials )
72
- .build ()) {
73
-
78
+ try (S3Client client = builder .build ()) {
74
79
String bucketName = uri .getHost ();
75
80
String objectKey = uri .getPath ()
76
81
.substring (1 );
0 commit comments