11package io .ejangs .docsa .domain .image .config ;
22
3+ import java .net .URI ;
34import lombok .RequiredArgsConstructor ;
45
56import org .springframework .beans .factory .annotation .Value ;
67import org .springframework .context .annotation .Bean ;
78import org .springframework .context .annotation .Configuration ;
9+ import software .amazon .awssdk .auth .credentials .AwsBasicCredentials ;
10+ import software .amazon .awssdk .auth .credentials .StaticCredentialsProvider ;
811import software .amazon .awssdk .regions .Region ;
912import software .amazon .awssdk .services .s3 .S3Client ;
13+ import software .amazon .awssdk .services .s3 .S3ClientBuilder ;
14+ import software .amazon .awssdk .services .s3 .S3Configuration ;
1015import software .amazon .awssdk .services .s3 .presigner .S3Presigner ;
1116
1217@ Configuration
@@ -15,19 +20,59 @@ public class S3Config {
1520
1621 @ Bean
1722 public S3Presigner s3Presigner (
18- @ Value ("${cloud.aws.region}" ) String region
23+ @ Value ("${cloud.aws.region}" ) String region ,
24+ @ Value ("${cloud.aws.s3.endpoint:}" ) String endpoint ,
25+ @ Value ("${cloud.aws.s3.path-style-access-enabled:false}" ) boolean pathStyleAccessEnabled ,
26+ @ Value ("${cloud.aws.credentials.access-key:}" ) String accessKey ,
27+ @ Value ("${cloud.aws.credentials.secret-key:}" ) String secretKey
1928 ) {
20- return S3Presigner .builder ()
21- .region (Region .of (region ))
22- .build ();
29+ S3Presigner .Builder builder = S3Presigner .builder ()
30+ .region (Region .of (region ));
31+
32+ if (!endpoint .isBlank ()) {
33+ builder .endpointOverride (URI .create (endpoint ));
34+ }
35+
36+ if (pathStyleAccessEnabled ) {
37+ builder .serviceConfiguration (S3Configuration .builder ()
38+ .pathStyleAccessEnabled (true )
39+ .build ());
40+ }
41+
42+ if (!accessKey .isBlank () && !secretKey .isBlank ()) {
43+ builder .credentialsProvider (StaticCredentialsProvider .create (
44+ AwsBasicCredentials .create (accessKey , secretKey )));
45+ }
46+
47+ return builder .build ();
2348 }
2449
2550 @ Bean
2651 public S3Client s3Client (
27- @ Value ("${cloud.aws.region}" ) String region
52+ @ Value ("${cloud.aws.region}" ) String region ,
53+ @ Value ("${cloud.aws.s3.endpoint:}" ) String endpoint ,
54+ @ Value ("${cloud.aws.s3.path-style-access-enabled:false}" ) boolean pathStyleAccessEnabled ,
55+ @ Value ("${cloud.aws.credentials.access-key:}" ) String accessKey ,
56+ @ Value ("${cloud.aws.credentials.secret-key:}" ) String secretKey
2857 ) {
29- return S3Client .builder ()
30- .region (Region .of (region ))
31- .build ();
58+ S3ClientBuilder builder = S3Client .builder ()
59+ .region (Region .of (region ));
60+
61+ if (!endpoint .isBlank ()) {
62+ builder .endpointOverride (URI .create (endpoint ));
63+ }
64+
65+ if (pathStyleAccessEnabled ) {
66+ builder .serviceConfiguration (S3Configuration .builder ()
67+ .pathStyleAccessEnabled (true )
68+ .build ());
69+ }
70+
71+ if (!accessKey .isBlank () && !secretKey .isBlank ()) {
72+ builder .credentialsProvider (StaticCredentialsProvider .create (
73+ AwsBasicCredentials .create (accessKey , secretKey )));
74+ }
75+
76+ return builder .build ();
3277 }
3378}
0 commit comments