AWS S3 설정을 연동하여 클라우드 서비스에 업로드 하고 다운로드하며 파일을 관리하는 서비스를 만들어봅니다.
I AM
키 단위로 정책을 적용할 수 있음.application.yml
cloud:
aws:
credentials:
access-key: {전달받은 키 파일 중 Access Key ID}
secret-key: {전달받은 키 파일 중 Secret Access Key}
region:
static: ap-northeast-2 (다를 수 있으니 확인하고 설정해주시면 됩니다.)
bucket: example-bucket
// S3 (버전맞게 조정해주세요)
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
본격적으로 코드를 작성해서 연동을 해보자
@Configuration
public class S3Config {
@Value("${cloud.aws.credentials.access-key}")
private String accessKey;
@Value("${cloud.aws.credentials.access-key}")
private String secretKey;
@Value("${cloud.aws.region.static}")
private String region;
@Bean
public AmazonS3Client amazonS3Config() {
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
return (AmazonS3Client) AmazonS3ClientBuilder.standard()
.withRegion(Regions.fromName(region)) // or .withRegion(Regions.AP_NORTHEAST_2)
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.build();
}
}