java/spring
[Spring] AWS S3 접근
danuri
2022. 7. 25. 17:25
라이브러리 추가
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
인증 키 추가
<application.yml>
본인 IAM 인증 키를 추가하면 된다.
cloud:
aws:
credentials:
instance-profile: false
access-key: [access-key]
secret-key: [secret-key]
region:
auto: false
static: ap-northeast-2
stack:
auto: false
S3Config
@Configuration
public class S3Config {
@Value("${cloud.aws.credentials.access-key}")
public String accessKey;
@Value("${cloud.aws.credentials.secret-key}")
public String secretKey;
@Value("${cloud.aws.region.static}")
public String region;
@Bean
@Primary
public BasicAWSCredentials awsCredentialsProvider(){
BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey, secretKey);
return basicAWSCredentials;
}
@Bean
public AmazonS3 amazonS3() {
AmazonS3 s3Builder = AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(awsCredentialsProvider()))
.build();
return s3Builder;
}
}
이제 amazonS3.getObject() 등 S3에 접근할 수 있다.