Skip to content

Commit

Permalink
Merge pull request #12 from 6QuizOnTheBlock/setting
Browse files Browse the repository at this point in the history
Feature/seeting 10, 11 GlobalException Setting & S3 Setting
  • Loading branch information
HABINOH authored Apr 25, 2024
2 parents f24cdf9 + 15af6c4 commit 0cb82bd
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 1 deletion.
3 changes: 3 additions & 0 deletions backEnd/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ dependencies {
runtimeOnly 'com.mysql:mysql-connector-j'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'

// aws
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'

//querydsl
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
annotationProcessor "com.querydsl:querydsl-apt:5.0.0:jakarta"
Expand Down
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;
}
}
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);
}
}
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;
}
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;
}
}
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());
}
}
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);
}
}
}
16 changes: 15 additions & 1 deletion backEnd/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,18 @@ springdoc:
path: /swagger-ui.html
tags-sorter: alpha # alpha: ์•ŒํŒŒ๋ฒณ ์ˆœ ํƒœ๊ทธ ์ •๋ ฌ, method: HTTP Method ์ˆœ ์ •๋ ฌ
operations-sorter: alpha # alpha: ์•ŒํŒŒ๋ฒณ ์ˆœ ํƒœ๊ทธ ์ •๋ ฌ, method: HTTP Method ์ˆœ ์ •๋ ฌ
display-request-duration: true
display-request-duration: true

#aws s3 bucket
cloud:
aws:
s3:
bucket: ulvanbucket
credentials:
access-key: AKIAQSCG2TLXCKNPB5FO
secret-key: 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
region:
static: ap-northeast-2
auto: false
stack:
auto: false

0 comments on commit 0cb82bd

Please sign in to comment.