-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAwsS3Config.java
More file actions
45 lines (40 loc) · 1.68 KB
/
AwsS3Config.java
File metadata and controls
45 lines (40 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.app.config;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
@Configuration
@ConditionalOnProperty(name = "aws.s3.enabled", havingValue = "true")
@Log4j2
public class AwsS3Config {
@Value("${aws.s3.region:us-east-1}")
private String awsRegion;
@Value("${aws.s3.accessKeyId:us-east-1}")
private String accessKeyId;
@Value("${aws.s3.secretAccessKey}")
private String secretAccessKey;
@Bean
public S3Client s3Client() {
try {
log.info("Trying to S3Client create.");
return S3Client.builder()
.region(Region.of(awsRegion))
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKeyId, secretAccessKey)))
.overrideConfiguration(ClientOverrideConfiguration.builder()
.retryPolicy(r -> r.numRetries(3))
.build())
.build();
} catch (Exception e) {
log.error("Failed to create S3Client.", e);
throw e;
} finally {
log.info("S3Client created successfully.");
}
}
}