Skip to content

Commit

Permalink
setting(#33): aws s3 module
Browse files Browse the repository at this point in the history
setting(#33): aws s3 module
  • Loading branch information
ghdcksgml1 authored Oct 3, 2023
2 parents 5a647b1 + 4cfd136 commit db6d0c6
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 3 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/CI_dev_pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@ jobs:
mkdir -p ./heachi-core/auth-api/src/main/resources
mkdir -p ./heachi-domain-mongo/src/main/resources
mkdir -p ./heachi-domain-mysql/src/main/resources
mkdir -p ./heachi-domain-redis/src/main/resources
mkdir -p ./heachi-notify/src/main/resources
mkdir -p ./heachi-support/common/src/main/resources
mkdir -p ./heachi-support/external-clients/src/main/resources
mkdir -p ./heachi-support/aws-s3/src/main/resources
- name: yml 파일 생성
run: |
run: |
echo "${{ secrets.HEACHICORE_AUTHAPI_APPLICATION }}" > ./heachi-core/auth-api/src/main/resources/application.yml
echo "${{ secrets.HEACHIDOMAINMONGO_APPLICATION }}" > ./heachi-domain-mongo/src/main/resources/application.yml
echo "${{ secrets.HEACHIDOMAINMYSQL_DATABASE }}" > ./heachi-domain-mysql/src/main/resources/database.yml
echo "${{ secrets.HEACHIDOMAINREDIS_DATABASE }}" > ./heachi-domain-redis/src/main/resources/redis.yml
echo "${{ secrets.HEACHINOTIFY_APPLICATION }}" > ./heachi-notify/src/main/resources/application.yml
echo "${{ secrets.HEACHISUPPORT_COMMON_COMMON }}" > ./heachi-support/common/src/main/resources/common.yml
echo "${{ secrets.HEACHISUPPORT_EXTERNALCLIENTS_APPLICATION }}" > ./heachi-support/external-clients/src/main/resources/application.yml
echo "${{ secrets.HEACHISUPPORT_AWS_S3 }}" > ./heachi-support/aws-s3/src/main/resources/s3.yml
# docker Build에 필요한 gradle.properties 설정
- name: gradle.properties 생성
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/CI_main_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
mkdir -p ./heachi-notify/src/main/resources
mkdir -p ./heachi-support/common/src/main/resources
mkdir -p ./heachi-support/external-clients/src/main/resources
mkdir -p ./heachi-support/aws-s3/src/main/resources
- name: yml 파일 생성
Expand All @@ -38,6 +39,7 @@ jobs:
echo "${{ secrets.HEACHINOTIFY_APPLICATION }}" > ./heachi-notify/src/main/resources/application.yml
echo "${{ secrets.HEACHISUPPORT_COMMON_COMMON }}" > ./heachi-support/common/src/main/resources/common.yml
echo "${{ secrets.HEACHISUPPORT_EXTERNALCLIENTS_APPLICATION }}" > ./heachi-support/external-clients/src/main/resources/application.yml
echo "${{ secrets.HEACHISUPPORT_AWS_S3 }}" > ./heachi-support/aws-s3/src/main/resources/s3.yml
# docker Build에 필요한 gradle.properties 설정
- name: gradle.properties 생성
Expand Down
2 changes: 1 addition & 1 deletion heachi-core/auth-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dependencies {

implementation 'org.springframework.boot:spring-boot-starter-web' // Spring Web
implementation 'org.springframework.boot:spring-boot-starter-validation' // Bean Validation
api 'org.springframework.boot:spring-boot-starter-security' // Spring Security
implementation 'org.springframework.boot:spring-boot-starter-security' // Spring Security

// jwt 라이브러리 추가
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
Expand Down
10 changes: 10 additions & 0 deletions heachi-support/aws-s3/build.gradle
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
}
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);
}
}
}
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public enum ExceptionMessage {
// NotifyException
NOTIFY_NOT_FOUND("해당 아이디의 알림을 찾을 수 없습니다."),
NOTIFY_DUPLICATE_ID("SendUser와 ReceiveUser가 같습니다."),

// AwsS3Exception
S3_FILE_SIZE_LIMIT_EXCEEDED("파일 크기가 너무 큽니다."),
S3_FILE_UPLOAD_FAILED("파일을 업로드하는데 실패했습니다."),
S3_MALFORMED_FILE("잘못된 파일 확장자 입니다."),
;

private final String text;
Expand Down
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());
}
}
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ include(
"heachi-notify",
"heachi-support:common",
"heachi-support:logging",
"heachi-support:external-clients"
"heachi-support:external-clients",
"heachi-support:aws-s3"
)

0 comments on commit db6d0c6

Please sign in to comment.