본문 바로가기
Spring

[TIL] 240510 Spring S3 bucket에 사진 추가 | SpringBoot MultipartFile로 이미지 추가하기

by studymode 2024. 5. 10.

AWS S3 버킷 만들기

먼저 AWS S3버킷을 만들어줘야합니다!!

AWS S3 > 버킷 > 버킷 만들기

 

 

Spring 연결

1. build.gradle 의존성 추가

 

🔽 build.gradle

  // AWS S3
    implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'

 

2. application.properties 추가

 

🔽 application.properties

# S3
cloud.aws.credentials.accessKey={발급받은 공개키}
cloud.aws.credentials.secretKey={발급받은 비밀키}
cloud.aws.s3.bucket={버킷명}
cloud.aws.region.static:ap-northeast-2
cloud.aws.stack.auto=false

 

 

3. S3 파일 생성

 

🔽 S3Config

@Configuration
public class S3Config {
    @Value("${cloud.aws.credentials.accessKey}")
    private String accessKey;

    @Value("${cloud.aws.credentials.secretKey}")
    private String secretKey;

    @Value("${cloud.aws.region.static}")
    private String region;

    @Bean
    public AmazonS3 amazonS3Client() {
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);

        return AmazonS3ClientBuilder
                .standard()
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .withRegion(region)
                .build();
    }
}

 

 

4. S3 Manager만들기

multipartFile을 S3버킷에 저장해주는 S3 Manager을 만들어서 이미지 파일 관리!!

 

 

 

 

🔽 참고 블로그

 

[Spring] S3 이미지 업로드하기

사진, 동영상 등 파일을 저장하기 위해 사용하는 파일 서버 서비스. 일반적인 파일서버는 트래픽이 증가함에 따라서 장비를 증설하는 작업을 해야 하는데 S3는 이와 같은 것을 대행한다.

velog.io