-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat(S3PreSignedUrl): S3 파일 업로드 방식을 PreSigned URL로 구현 #159
Open
qogustj
wants to merge
8
commits into
develop
Choose a base branch
from
feat/158-s3image
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f9b4ddf
[refactor]: #133 fetch join, count 쿼리 최적화를 통한 중복 방지
qogustj 24dc491
Merge branch 'local develop' into develop
qogustj b373c47
Merge branch 'develop' of github.com:ssu-student-union/homepage-backe…
qogustj 8a01a16
feat(S3PreSignedUrl): S3 파일 업로드 방식을 PreSigned URL로 변경
qogustj 1de4c10
refactor(ImageService): 코드 스타일 및 성능 개선
qogustj 4315239
refactor(S3PreSignedUrlUtils): PreSigned URL 설정 개선 및 Content-Type 검증 강화
qogustj 6b79342
refactor(file): PreSigned URL 생성 로직 개선
qogustj c1d39da
feat(S3): S3 파일 존재 여부 검증 비동기 처리 추가
qogustj 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
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
61 changes: 61 additions & 0 deletions
61
src/main/generated/ussum/homepage/infra/jpa/post/entity/QRightsDetailEntity.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,61 @@ | ||
package ussum.homepage.infra.jpa.post.entity; | ||
|
||
import static com.querydsl.core.types.PathMetadataFactory.*; | ||
|
||
import com.querydsl.core.types.dsl.*; | ||
|
||
import com.querydsl.core.types.PathMetadata; | ||
import javax.annotation.processing.Generated; | ||
import com.querydsl.core.types.Path; | ||
import com.querydsl.core.types.dsl.PathInits; | ||
|
||
|
||
/** | ||
* QRightsDetailEntity is a Querydsl query type for RightsDetailEntity | ||
*/ | ||
@Generated("com.querydsl.codegen.DefaultEntitySerializer") | ||
public class QRightsDetailEntity extends EntityPathBase<RightsDetailEntity> { | ||
|
||
private static final long serialVersionUID = 30014936L; | ||
|
||
private static final PathInits INITS = PathInits.DIRECT2; | ||
|
||
public static final QRightsDetailEntity rightsDetailEntity = new QRightsDetailEntity("rightsDetailEntity"); | ||
|
||
public final NumberPath<Long> id = createNumber("id", Long.class); | ||
|
||
public final StringPath major = createString("major"); | ||
|
||
public final StringPath name = createString("name"); | ||
|
||
public final EnumPath<RightsDetailEntity.PersonType> personType = createEnum("personType", RightsDetailEntity.PersonType.class); | ||
|
||
public final StringPath phoneNumber = createString("phoneNumber"); | ||
|
||
public final QPostEntity postEntity; | ||
|
||
public final StringPath studentId = createString("studentId"); | ||
|
||
public QRightsDetailEntity(String variable) { | ||
this(RightsDetailEntity.class, forVariable(variable), INITS); | ||
} | ||
|
||
public QRightsDetailEntity(Path<? extends RightsDetailEntity> path) { | ||
this(path.getType(), path.getMetadata(), PathInits.getFor(path.getMetadata(), INITS)); | ||
} | ||
|
||
public QRightsDetailEntity(PathMetadata metadata) { | ||
this(metadata, PathInits.getFor(metadata, INITS)); | ||
} | ||
|
||
public QRightsDetailEntity(PathMetadata metadata, PathInits inits) { | ||
this(RightsDetailEntity.class, metadata, inits); | ||
} | ||
|
||
public QRightsDetailEntity(Class<? extends RightsDetailEntity> type, PathMetadata metadata, PathInits inits) { | ||
super(type, metadata, inits); | ||
this.postEntity = inits.isInitialized("postEntity") ? new QPostEntity(forProperty("postEntity"), inits.get("postEntity")) : null; | ||
} | ||
|
||
} | ||
|
47 changes: 47 additions & 0 deletions
47
src/main/java/ussum/homepage/application/image/controller/ImageController.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,47 @@ | ||
package ussum.homepage.application.image.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import ussum.homepage.application.image.controller.dto.request.FileUploadConfirmRequest; | ||
import ussum.homepage.application.image.controller.dto.request.FileUploadRequest; | ||
import ussum.homepage.application.image.controller.dto.response.PreSignedUrlResponse; | ||
import ussum.homepage.application.image.service.ImageService; | ||
import ussum.homepage.application.post.service.dto.response.postSave.PostFileListResponse; | ||
import ussum.homepage.global.ApiResponse; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/image") | ||
public class ImageController { | ||
|
||
private final ImageService imageService; | ||
|
||
@PostMapping("/presigned-url") | ||
public ResponseEntity<ApiResponse<?>> getPreSignedUrls( | ||
@RequestParam(value = "userId") Long userId, | ||
@RequestParam(value = "boardCode") String boardCode, | ||
@RequestBody FileUploadRequest request | ||
) { | ||
return ApiResponse.success( | ||
imageService.createPreSignedUrls( | ||
userId, | ||
boardCode, | ||
request.files(), | ||
request.images() | ||
) | ||
); | ||
} | ||
|
||
@PostMapping("/confirm") | ||
public ResponseEntity<ApiResponse<?>> confirmUpload( | ||
@RequestParam(value = "userId") Long userId, | ||
@RequestBody List<FileUploadConfirmRequest> confirmRequests | ||
) { | ||
return ApiResponse.success( | ||
imageService.confirmUpload(userId, confirmRequests) | ||
); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/ussum/homepage/application/image/controller/dto/request/FileRequest.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,7 @@ | ||
package ussum.homepage.application.image.controller.dto.request; | ||
|
||
public record FileRequest( | ||
String fileName, | ||
String contentType | ||
) { | ||
} |
8 changes: 8 additions & 0 deletions
8
...ava/ussum/homepage/application/image/controller/dto/request/FileUploadConfirmRequest.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,8 @@ | ||
package ussum.homepage.application.image.controller.dto.request; | ||
|
||
public record FileUploadConfirmRequest( | ||
String fileUrl, | ||
String fileType, | ||
String originalFileName | ||
) { | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/ussum/homepage/application/image/controller/dto/request/FileUploadRequest.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,22 @@ | ||
package ussum.homepage.application.image.controller.dto.request; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public record FileUploadRequest( | ||
List<FileRequest> files, | ||
List<FileRequest> images | ||
) { | ||
public static FileUploadRequest of(List<FileRequest> files, List<FileRequest> images) { | ||
return new FileUploadRequest( | ||
files != null ? files : Collections.emptyList(), | ||
images != null ? images : Collections.emptyList() | ||
); | ||
} | ||
|
||
// null check를 위한 기본 생성자 | ||
public FileUploadRequest { | ||
files = files != null ? files : Collections.emptyList(); | ||
images = images != null ? images : Collections.emptyList(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...n/java/ussum/homepage/application/image/controller/dto/response/PreSignedUrlResponse.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,14 @@ | ||
package ussum.homepage.application.image.controller.dto.response; | ||
|
||
import ussum.homepage.application.image.service.dto.PreSignedUrlInfo; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public record PreSignedUrlResponse( | ||
List<PreSignedUrlInfo> preSignedUrls | ||
) { | ||
public static PreSignedUrlResponse of(List<PreSignedUrlInfo> preSignedUrls) { | ||
return new PreSignedUrlResponse(preSignedUrls); | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
src/main/java/ussum/homepage/application/image/service/ImageService.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,116 @@ | ||
package ussum.homepage.application.image.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import ussum.homepage.application.image.controller.dto.request.FileRequest; | ||
import ussum.homepage.application.image.controller.dto.request.FileUploadConfirmRequest; | ||
import ussum.homepage.application.image.controller.dto.response.PreSignedUrlResponse; | ||
import ussum.homepage.application.image.service.dto.PreSignedUrlInfo; | ||
import ussum.homepage.application.post.service.dto.response.postSave.PostFileListResponse; | ||
import ussum.homepage.application.post.service.dto.response.postSave.PostFileResponse; | ||
import ussum.homepage.domain.post.PostFile; | ||
import ussum.homepage.domain.post.service.PostFileAppender; | ||
import ussum.homepage.infra.utils.S3PreSignedUrlUtils; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
import org.springframework.util.CollectionUtils; | ||
import ussum.homepage.infra.utils.s3.S3FileException; | ||
|
||
import static ussum.homepage.global.error.status.ErrorStatus.FILE_NOT_FOUND; | ||
|
||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class ImageService { | ||
private final S3PreSignedUrlUtils s3PreSignedUrlUtils; | ||
private final PostFileAppender postFileAppender; | ||
|
||
public PreSignedUrlResponse createPreSignedUrls( | ||
Long userId, | ||
String boardCode, | ||
List<FileRequest> files, | ||
List<FileRequest> images | ||
) { | ||
List<PreSignedUrlInfo> preSignedUrls = new ArrayList<>(); | ||
|
||
// 이미지 파일 처리 | ||
if (!CollectionUtils.isEmpty(images)) { | ||
List<PreSignedUrlInfo> imageUrls = s3PreSignedUrlUtils.generatePreSignedUrlWithPath( | ||
userId, | ||
boardCode, | ||
images.stream().map(FileRequest::fileName).toList(), | ||
images.stream().map(FileRequest::contentType).toList(), | ||
"images" | ||
); | ||
preSignedUrls.addAll(imageUrls); | ||
} | ||
|
||
// 일반 파일 처리 | ||
if (!CollectionUtils.isEmpty(files)) { | ||
List<PreSignedUrlInfo> fileUrls = s3PreSignedUrlUtils.generatePreSignedUrlWithPath( | ||
userId, | ||
boardCode, | ||
files.stream().map(FileRequest::fileName).toList(), | ||
files.stream().map(FileRequest::contentType).toList(), | ||
"files" | ||
); | ||
preSignedUrls.addAll(fileUrls); | ||
} | ||
|
||
return PreSignedUrlResponse.of(preSignedUrls); | ||
} | ||
|
||
@Transactional | ||
public PostFileListResponse confirmUpload(Long userId, List<FileUploadConfirmRequest> confirmRequests) { | ||
// 모든 파일의 존재 여부를 비동기로 확인 | ||
List<CompletableFuture<Void>> validationFutures = confirmRequests.stream() | ||
.map(request -> s3PreSignedUrlUtils.doesObjectExistAsync(request.fileUrl()) | ||
.thenAccept(exists -> { | ||
if (!exists) { | ||
throw new S3FileException.FileNotFoundException(FILE_NOT_FOUND); | ||
} | ||
})) | ||
.toList(); | ||
|
||
// 모든 검증 완료 대기 | ||
CompletableFuture.allOf(validationFutures.toArray(new CompletableFuture[0])) | ||
.join(); | ||
|
||
// 이후 기존 로직 수행 | ||
List<PostFile> postFiles = confirmRequests.stream() | ||
.map(request -> PostFile.builder() | ||
.url(request.fileUrl()) | ||
.typeName(request.fileType()) | ||
.build()) | ||
.toList(); | ||
|
||
List<PostFile> afterSaveList = postFileAppender.saveAllPostFile(postFiles); | ||
|
||
String thumbnailUrl = afterSaveList.stream() | ||
.filter(postFile -> postFile.getTypeName().equals("images")) | ||
.min(Comparator.comparing(PostFile::getId)) | ||
.map(PostFile::getUrl) | ||
.orElse(null); | ||
|
||
List<PostFileResponse> postFileResponses = IntStream.range(0, afterSaveList.size()) | ||
.mapToObj(i -> PostFileResponse.of( | ||
afterSaveList.get(i).getId(), | ||
afterSaveList.get(i).getUrl(), | ||
confirmRequests.get(i).originalFileName() | ||
)) | ||
.toList(); | ||
|
||
return PostFileListResponse.of(thumbnailUrl, postFileResponses); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/ussum/homepage/application/image/service/dto/PreSignedUrlInfo.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,36 @@ | ||
package ussum.homepage.application.image.service.dto; | ||
|
||
import lombok.Getter; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Objects; | ||
|
||
@Getter | ||
public class PreSignedUrlInfo { | ||
private final String preSignedUrl; | ||
private final String fileUrl; | ||
private final String originalFileName; | ||
private final LocalDateTime expirationTime; // URL 만료 시간 추가 | ||
|
||
private PreSignedUrlInfo(String preSignedUrl, String fileUrl, String originalFileName, LocalDateTime expirationTime) { | ||
this.preSignedUrl = validateNotBlank(preSignedUrl, "PreSigned URL"); | ||
this.fileUrl = validateNotBlank(fileUrl, "File URL"); | ||
this.originalFileName = validateNotBlank(originalFileName, "Original File Name"); | ||
this.expirationTime = Objects.requireNonNull(expirationTime, "Expiration Time must not be null"); | ||
} | ||
|
||
private String validateNotBlank(String value, String fieldName) { | ||
if (value == null || value.isBlank()) { | ||
throw new IllegalArgumentException(fieldName + "은(는) 필수값입니다."); | ||
} | ||
return value; | ||
} | ||
|
||
public static PreSignedUrlInfo of(String preSignedUrl, String fileUrl, String originalFileName, LocalDateTime expirationTime) { | ||
return new PreSignedUrlInfo(preSignedUrl, fileUrl, originalFileName, expirationTime); | ||
} | ||
|
||
public boolean isExpired() { | ||
return LocalDateTime.now().isAfter(expirationTime); | ||
} | ||
} |
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.
S3PreSignedUrlUtils.java 클래스에
이 메소드를 추가해서 ImageService의 confirmUpload 메소드에서 builder 들어가기전에, 한번 S3 객체 존재 여부를 확인하는건 어떻게 생각하시나여?
S3 객체 존재 여부 확인 메서드 추가
ImageService 클래스에서 검증 적용
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.
그렇네요! doesObjectExist 메서드는 S3 파일 존재 여부를 확인하는 간단한 방식으로 구현되어 있지만, 코멘트 주신 대로 다수의 파일을 처리할 때는 네트워크 요청이 블로킹 방식으로 동작하여 비효율적일 수 있을 것 같네여!!;;
말씀하신 시스템이 작더라도 확장성과 효율성을 고려해 비동기 방식(CompletableFuture)으로 리팩토링하여 네트워크 요청의 병렬 처리를 지원하는 것은 좋은 방식인 것 같습니다!!