-
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.
Merge pull request #12 from 6QuizOnTheBlock/setting
Feature/seeting 10, 11 GlobalException Setting & S3 Setting
- Loading branch information
Showing
8 changed files
with
202 additions
and
1 deletion.
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
40 changes: 40 additions & 0 deletions
40
backEnd/src/main/java/com/quiz/ourclass/global/config/AWSS3Config.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,40 @@ | ||
package com.quiz.ourclass.global.config; | ||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import com.quiz.ourclass.global.util.AwsS3ObjectStorage; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class AWSS3Config { | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
@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 AmazonS3Client amazonS3Client() { | ||
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey); | ||
return (AmazonS3Client) AmazonS3ClientBuilder.standard() | ||
.withRegion(region) | ||
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public AwsS3ObjectStorage awsS3ObjectStorageUpload(AmazonS3 amazonS3) { | ||
AwsS3ObjectStorage awsS3ObjectStorageUpload = new AwsS3ObjectStorage(amazonS3); | ||
awsS3ObjectStorageUpload.setBucket(bucket); | ||
return awsS3ObjectStorageUpload; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
backEnd/src/main/java/com/quiz/ourclass/global/dto/ApiResponse.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,21 @@ | ||
package com.quiz.ourclass.global.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class ApiResponse<T> { | ||
|
||
private String status; | ||
private String message; | ||
private T data; | ||
|
||
public static <T> ApiResponse<T> success(T data) { | ||
return new ApiResponse<>("success", "์ฑ๊ณตใ ", data); | ||
} | ||
|
||
public static <T> ApiResponse<T> fail(String message) { | ||
return new ApiResponse<>("fail", message, null); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
backEnd/src/main/java/com/quiz/ourclass/global/exception/ErrorCode.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,17 @@ | ||
package com.quiz.ourclass.global.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum ErrorCode { | ||
//domain | ||
FILE_NOT_FOUND(HttpStatus.NOT_FOUND, "ํ์ผ์ด ์กด์ฌํ์ง ์์ต๋๋ค."), | ||
AWS_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "AWS ์๋ฒ ์๋ฌ์ ๋๋ค."); | ||
|
||
|
||
private final HttpStatus status; | ||
private final String message; | ||
} |
17 changes: 17 additions & 0 deletions
17
backEnd/src/main/java/com/quiz/ourclass/global/exception/GlobalException.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,17 @@ | ||
package com.quiz.ourclass.global.exception; | ||
|
||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public class GlobalException extends RuntimeException { | ||
|
||
private final ErrorCode errorCode; | ||
private final HttpStatus status; | ||
|
||
public GlobalException(ErrorCode errorCode) { | ||
super(errorCode.getMessage()); | ||
this.status = errorCode.getStatus(); | ||
this.errorCode = errorCode; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
backEnd/src/main/java/com/quiz/ourclass/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,28 @@ | ||
package com.quiz.ourclass.global.exception; | ||
|
||
import com.quiz.ourclass.global.dto.ApiResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@RestControllerAdvice | ||
@RequiredArgsConstructor | ||
public class GlobalExceptionHandler { | ||
|
||
private static final HttpHeaders jsonHeaders; | ||
|
||
static { | ||
jsonHeaders = new HttpHeaders(); | ||
jsonHeaders.add(HttpHeaders.CONTENT_TYPE, "application/json"); | ||
} | ||
|
||
@ExceptionHandler(GlobalException.class) | ||
public ResponseEntity<ApiResponse<Object>> handleGlobalException( | ||
GlobalException globalException) { | ||
ApiResponse<Object> response = ApiResponse.fail( | ||
globalException.getErrorCode().getMessage()); | ||
return new ResponseEntity<>(response, jsonHeaders, globalException.getStatus()); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
backEnd/src/main/java/com/quiz/ourclass/global/util/AwsS3ObjectStorage.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 com.quiz.ourclass.global.util; | ||
|
||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.quiz.ourclass.global.exception.ErrorCode; | ||
import com.quiz.ourclass.global.exception.GlobalException; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.UUID; | ||
import lombok.Data; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@Log4j2 | ||
@Data | ||
public class AwsS3ObjectStorage { | ||
|
||
private AmazonS3 amazonS3; //AmazonS3 config ๋ฏธ๋ฆฌ ๋น ์ฃผ์ | ||
private String bucket; //๋น ์ฃผ์ ์ setter | ||
private String aiS3Url; //๋น ์ฃผ์ ์ setter | ||
|
||
public AwsS3ObjectStorage(AmazonS3 amazonS3) { | ||
this.amazonS3 = amazonS3; | ||
} | ||
|
||
public String uploadFile(MultipartFile multipartFile) throws IOException { | ||
// UUID ์ด์ฉํด ๊ณ ์ ํ ํ์ผ๋ช ์์ฑ | ||
String originalFileName = multipartFile.getOriginalFilename(); | ||
String fileName = UUID.randomUUID() + "_" + originalFileName; | ||
|
||
ObjectMetadata metadata = new ObjectMetadata(); | ||
metadata.setContentLength(multipartFile.getSize()); | ||
metadata.setContentType(multipartFile.getContentType()); | ||
|
||
amazonS3.putObject(bucket, fileName, multipartFile.getInputStream(), metadata); | ||
return amazonS3.getUrl(bucket, fileName).toString(); | ||
} | ||
|
||
public int deleteFile(String fileUrl) { | ||
try { | ||
// URL์์ ๊ฐ์ฒด ํค ์ถ์ถ | ||
URL url = new URL(fileUrl); | ||
// URL์ ์ฒซ ๋ฒ์งธ '/'๋ฅผ ์ ๊ฑฐํ์ฌ ๊ฐ์ฒด ํค ์ป๊ธฐ | ||
String key = url.getPath().substring(1); | ||
|
||
// ํ์ผ ์กด์ฌ ์ฌ๋ถ ํ์ธ | ||
if (amazonS3.doesObjectExist(bucket, key)) { | ||
// S3์์ ํ์ผ ์ญ์ | ||
amazonS3.deleteObject(bucket, key); | ||
log.info("File deleted successfully: {}", key); | ||
return 1; | ||
} else { // file not found | ||
log.warn("File not found: {}", key); | ||
throw new GlobalException(ErrorCode.FILE_NOT_FOUND); | ||
} | ||
} catch (Exception e) { //error | ||
log.error("Failed to delete file!: {}", fileUrl, e); | ||
throw new GlobalException(ErrorCode.AWS_SERVER_ERROR); | ||
} | ||
} | ||
} |
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