-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
154 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
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
bootJar.enabled = false | ||
jar.enabled = true | ||
|
||
dependencies { | ||
// 추가 모듈 | ||
implementation project(":heachi-support:common") | ||
|
||
implementation 'org.springframework.boot:spring-boot-starter-web' // Spring Boot | ||
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE' // AWS S3 | ||
} |
87 changes: 87 additions & 0 deletions
87
heachi-support/aws-s3/src/main/java/com/heachi/s3/api/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,87 @@ | ||
package com.heachi.s3.api.service; | ||
|
||
import com.amazonaws.AmazonClientException; | ||
import com.amazonaws.AmazonServiceException; | ||
import com.amazonaws.SdkClientException; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.DeleteObjectRequest; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.heachi.admin.common.exception.ExceptionMessage; | ||
import com.heachi.admin.common.exception.s3.AwsS3Exception; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class AwsS3Service { | ||
|
||
private final AmazonS3 amazonS3; | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
public String bucket; | ||
|
||
@Value("${cloud.aws.region.static}") | ||
public String region; | ||
|
||
public String uploadImage(MultipartFile multipartFile) { | ||
String url = "https://s3." + region + ".amazonaws.com/" + bucket + "/"; | ||
|
||
// 파일의 크기가 10MB가 넘어가면 Exception 발생 | ||
if (multipartFile.getSize() > 1024 * 1024 * 10) { | ||
log.warn(">>>> 사진의 용량이 10MB를 초과합니다."); | ||
|
||
throw new AwsS3Exception(ExceptionMessage.S3_FILE_SIZE_LIMIT_EXCEEDED); | ||
} | ||
|
||
String fileName = generateFileName((multipartFile.getOriginalFilename())); | ||
|
||
ObjectMetadata objectMetadata = new ObjectMetadata(); | ||
objectMetadata.setContentLength(multipartFile.getSize()); | ||
objectMetadata.setContentType(multipartFile.getContentType()); | ||
|
||
try { | ||
amazonS3.putObject(bucket, fileName, multipartFile.getInputStream(), objectMetadata); | ||
|
||
return url + fileName; | ||
} catch (IOException e) { | ||
log.warn(">>>> AWS S3 파일 업로드에 실패했습니다."); | ||
|
||
throw new AwsS3Exception(ExceptionMessage.S3_FILE_UPLOAD_FAILED); | ||
} | ||
} | ||
|
||
public boolean deleteImage(String fileName) { | ||
try { | ||
amazonS3.deleteObject(new DeleteObjectRequest(bucket, fileName)); | ||
|
||
return true; | ||
} catch (AmazonClientException e) { | ||
log.warn(">>>> 이미지 삭제에 실패했습니다."); | ||
|
||
return false; | ||
} | ||
} | ||
|
||
private String generateFileName(String fileName) { | ||
|
||
return UUID.randomUUID().toString() + getFileExtension(fileName); | ||
} | ||
|
||
private String getFileExtension(String fileName) { | ||
try { | ||
|
||
return fileName.substring(fileName.lastIndexOf(".")); | ||
} catch (StringIndexOutOfBoundsException e) { | ||
log.warn(">>>> 잘못된 확장자명입니다."); | ||
|
||
throw new AwsS3Exception(ExceptionMessage.S3_MALFORMED_FILE); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
heachi-support/aws-s3/src/main/java/com/heachi/s3/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,32 @@ | ||
package com.heachi.s3.config; | ||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3Client; | ||
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 AwsS3Config { | ||
|
||
@Value("${cloud.aws.credentials.accessKey}") | ||
private String accessKey; | ||
|
||
@Value("${cloud.aws.credentials.secretKey}") | ||
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(); | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
heachi-support/common/src/main/java/com/heachi/admin/common/exception/s3/AwsS3Exception.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,10 @@ | ||
package com.heachi.admin.common.exception.s3; | ||
|
||
import com.heachi.admin.common.exception.ExceptionMessage; | ||
import com.heachi.admin.common.exception.HeachiException; | ||
|
||
public class AwsS3Exception extends HeachiException { | ||
public AwsS3Exception(ExceptionMessage message) { | ||
super(message.getText()); | ||
} | ||
} |
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