연동 설정

AWS S3 설정을 연동하여 클라우드 서비스에 업로드 하고 다운로드하며 파일을 관리하는 서비스를 만들어봅니다.


S3 계정 설정


dependencies 추가

// S3 (버전맞게 조정해주세요)
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'

코드 작성

본격적으로 코드를 작성해서 연동을 해보자


어플리케이션 S3 연결 설정

@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();
	}
}

업로드 및 다운로드 기능 구현