diff --git a/module-api/src/main/java/com/mile/controller/TestController.java b/module-api/src/main/java/com/mile/controller/TestController.java deleted file mode 100644 index 0deb853d..00000000 --- a/module-api/src/main/java/com/mile/controller/TestController.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.mile.controller; - -import com.mile.aws.utils.PreSignedUrlResponse; -import com.mile.aws.utils.S3BucketDirectory; -import com.mile.aws.utils.S3Service; -import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.GetMapping; -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; -import org.springframework.web.multipart.MultipartFile; - -import java.io.IOException; - -@RestController -@RequestMapping("/api/test") -@RequiredArgsConstructor -public class TestController { - private final S3Service s3Service; - - @PostMapping() - public void testImage(@RequestBody MultipartFile image) throws IOException { - s3Service.uploadImage(S3BucketDirectory.TEST_PREFIX, image); - } - - @GetMapping - public PreSignedUrlResponse getPresignedUrl() { - return s3Service.getUploadPreSignedUrl(S3BucketDirectory.TEST_PREFIX); - } -} diff --git a/module-api/src/main/java/com/mile/controller/external/S3Controller.java b/module-api/src/main/java/com/mile/controller/external/S3Controller.java new file mode 100644 index 00000000..02858692 --- /dev/null +++ b/module-api/src/main/java/com/mile/controller/external/S3Controller.java @@ -0,0 +1,24 @@ +package com.mile.controller.external; + +import com.mile.aws.utils.PreSignedUrlResponse; +import com.mile.aws.utils.S3BucketDirectory; +import com.mile.aws.utils.S3Service; +import com.mile.dto.SuccessResponse; +import com.mile.exception.message.SuccessMessage; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api") +@RequiredArgsConstructor +public class S3Controller implements S3ControllerSwagger { + private final S3Service s3Service; + + @GetMapping("/image/upload") + @Override + public SuccessResponse getPreSignedUrl() { + return SuccessResponse.of(SuccessMessage.PRESIGNED_URL_GET_SUCCESS, s3Service.getUploadPreSignedUrl(S3BucketDirectory.POST_PREFIX)); + } +} diff --git a/module-api/src/main/java/com/mile/controller/external/S3ControllerSwagger.java b/module-api/src/main/java/com/mile/controller/external/S3ControllerSwagger.java new file mode 100644 index 00000000..28f16503 --- /dev/null +++ b/module-api/src/main/java/com/mile/controller/external/S3ControllerSwagger.java @@ -0,0 +1,21 @@ +package com.mile.controller.external; + +import com.mile.aws.utils.PreSignedUrlResponse; +import com.mile.dto.SuccessResponse; +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; + +@Tag(name = "Image - PreSigned Url", description = "이미지 업로드 할 Url 받기") +public interface S3ControllerSwagger { + + @Operation(summary = "이미지 업로드 presigned url") + @ApiResponses( + value = { + @ApiResponse(responseCode = "200", description = "이미지를 업로드할 url이 발행되었습니다."), + @ApiResponse(responseCode = "500", description = "S3 PRESIGNED URL을 받아오기에 실패했습니다.") + } + ) + SuccessResponse getPreSignedUrl(); +} diff --git a/module-common/src/main/java/com/mile/exception/message/ErrorMessage.java b/module-common/src/main/java/com/mile/exception/message/ErrorMessage.java index b7d60f6e..37ff82a8 100644 --- a/module-common/src/main/java/com/mile/exception/message/ErrorMessage.java +++ b/module-common/src/main/java/com/mile/exception/message/ErrorMessage.java @@ -58,7 +58,8 @@ public enum ErrorMessage { /* Internal Server Error */ - IMAGE_UPLOAD_ERROR(HttpStatus.INTERNAL_SERVER_ERROR.value(), "S3 버킷에 이미지를 업로드하는 데 실패했습니다."), + IMAGE_UPLOAD_ERROR(HttpStatus.INTERNAL_SERVER_ERROR.value(), "S3 버킷에 이미지를 업로드에 실패했습니다."), + PRESIGNED_URL_GET_ERROR(HttpStatus.INTERNAL_SERVER_ERROR.value(), "S3 PRESIGNED URL을 받아오기에 실패했습니다."), IMAGE_DELETE_ERROR(HttpStatus.INTERNAL_SERVER_ERROR.value(), "S3 버킷으로부터 이미지를 삭제하는 데 실패했습니다."), INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR.value(), "서버 내부 오류입니다."); diff --git a/module-common/src/main/java/com/mile/exception/message/SuccessMessage.java b/module-common/src/main/java/com/mile/exception/message/SuccessMessage.java index 0c3257b1..2664f868 100644 --- a/module-common/src/main/java/com/mile/exception/message/SuccessMessage.java +++ b/module-common/src/main/java/com/mile/exception/message/SuccessMessage.java @@ -19,7 +19,9 @@ public enum SuccessMessage { CURIOUS_DELETE_SUCCESS(HttpStatus.OK.value(), "궁금해요 삭제가 완료되었습니다."), WRITER_AUTHENTIACTE_SUCCESS(HttpStatus.OK.value(), "게시글 권한이 확인되었습니다."), POST_PUT_SUCCESS(HttpStatus.OK.value(), "글 수정이 완료되었습니다."), + PRESIGNED_URL_GET_SUCCESS(HttpStatus.OK.value(), "이미지를 업로드할 url이 발행되었습니다."), POST_DELETE_SUCCESS(HttpStatus.OK.value(), "글 삭제가 완료되었습니다."), + /* 201 CREATED */ diff --git a/module-external/src/main/java/com/mile/aws/utils/S3Service.java b/module-external/src/main/java/com/mile/aws/utils/S3Service.java index 90f46b0b..5ef9259f 100644 --- a/module-external/src/main/java/com/mile/aws/utils/S3Service.java +++ b/module-external/src/main/java/com/mile/aws/utils/S3Service.java @@ -84,7 +84,7 @@ public PreSignedUrlResponse getUploadPreSignedUrl(final S3BucketDirectory prefix String url = preSigner.presignPutObject(preSignedUrlRequest).url().toString(); return PreSignedUrlResponse.of(fileName, url); } catch (RuntimeException e) { - throw new MileException(ErrorMessage.IMAGE_UPLOAD_ERROR); + throw new MileException(ErrorMessage.PRESIGNED_URL_GET_ERROR); } }