-
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
10 changed files
with
218 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,5 @@ out/ | |
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
common-module/src/main/resources/application-s3.yml |
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
35 changes: 35 additions & 0 deletions
35
common-module/src/main/java/com/likelion/commonmodule/image/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,35 @@ | ||
package com.likelion.commonmodule.image.config; | ||
|
||
import com.amazonaws.auth.AWSCredentials; | ||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class AwsS3Config { | ||
|
||
@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 generateS3client() { | ||
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey); | ||
return AmazonS3ClientBuilder.standard() | ||
.withRegion(region) | ||
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) | ||
.build(); | ||
|
||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...n-module/src/main/java/com/likelion/commonmodule/image/exception/FileDeleteException.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,13 @@ | ||
package com.likelion.commonmodule.image.exception; | ||
|
||
import com.likelion.commonmodule.exception.common.BaseErrorCode; | ||
|
||
public class FileDeleteException extends ImageException { | ||
public FileDeleteException(BaseErrorCode errorCode) { | ||
super(errorCode); | ||
} | ||
|
||
public FileDeleteException(BaseErrorCode errorCode, Throwable cause) { | ||
super(errorCode, cause); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...odule/src/main/java/com/likelion/commonmodule/image/exception/FileExtensionException.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,13 @@ | ||
package com.likelion.commonmodule.image.exception; | ||
|
||
import com.likelion.commonmodule.exception.common.BaseErrorCode; | ||
|
||
public class FileExtensionException extends ImageException { | ||
public FileExtensionException(BaseErrorCode errorCode) { | ||
super(errorCode); | ||
} | ||
|
||
public FileExtensionException(BaseErrorCode errorCode, Throwable cause) { | ||
super(errorCode, cause); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...n-module/src/main/java/com/likelion/commonmodule/image/exception/FileUploadException.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,13 @@ | ||
package com.likelion.commonmodule.image.exception; | ||
|
||
import com.likelion.commonmodule.exception.common.BaseErrorCode; | ||
|
||
public class FileUploadException extends ImageException { | ||
public FileUploadException(BaseErrorCode errorCode) { | ||
super(errorCode); | ||
} | ||
|
||
public FileUploadException(BaseErrorCode errorCode, Throwable cause) { | ||
super(errorCode, cause); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
common-module/src/main/java/com/likelion/commonmodule/image/exception/ImageErrorCode.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,27 @@ | ||
package com.likelion.commonmodule.image.exception; | ||
|
||
import com.likelion.commonmodule.exception.common.ApiResponse; | ||
import com.likelion.commonmodule.exception.common.BaseErrorCode; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum ImageErrorCode implements BaseErrorCode { | ||
|
||
FILE_UPLOAD_FAIL(HttpStatus.INTERNAL_SERVER_ERROR, "3000", "파일 업로드에 실패했습니다."), | ||
FILE_DELETE_FAIL(HttpStatus.INTERNAL_SERVER_ERROR, "3000", "파일 삭제에 실패했습니다."), | ||
WRONG_FILE_FORMAT(HttpStatus.INTERNAL_SERVER_ERROR, "3000", "파일 타입이 올바르지 않습니다."), | ||
; | ||
|
||
private final HttpStatus httpStatus; | ||
private final String code; | ||
private final String message; | ||
|
||
|
||
@Override | ||
public ApiResponse<Void> getErrorResponse() { | ||
return null; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
common-module/src/main/java/com/likelion/commonmodule/image/exception/ImageException.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,22 @@ | ||
package com.likelion.commonmodule.image.exception; | ||
|
||
import com.likelion.commonmodule.exception.common.BaseErrorCode; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class ImageException extends RuntimeException { | ||
|
||
private final BaseErrorCode errorCode; | ||
|
||
private final Throwable cause; | ||
|
||
public ImageException(BaseErrorCode errorCode) { | ||
this.errorCode = errorCode; | ||
this.cause = null; | ||
} | ||
|
||
public ImageException(BaseErrorCode errorCode, Throwable cause) { | ||
this.errorCode = errorCode; | ||
this.cause = cause; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
common-module/src/main/java/com/likelion/commonmodule/image/service/AwsS3Service.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,83 @@ | ||
package com.likelion.commonmodule.image.service; | ||
|
||
import com.amazonaws.AmazonServiceException; | ||
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.likelion.commonmodule.image.exception.FileDeleteException; | ||
import com.likelion.commonmodule.image.exception.FileExtensionException; | ||
import com.likelion.commonmodule.image.exception.FileUploadException; | ||
import com.likelion.commonmodule.image.exception.ImageErrorCode; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AwsS3Service { | ||
|
||
private final AmazonS3 amazonS3; | ||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucketName; | ||
|
||
/** | ||
* file upload | ||
*/ | ||
public String uploadFile(MultipartFile multipartFile) { | ||
|
||
if (Objects.isNull(multipartFile)) return null; | ||
if (multipartFile.isEmpty()) return null; | ||
|
||
String fileName = createFileName(multipartFile.getOriginalFilename()); | ||
|
||
ObjectMetadata objectMetadata = new ObjectMetadata(); | ||
objectMetadata.setContentType(multipartFile.getContentType()); | ||
|
||
try (InputStream inputStream = multipartFile.getInputStream()) { | ||
amazonS3.putObject(new PutObjectRequest(bucketName, fileName, inputStream, objectMetadata) | ||
.withCannedAcl(CannedAccessControlList.PublicRead)); | ||
} catch (IOException e) { | ||
throw new FileUploadException(ImageErrorCode.FILE_UPLOAD_FAIL); | ||
} | ||
|
||
return amazonS3.getUrl(bucketName, fileName).toString(); | ||
} | ||
|
||
/** | ||
* 파일 삭제 메서드 | ||
*/ | ||
public void deleteFile(String fileUrl) { | ||
if (fileUrl == null) return; | ||
try { | ||
amazonS3.deleteObject(bucketName, fileUrl); | ||
} catch (AmazonServiceException e) { | ||
throw new FileDeleteException(ImageErrorCode.FILE_DELETE_FAIL); | ||
} | ||
} | ||
|
||
/** | ||
* 파일 업로드 시에 파일명을 난수화하는 메서드 | ||
*/ | ||
private String createFileName(String fileName) { | ||
return UUID.randomUUID().toString().concat(getFileExtension(fileName)); | ||
} | ||
|
||
/** | ||
* 파일 확장자 가져오는 메서드 | ||
*/ | ||
private String getFileExtension(String fileName) { | ||
try { | ||
return fileName.substring(fileName.lastIndexOf(".")); | ||
} catch (StringIndexOutOfBoundsException e) { | ||
throw new FileExtensionException(ImageErrorCode.WRONG_FILE_FORMAT); | ||
} | ||
} | ||
|
||
} |