-
Notifications
You must be signed in to change notification settings - Fork 8
refactor: s3 버전 업그레이드 및 로직 수정 #608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
8bc53a7
6210cac
4a020d6
e799840
18b9039
a709bdf
1bd38ee
3d01d3b
4c7febc
137f27c
f31bf48
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,49 @@ | ||
| package com.example.solidconnection.s3.service; | ||
| package com.example.solidconnection.s3.service; | ||
whqtker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| import static com.example.solidconnection.common.exception.ErrorCode.S3_CLIENT_EXCEPTION; | ||
| import static com.example.solidconnection.common.exception.ErrorCode.S3_SERVICE_EXCEPTION; | ||
| import static com.example.solidconnection.common.exception.ErrorCode.S3_CLIENT_EXCEPTION; | ||
| import static com.example.solidconnection.common.exception.ErrorCode.S3_SERVICE_EXCEPTION; | ||
|
|
||
| import com.amazonaws.AmazonServiceException; | ||
| import com.amazonaws.SdkClientException; | ||
| import com.amazonaws.services.s3.AmazonS3Client; | ||
| import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
| import com.amazonaws.services.s3.model.ObjectMetadata; | ||
| import com.amazonaws.services.s3.model.PutObjectRequest; | ||
| import com.example.solidconnection.common.exception.CustomException; | ||
| import java.io.IOException; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.scheduling.annotation.Async; | ||
| import org.springframework.scheduling.annotation.EnableAsync; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
| import com.example.solidconnection.common.exception.CustomException; | ||
| import java.io.IOException; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.scheduling.annotation.Async; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
| import software.amazon.awssdk.core.exception.SdkException; | ||
| import software.amazon.awssdk.core.sync.RequestBody; | ||
| import software.amazon.awssdk.services.s3.S3Client; | ||
| import software.amazon.awssdk.services.s3.model.ObjectCannedACL; | ||
| import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
| import software.amazon.awssdk.services.s3.model.S3Exception; | ||
|
|
||
| @Component | ||
| @EnableAsync | ||
| @Slf4j | ||
| public class FileUploadService { | ||
| @Component | ||
| @Slf4j | ||
| @RequiredArgsConstructor | ||
| public class FileUploadService { | ||
|
|
||
| private final AmazonS3Client amazonS3; | ||
| private final S3Client s3Client; | ||
|
|
||
| public FileUploadService(AmazonS3Client amazonS3) { | ||
| this.amazonS3 = amazonS3; | ||
| } | ||
| @Async | ||
| public void uploadFile(String bucket, String fileName, MultipartFile multipartFile) { | ||
|
||
| try { | ||
| PutObjectRequest putObjectRequest = PutObjectRequest.builder() | ||
| .bucket(bucket) | ||
| .key(fileName) | ||
| .contentType(multipartFile.getContentType()) | ||
| .contentLength(multipartFile.getSize()) | ||
| .build(); | ||
|
|
||
| @Async | ||
| public void uploadFile(String bucket, String fileName, MultipartFile multipartFile) { | ||
| // 메타데이터 생성 | ||
| String contentType = multipartFile.getContentType(); | ||
| ObjectMetadata metadata = new ObjectMetadata(); | ||
| metadata.setContentType(contentType); | ||
| metadata.setContentLength(multipartFile.getSize()); | ||
| s3Client.putObject(putObjectRequest, | ||
| RequestBody.fromInputStream(multipartFile.getInputStream(), multipartFile.getSize())); | ||
|
|
||
| try { | ||
| amazonS3.putObject(new PutObjectRequest(bucket, fileName, multipartFile.getInputStream(), metadata) | ||
| .withCannedAcl(CannedAccessControlList.PublicRead)); | ||
| log.info("이미지 업로드 정상적 완료 thread: {}", Thread.currentThread().getName()); | ||
| } catch (AmazonServiceException e) { | ||
| log.error("이미지 업로드 중 s3 서비스 예외 발생 : {}", e.getMessage()); | ||
| throw new CustomException(S3_SERVICE_EXCEPTION); | ||
| } catch (SdkClientException | IOException e) { | ||
| log.error("이미지 업로드 중 s3 클라이언트 예외 발생 : {}", e.getMessage()); | ||
| throw new CustomException(S3_CLIENT_EXCEPTION); | ||
| log.info("파일 업로드 정상 완료 thread: {}", Thread.currentThread().getName()); | ||
| } catch (S3Exception e) { | ||
| log.error("S3 서비스 예외 발생 : {}", e.awsErrorDetails().errorMessage()); | ||
| throw new CustomException(S3_SERVICE_EXCEPTION); | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } catch (SdkException | IOException e) { | ||
| log.error("S3 클라이언트 또는 IO 예외 발생 : {}", e.getMessage()); | ||
| throw new CustomException(S3_CLIENT_EXCEPTION); | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저에게는 UploadType이 직관적으로는 업로드한 파일이 어떤 타입인지, 즉 파일 확장자를 구별하는 걸로 느껴집니다..!
해당 enum의 목적이 어떤 기능(도메인)에서 사용되는 기능인지를 구분하고, 저장하는 파일의 basePath를 결정지어야 하는 책임이 있다고 생각해서 다른 네이밍이 좋을 것 같다는 조심스런 의견을 드립니다 ㅎㅎ
(추신 : 제미나이가 추천한 이름은
UploadPath,UploadCategory였습니다..!)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이건 회의때 한 번 얘기해볼게용
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UploadPath로 변경했습니다