AWS S3 버킷 생성
- 버킷 생성
- 버킷 정책 설정
- AWS S3- 버킷- 해당 버킷 클릭- 권한- 버킷 정책
- 버킷 정책을 편집을 누르고 아래 코드를 추가해준다.
-
{ "Version": "2012-10-17", "Statement": [ { "Sid": "PublicReadGetObject", "Effect": "Allow", "Principal": "*", "Action": "s3:GetObject", "Resource": "arn:aws:s3:::{버킷이름}/*" } ] }
-
스프링부트 이미지 업로드 구현
- build.gradle 추가
-
// AWS S3 implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
-
- application.yml 추가
-
cloud: aws: s3: bucket: hanafun-bucket # 버킷이름 credentials: access-key: #액세스키 secret-key: #비밀액세스키 region: static: ap-northeast-2 auto: false stack: auto: false
-
- S3Config.java 생성
-
import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class S3Config { @Value("${cloud.aws.credentials.access-key}") private String accessKey; @Value("${cloud.aws.credentials.secret-key}") private String secretKey; @Value("${cloud.aws.region.static}") private String region; @Bean public AmazonS3Client amazonS3Client() { BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); return (AmazonS3Client) AmazonS3ClientBuilder .standard() .withRegion(region) .withCredentials(new AWSStaticCredentialsProvider(credentials)) .build(); } }
-
- S3UploadeService.java 생성
-
import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.model.CannedAccessControlList; import com.amazonaws.services.s3.model.ObjectMetadata; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; @RequiredArgsConstructor @Service public class S3UploadService { private final AmazonS3 amazonS3; @Value("${cloud.aws.s3.bucket}") private String bucket; public String saveFile(MultipartFile multipartFile) throws IOException { String originalFilename = multipartFile.getOriginalFilename(); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(multipartFile.getSize()); metadata.setContentType(multipartFile.getContentType()); amazonS3.putObject(bucket, originalFilename, multipartFile.getInputStream(), metadata); // 파일을 퍼블릭으로 설정 amazonS3.setObjectAcl(bucket, originalFilename, CannedAccessControlList.PublicRead); return amazonS3.getUrl(bucket, originalFilename).toString(); } }
-
- Controller 추가
-
private final S3UploadService s3UploadService; // 클래스 이미지 업로드 @PostMapping("/lesson/image-upload") public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile file) { try { String imageUrl = s3UploadService.saveFile(file); return ResponseEntity.ok(imageUrl); } catch (IOException e) { return ResponseEntity.status(500).body("파일 업로드 중 오류가 발생했습니다."); } }
-
Postman 테스트
- 테스트 방법
- Body에서 form-data로 변경
- Key- file, Value- 이미지 업로드 후 Send 클릭
- URL return 완료
- AWS S3 버킷 업로드 확인
728x90
'Back-end > Spring Boot' 카테고리의 다른 글
[Spring Boot] REST API 생성 순서 (3) | 2025.01.23 |
---|---|
[Spring Boot] Repository - Service - Controller (0) | 2024.04.21 |
[Spring Boot] Scheduler (0) | 2024.04.13 |
[Spring Boot] TDD (0) | 2024.04.13 |
[Spring Boot] MyBatis (0) | 2024.04.13 |