-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
284 additions
and
5 deletions.
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
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
14 changes: 14 additions & 0 deletions
14
src/main/java/com/example/hackathon/global/exception/BaseErrorException.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 com.example.hackathon.global.exception; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class BaseErrorException extends RuntimeException { | ||
|
||
private final int ErrorCode; | ||
|
||
public BaseErrorException(int errorCode, String message) { | ||
super(message); | ||
ErrorCode = errorCode; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/com/example/hackathon/global/exception/GlobalExceptionHandler.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,73 @@ | ||
package com.example.hackathon.global.exception; | ||
|
||
|
||
import com.example.hackathon.global.exception.Response.ExceptionResponse; | ||
import java.io.UnsupportedEncodingException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
// 로그 형식 | ||
private static final String LOG_FORMAT = "Class : {}, Code : {}, Message : {}"; | ||
private static final int ERROR_CODE = 400; | ||
private static final int SERVER_ERROR_CODE = 500; | ||
|
||
// 사용자 정의 예외 처리 | ||
@ExceptionHandler(BaseErrorException.class) | ||
public ResponseEntity<ExceptionResponse<Void>> handle(BaseErrorException e) { | ||
|
||
logWarning(e, e.getErrorCode()); | ||
ExceptionResponse<Void> response = ExceptionResponse.fail(e.getErrorCode(), e.getMessage()); | ||
|
||
return ResponseEntity | ||
.status(e.getErrorCode()) | ||
.body(response); | ||
} | ||
|
||
// @Valid 예외 처리 (@NotNull, @Size, etc...) or IllegalArgumentException | ||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public ResponseEntity<ExceptionResponse<Void>> handle(MethodArgumentNotValidException e) { | ||
|
||
logWarning(e, ERROR_CODE); | ||
ExceptionResponse<Void> response = ExceptionResponse.fail(ERROR_CODE, e.getBindingResult().getAllErrors().get(0).getDefaultMessage()); | ||
|
||
return ResponseEntity | ||
.status(ERROR_CODE) | ||
.body(response); | ||
} | ||
|
||
@ExceptionHandler({UnsupportedEncodingException.class}) | ||
public ResponseEntity<ExceptionResponse<Void>> handle(UnsupportedEncodingException e) { | ||
|
||
|
||
logWarning(e, ERROR_CODE); | ||
ExceptionResponse<Void> response = ExceptionResponse.fail(ERROR_CODE, e.getMessage()); | ||
|
||
return ResponseEntity | ||
.status(ERROR_CODE) | ||
.body(response); | ||
} | ||
|
||
// 서버 측 에러 (이외의 에러) | ||
@ExceptionHandler(Exception.class) | ||
public ResponseEntity<ExceptionResponse<Void>> handle(Exception e) { | ||
|
||
logWarning(e, SERVER_ERROR_CODE); | ||
ExceptionResponse<Void> response = ExceptionResponse.fail(SERVER_ERROR_CODE, e.getMessage()); | ||
|
||
return ResponseEntity | ||
.status(SERVER_ERROR_CODE) | ||
.body(response); } | ||
|
||
// log.warn이 중복되어 리팩토링 | ||
private void logWarning(Exception e, int errorCode) { | ||
log.warn(e.getMessage(), e); // 전체 로그 출력, 운영 단계에서는 삭제 | ||
log.warn(LOG_FORMAT, e.getClass().getSimpleName(), errorCode, e.getMessage()); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/example/hackathon/global/exception/Response/ExceptionResponse.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,29 @@ | ||
package com.example.hackathon.global.exception.Response; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class ExceptionResponse<T> { | ||
|
||
private int code; | ||
private String message; | ||
private T data; | ||
|
||
private ExceptionResponse(int code, String message, T data) { | ||
this.code = code; | ||
this.message = message; | ||
this.data = data; | ||
} | ||
|
||
private ExceptionResponse(int code, String message) { | ||
this.code = code; | ||
this.message = message; | ||
} | ||
|
||
public static <T> ExceptionResponse<T> fail(int code, String message) { | ||
|
||
return new ExceptionResponse<>(code, message); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/example/hackathon/global/s3/config/S3Config.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,35 @@ | ||
package com.example.hackathon.global.s3.config; | ||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@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 AmazonS3 amazonS3Client(){ | ||
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); | ||
|
||
return AmazonS3ClientBuilder | ||
.standard() | ||
.withRegion(region) | ||
.withCredentials(new AWSStaticCredentialsProvider(credentials)) | ||
.build(); | ||
} | ||
|
||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/example/hackathon/global/s3/exception/S3ErrorException.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,18 @@ | ||
package com.example.hackathon.global.s3.exception; | ||
|
||
import com.example.hackathon.global.exception.BaseErrorException; | ||
|
||
public abstract class S3ErrorException { | ||
|
||
public static class S3UploadFailException extends BaseErrorException { | ||
public S3UploadFailException() { | ||
super(S3ErrorMessage.UPLOAD_FAIL.getErrorCode(), S3ErrorMessage.UPLOAD_FAIL.getMessage()); | ||
} | ||
} | ||
|
||
public static class S3NotExistNameException extends BaseErrorException { | ||
public S3NotExistNameException() { | ||
super(S3ErrorMessage.NOT_EXIST_NAME.getErrorCode(), S3ErrorMessage.NOT_EXIST_NAME.getMessage()); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/example/hackathon/global/s3/exception/S3ErrorMessage.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,19 @@ | ||
package com.example.hackathon.global.s3.exception; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum S3ErrorMessage { | ||
|
||
UPLOAD_FAIL(400, "사진 업로드에 실패하였습니다."), | ||
NOT_EXIST_NAME(404, "존재하지 않는 파일 이름입니다."); | ||
|
||
private final int errorCode; | ||
private final String message; | ||
|
||
S3ErrorMessage(int errorCode, String message) { | ||
this.errorCode = errorCode; | ||
this.message = message; | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
src/main/java/com/example/hackathon/global/s3/service/S3Service.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,74 @@ | ||
package com.example.hackathon.global.s3.service; | ||
|
||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import com.example.hackathon.global.s3.exception.S3ErrorException.S3NotExistNameException; | ||
import com.example.hackathon.global.s3.exception.S3ErrorException.S3UploadFailException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@Service | ||
public class S3Service { | ||
|
||
private final AmazonS3 s3Client; | ||
private final String bucketName; | ||
|
||
public S3Service(AmazonS3 s3Client, @Value("${bucket_name}") String bucketName) { | ||
this.s3Client = s3Client; | ||
this.bucketName = bucketName; | ||
} | ||
|
||
public URL uploadImages(MultipartFile file) { | ||
String key = file.getOriginalFilename(); | ||
checkExistFile(key); | ||
// MultipartFile에서 InputStream을 얻어 S3에 업로드합니다. | ||
try (InputStream inputStream = file.getInputStream()) { | ||
ObjectMetadata metadata = setMetaData(file); | ||
|
||
// PutObjectRequest 생성 시 InputStream과 ContentType을 설정합니다. | ||
PutObjectRequest putRequest = new PutObjectRequest(bucketName, key, inputStream, metadata) | ||
.withCannedAcl(CannedAccessControlList.PublicRead); | ||
s3Client.putObject(putRequest); | ||
return getImageUrl(key); | ||
} catch (IOException e) { | ||
throw new S3UploadFailException(); | ||
} | ||
} | ||
|
||
public URL getImageUrl(String key) { | ||
try { | ||
return s3Client.getUrl(bucketName, key); | ||
} catch (Exception e) { | ||
throw new S3NotExistNameException(); | ||
} | ||
} | ||
|
||
public void deleteImage(String key) { | ||
try { | ||
s3Client.deleteObject(bucketName, key); | ||
} catch (Exception e) { | ||
throw new S3NotExistNameException(); | ||
} | ||
} | ||
|
||
private ObjectMetadata setMetaData(MultipartFile file) { | ||
ObjectMetadata metadata = new ObjectMetadata(); | ||
metadata.setContentLength(file.getSize()); | ||
metadata.setContentType(file.getContentType()); | ||
|
||
return metadata; | ||
} | ||
|
||
private void checkExistFile(String key) { | ||
if (s3Client.doesObjectExist(bucketName, key)) { | ||
s3Client.deleteObject(bucketName, key); | ||
} | ||
} | ||
|
||
} |
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