-
Notifications
You must be signed in to change notification settings - Fork 17
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
스프링 이벤트를 사용한 S3 이미지 파일 삭제 기능 추가 #714
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9181f38
refactor: 이미지 업로드를 담당하는 클래스로 분리
jjongwa fc40a6a
refactor: 사용하지 않는 & 중복된 Repository 삭제
jjongwa 575dd23
feat: S3ImageEvent와 listener 구현
jjongwa eda8458
feat: 이미지 네임 삭제 완료시 S3에서 해당 파일 삭제하는 기능 추가
jjongwa 40b5682
refactor: trip 대표이미지 url -> name 변경
jjongwa 0319c60
feat: 여행 대표이미지 변경 시 이전에 존재하던 이미지 파일 S3에서 삭제하는 기능 추가
jjongwa b81c316
refactor: 사용하지 않는 상수 제거
jjongwa 3db499b
feat: 마이페이지 프로필 사진 변경 시 S3에 존재하는 기존 파일 삭제 기능 추가
jjongwa aa4b8cf
refactor: static import 적용
jjongwa 003eeff
refactor: 변수 네이밍 변경 및 컨벤션 적용
jjongwa ff85e4d
refactor: 삭제 여부 조건 및 예외 코드 변경
jjongwa 09713b7
feat: 아이템 삭제 시 이미지 파일 삭제 기능 추가
jjongwa efa5c8d
refactor: 네이밍 변경
jjongwa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
backend/src/main/java/hanglog/image/domain/S3ImageEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package hanglog.image.domain; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public class S3ImageEvent { | ||
|
||
private final String imageName; | ||
} |
54 changes: 54 additions & 0 deletions
54
backend/src/main/java/hanglog/image/infrastructure/ImageUploader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package hanglog.image.infrastructure; | ||
|
||
import static hanglog.global.exception.ExceptionCode.INVALID_IMAGE; | ||
import static hanglog.global.exception.ExceptionCode.INVALID_IMAGE_PATH; | ||
|
||
import com.amazonaws.AmazonServiceException; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import hanglog.global.exception.ImageException; | ||
import hanglog.image.domain.ImageFile; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class ImageUploader { | ||
|
||
private static final String CACHE_CONTROL_VALUE = "max-age=3153600"; | ||
|
||
private final AmazonS3 s3Client; | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
@Value("${cloud.aws.s3.folder}") | ||
private String folder; | ||
|
||
public List<String> uploadImages(final List<ImageFile> imageFiles) { | ||
return imageFiles.stream() | ||
.map(this::uploadImage) | ||
.toList(); | ||
} | ||
|
||
private String uploadImage(final ImageFile imageFile) { | ||
final String path = folder + imageFile.getHashedName(); | ||
final ObjectMetadata metadata = new ObjectMetadata(); | ||
metadata.setContentType(imageFile.getContentType()); | ||
metadata.setContentLength(imageFile.getSize()); | ||
metadata.setCacheControl(CACHE_CONTROL_VALUE); | ||
|
||
try (final InputStream inputStream = imageFile.getInputStream()) { | ||
s3Client.putObject(bucket, path, inputStream, metadata); | ||
} catch (final AmazonServiceException e) { | ||
throw new ImageException(INVALID_IMAGE_PATH); | ||
} catch (final IOException e) { | ||
throw new ImageException(INVALID_IMAGE); | ||
} | ||
return imageFile.getHashedName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
backend/src/main/java/hanglog/listener/S3ImageEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package hanglog.listener; | ||
|
||
import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW; | ||
|
||
import com.amazonaws.services.s3.AmazonS3; | ||
import hanglog.image.domain.S3ImageEvent; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.transaction.event.TransactionalEventListener; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class S3ImageEventListener { | ||
|
||
private final AmazonS3 s3Client; | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
@Value("${cloud.aws.s3.folder}") | ||
private String folder; | ||
|
||
@Async | ||
@Transactional(propagation = REQUIRES_NEW) | ||
@TransactionalEventListener(fallbackExecution = true) | ||
public void deleteImageFileInS3(final S3ImageEvent event) { | ||
final String imageName = event.getImageName(); | ||
s3Client.deleteObject(bucket, folder + imageName); | ||
Comment on lines
+30
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. 여기서 catch를 받아 customException을 던져 줄까요.? |
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@Value
때문에 분리해준 건가요? 분리한 이유는 무엇인가요? 어차피 서비스에서 밖에 사용 안되던데...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.
레이어드 아키텍처 관점에서 분리했습니다.!