-
Notifications
You must be signed in to change notification settings - Fork 0
[Feat/s3-presigned-url] #51
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f868fa2
:hammer: chore : add aws config to application.yml
ekgns33 a2ef798
:heavy_plus_sign: add `aws-sdk` dependency
ekgns33 194fc0d
:sparkles: feat : implement fetching pre-signed url from S3
ekgns33 ca187b4
:heavy_plus_sign: chore : update `aws-sdk` version to `2.20.56` due t…
ekgns33 1be761f
:sparkles: feat : migrate S3 service codes to `aws-sdk:2.20`
ekgns33 fce5061
:bug: fix : fix application.yml path
ekgns33 0945979
:sparkles: feat : change GET method to PUT
ekgns33 887681b
:bug: fix : fix response code fields
ekgns33 473d860
:sparkles: feat : add validation logic, extract functions
ekgns33 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 hidden or 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 hidden or 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,31 @@ | ||
| package org.runimo.runimo.config; | ||
|
|
||
|
|
||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import software.amazon.awssdk.regions.Region; | ||
| import software.amazon.awssdk.services.s3.presigner.S3Presigner; | ||
|
|
||
| @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 S3Presigner amazonS3Client() { | ||
| return S3Presigner.builder() | ||
| .region(Region.of(region)) | ||
| .credentialsProvider( | ||
| () -> software.amazon.awssdk.auth.credentials.AwsBasicCredentials.create(accessKey, secretKey) | ||
| ) | ||
| .build(); | ||
| } | ||
| } |
This file contains hidden or 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
41 changes: 41 additions & 0 deletions
41
src/main/java/org/runimo/runimo/external/ExternalResponseCode.java
This file contains hidden or 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,41 @@ | ||
| package org.runimo.runimo.external; | ||
|
|
||
| import org.runimo.runimo.exceptions.code.CustomResponseCode; | ||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| public enum ExternalResponseCode implements CustomResponseCode { | ||
| PRESIGNED_URL_FETCHED("ESH2011", HttpStatus.CREATED, "Presigned URL 발급 성공", "Presigned URL 발급 성공"), | ||
|
|
||
| PRESIGNED_URL_FETCH_FAILED("ESH4001", HttpStatus.BAD_REQUEST, "Presigned URL 발급 실패", "Presigned URL 발급 실패"); | ||
| private final String code; | ||
| private final HttpStatus httpStatus; | ||
| private final String clientMessage; | ||
| private final String logMessage; | ||
|
|
||
| ExternalResponseCode(String code, HttpStatus httpStatus, String clientMessage, String logMessage) { | ||
| this.code = code; | ||
| this.httpStatus = httpStatus; | ||
| this.clientMessage = clientMessage; | ||
| this.logMessage = logMessage; | ||
| } | ||
|
|
||
| @Override | ||
| public String getCode() { | ||
| return this.code; | ||
| } | ||
|
|
||
| @Override | ||
| public String getClientMessage() { | ||
| return this.clientMessage; | ||
| } | ||
|
|
||
| @Override | ||
| public String getLogMessage() { | ||
| return this.logMessage; | ||
| } | ||
|
|
||
| @Override | ||
| public HttpStatus getHttpStatusCode() { | ||
| return this.httpStatus; | ||
| } | ||
| } | ||
15 changes: 15 additions & 0 deletions
15
src/main/java/org/runimo/runimo/external/ExternalServiceException.java
This file contains hidden or 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,15 @@ | ||
| package org.runimo.runimo.external; | ||
|
|
||
| import org.runimo.runimo.exceptions.BusinessException; | ||
| import org.runimo.runimo.exceptions.code.CustomResponseCode; | ||
|
|
||
| public class ExternalServiceException extends BusinessException { | ||
|
|
||
| protected ExternalServiceException(CustomResponseCode errorCode) { | ||
| super(errorCode); | ||
| } | ||
|
|
||
| public static ExternalServiceException of(CustomResponseCode errorCode) { | ||
| return new ExternalServiceException(errorCode); | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
src/main/java/org/runimo/runimo/external/ImageUploadController.java
This file contains hidden or 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,43 @@ | ||
| package org.runimo.runimo.external; | ||
|
|
||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import java.net.URL; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.runimo.runimo.common.response.SuccessResponse; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @Tag(description = "파일 업로드 관련 API", name = "FILE UPLOAD") | ||
| @RestController | ||
| @RequestMapping("/api/v1/uploads") | ||
| @RequiredArgsConstructor | ||
| public class ImageUploadController { | ||
|
|
||
| private final S3Service s3Service; | ||
|
|
||
| @Operation(summary = "Presigned URL 발급", description = "Presigned URL을 발급합니다.") | ||
| @ApiResponses(value = { | ||
| @ApiResponse(responseCode = "201", description = "Presigned URL 발급"), | ||
| @ApiResponse(responseCode = "400", description = "데이터 입력 오류"), | ||
| @ApiResponse(responseCode = "401", description = "인증 실패"), | ||
| @ApiResponse(responseCode = "500", description = "서버 오류"), | ||
| }) | ||
| @PostMapping | ||
| public ResponseEntity<SuccessResponse<String>> upload( | ||
| @RequestBody ImageUploadRequest request | ||
| ) { | ||
|
ekgns33 marked this conversation as resolved.
|
||
| URL presignedUrl = s3Service.generatePresignedUrl(request.fileName()); | ||
| return ResponseEntity.status(201) | ||
| .body(SuccessResponse.of( | ||
| ExternalResponseCode.PRESIGNED_URL_FETCHED, | ||
| presignedUrl.toExternalForm() | ||
| )); | ||
| } | ||
|
|
||
| } | ||
|
ekgns33 marked this conversation as resolved.
|
||
12 changes: 12 additions & 0 deletions
12
src/main/java/org/runimo/runimo/external/ImageUploadRequest.java
This file contains hidden or 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,12 @@ | ||
| package org.runimo.runimo.external; | ||
|
|
||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import jakarta.validation.constraints.NotEmpty; | ||
|
|
||
| @Schema(description = "이미지 업로드 url발급 요청") | ||
| public record ImageUploadRequest( | ||
| @Schema(description = "파일명") | ||
| @NotEmpty String fileName | ||
| ) { | ||
|
|
||
| } |
This file contains hidden or 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,62 @@ | ||
| package org.runimo.runimo.external; | ||
|
|
||
| import java.net.URL; | ||
| import java.time.Duration; | ||
| import java.util.UUID; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.stereotype.Service; | ||
| import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
| import software.amazon.awssdk.services.s3.presigner.S3Presigner; | ||
| import software.amazon.awssdk.services.s3.presigner.model.PresignedPutObjectRequest; | ||
| import software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class S3Service { | ||
|
|
||
| private final S3Presigner s3Presigner; | ||
| @Value("${cloud.aws.s3.bucket}") | ||
| private String bucketName; | ||
|
|
||
| public URL generatePresignedUrl(String fileName) { | ||
| validateFileName(fileName); | ||
| String objectKey = "uploads/" + UUID.randomUUID() + "_" + sanitizeFileName(fileName); | ||
| try { | ||
| PutObjectPresignRequest presignRequest = PutObjectPresignRequest.builder() | ||
| .signatureDuration(Duration.ofMinutes(10)) // The URL expires in 10 minutes. | ||
| .putObjectRequest(createPutObjectRequest(bucketName, objectKey)) | ||
| .build(); | ||
| PresignedPutObjectRequest presignedRequest = s3Presigner.presignPutObject(presignRequest); | ||
| log.debug("Presigned URL: [{}]", presignedRequest.url().toString()); | ||
| return presignedRequest.url(); | ||
| } catch (Exception e) { | ||
| log.error("Error generating presigned URL: {}", e.getMessage()); | ||
| throw ExternalServiceException.of(ExternalResponseCode.PRESIGNED_URL_FETCH_FAILED); | ||
| } | ||
| } | ||
|
|
||
| private void validateFileName(String fileName) { | ||
| if (fileName == null || fileName.isEmpty()) { | ||
| throw new IllegalArgumentException("파일명이 비어있습니다."); | ||
| } | ||
| if (fileName.length() > 255) { | ||
| throw new IllegalArgumentException("파일명이 너무 깁니다. 최대 255자까지 가능합니다."); | ||
| } | ||
| } | ||
|
|
||
| private String sanitizeFileName(String fileName) { | ||
| String sanitized = fileName.replaceAll("\\.\\.[\\\\/]", ""); | ||
| sanitized = sanitized.replaceAll("[^a-zA-Z0-9._-]", "_"); | ||
| return sanitized; | ||
| } | ||
|
|
||
| private PutObjectRequest createPutObjectRequest(String bucketName, String objectKey) { | ||
| return PutObjectRequest.builder() | ||
| .bucket(bucketName) | ||
| .key(objectKey) | ||
| .build(); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.