[Spring Boot] S3 Bucket에 파일 업로드

귀찮Lee·2022년 9월 11일
0

MOCCO Project (22.09~10)

목록 보기
1/5

◎ S3 설정

◎ property 설정

cloud:
  aws:
    credentials:
      accessKey: {{ AWS IAM AccessKey }}
      secretKey: {{ AWS IAM SecretKey }}
    s3:
      bucket: {{ bucket 이름 }}
      dir: profile
    region:
      static: ap-northeast-2 # 해당 지역 작성
    stack:
      auto: false

◎ S3에 파일 올리기

  • interface

    public interface S3Upload {
    
        String upload(InputStream inputStream, String originFileName, String fileSize);
    }
  • implement

    @RequiredArgsConstructor
    @Service
    public class S3UploadImpl implements S3Upload{
    
        @Value("${cloud.aws.s3.bucket}")
        private String bucket;
    
        @Value("${cloud.aws.s3.dir}")
        private String dir;
    
        private final AmazonS3Client s3Client;
    
        @Override
        public String upload(InputStream inputStream, String originFileName, String fileSize) {
    
            // image 체크       
            if (!originFileName.substring(originFileName.length()-4).equals(".jpg") &&
                    !originFileName.substring(originFileName.length()-4).equals(".png")){
                throw new BusinessLogicException(ExceptionCode.NOT_IMAGE_EXTENSION);
            }
    
            // 저장할 파일 이름 설정
            DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss-SSS");
            // "/"를 이용해 파일 하위 폴더 설정
            String s3FileName = dir + "/" +LocalDateTime.now().format(format)+ "-" + originFileName;
    
            ObjectMetadata objMeta = new ObjectMetadata();
            objMeta.setContentLength(Long.parseLong(fileSize));
    
            s3Client.putObject(bucket, s3FileName, inputStream, objMeta);
    
            return s3Client.getUrl(bucket, s3FileName).toString();
        }
    }
  • 사용 예시

      @PatchMapping("/profile-image/{member-id}")
      @Secured("ROLE_USER")
      public ResponseEntity patchImage(@PathVariable("member-id") @Positive long memberId,
                                       Authentication authentication,
                                       @RequestParam("images") MultipartFile multipartFile,
                                       @RequestParam String fileSize) throws IOException {
    
          securityService.checkMemberEqual(authentication, memberId);
          String url = s3Upload.upload(multipartFile.getInputStream(),
                  multipartFile.getOriginalFilename(), fileSize);
    
          Member member = new Member();
          member.setMemberId(memberId);
          member.setImage(url);
    
          Member updateMember = memberService.updateMember(member);
          MemberDto.Response response = mapper.memberToMemberResponseDto(updateMember);
          return new ResponseEntity<>(
                  new SingleResponseDto(response), HttpStatus.OK
          );
      }

◎ 참고 자료

profile
배운 것은 기록하자! / 오류 지적은 언제나 환영!

0개의 댓글