라이브러리 추가
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에 접근할 수 있다.
'java > spring' 카테고리의 다른 글
[Spring] Failed to validate connection (0) | 2022.08.03 |
---|---|
[Spring] AWS S3 객체 전체 조회 or 해당 level만 조회 (0) | 2022.08.01 |
[Spring] S3 Pre-Signed URL 생성 (0) | 2022.07.25 |
[Spring] @Value, static 변수에 사용하기 (0) | 2022.07.22 |
[Spring] Spring Security + Swagger 3.0 적용 (0) | 2022.07.22 |