Created
March 25, 2021 21:15
-
-
Save krisiye/dc17be11e955e3a75ce09eab462b5c13 to your computer and use it in GitHub Desktop.
AWSConfiguration Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example.springboot.config; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | |
import org.springframework.cloud.context.config.annotation.RefreshScope; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import com.amazonaws.auth.AWSCredentials; | |
import com.amazonaws.auth.AWSSessionCredentials; | |
import com.amazonaws.auth.AWSStaticCredentialsProvider; | |
import com.amazonaws.auth.BasicSessionCredentials; | |
import com.amazonaws.services.s3.AmazonS3; | |
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | |
/** | |
* AWS configuration class to initialize aws client sdk. | |
* | |
* @author iyerk | |
* | |
*/ | |
@Configuration | |
@ConditionalOnProperty(name="spring.cloud.vault.aws.enabled") | |
@RefreshScope | |
public class AWSConfiguration { | |
@Autowired | |
AwsConfigurationProperties awsConfigurationProperties; | |
@Value("${cloud.aws.region}") | |
private String region; | |
@Bean | |
@RefreshScope | |
public AWSSessionCredentials basicAWSCredentials() { | |
AWSSessionCredentials credentials = new BasicSessionCredentials(awsConfigurationProperties.getAccesskey(), | |
awsConfigurationProperties.getSecretKey(), awsConfigurationProperties.getSessionToken()); | |
return credentials; | |
} | |
@Bean | |
@RefreshScope | |
public AmazonS3 amazonS3Client(AWSCredentials awsCredentials) { | |
AmazonS3 s3client = AmazonS3ClientBuilder | |
.standard() | |
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) | |
.withRegion(region) | |
.build(); | |
return s3client; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment