기존 이미지 업로드 서버를 깃헙 레포로 쓰던걸 aws s3로 스택 마이그레이션, 전략패턴을 사용해 유연한 확장 실천
This commit is contained in:
@@ -54,6 +54,8 @@ dependencies {
|
||||
implementation 'org.commonmark:commonmark-ext-gfm-tables:0.18.0'
|
||||
implementation group: 'org.jdom', name: 'jdom2', version: '2.0.6'
|
||||
implementation group: 'net.sf.ehcache', name: 'ehcache', version: '2.10.9.2'
|
||||
implementation group: 'com.amazonaws', name: 'aws-java-sdk-s3', version: '1.12.152'
|
||||
|
||||
|
||||
compileOnly 'org.projectlombok:lombok'
|
||||
developmentOnly 'org.springframework.boot:spring-boot-devtools'
|
||||
|
||||
@@ -1,13 +1,25 @@
|
||||
package myblog.blog.base.config;
|
||||
|
||||
import com.amazonaws.auth.AWSCredentials;
|
||||
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
||||
import com.amazonaws.auth.BasicAWSCredentials;
|
||||
import com.amazonaws.regions.Regions;
|
||||
import com.amazonaws.services.s3.AmazonS3;
|
||||
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
||||
import com.google.gson.Gson;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class AppConfig {
|
||||
|
||||
@Value("${cloud.aws.credentials.accessKey}")
|
||||
private String AWS_ACCESS_KEY_ID;
|
||||
@Value("${cloud.aws.credentials.secretKey}")
|
||||
private String AWS_SECRET_ACCESS_KEY;
|
||||
|
||||
/*
|
||||
- DTO <-> 엔티티 매퍼 빈등록
|
||||
*/
|
||||
@@ -19,4 +31,17 @@ public class AppConfig {
|
||||
.setFieldMatchingEnabled(true);
|
||||
return modelMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* AWS S3 설정
|
||||
*/
|
||||
@Bean
|
||||
public AmazonS3 S3() {
|
||||
AWSCredentials awsCredentials =
|
||||
new BasicAWSCredentials(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY);
|
||||
return AmazonS3ClientBuilder
|
||||
.standard()
|
||||
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).withRegion(Regions.AP_NORTHEAST_2)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package myblog.blog.imgupload.service;
|
||||
|
||||
import com.amazonaws.services.s3.AmazonS3;
|
||||
import com.amazonaws.services.s3.model.*;
|
||||
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;
|
||||
|
||||
/**
|
||||
* S3 이미지 업로드 전략 구현체
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class AwsS3ImgUploadStrategy implements ImgUploadStrategy {
|
||||
|
||||
private final AmazonS3 amazonS3;
|
||||
@Value("${cloud.aws.s3.bucket}")
|
||||
private String bucket;
|
||||
|
||||
@Override
|
||||
public String uploadFile(MultipartFile file, String storeFileName) {
|
||||
ObjectMetadata metadata = createObjectMetadata(file);
|
||||
try(InputStream inputStream = file.getInputStream()) {
|
||||
amazonS3.putObject(new PutObjectRequest(bucket, storeFileName, inputStream, metadata)
|
||||
.withCannedAcl(CannedAccessControlList.PublicRead));
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("파일 업로드에 실패했습니다.");
|
||||
}
|
||||
return amazonS3.getUrl(bucket, storeFileName).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* s3 api를 위한 dto 생성 메서드
|
||||
* @param file 업로드 요청된 이미지 파일
|
||||
* @return s3 api 요구 스펙 dto
|
||||
*/
|
||||
private ObjectMetadata createObjectMetadata(MultipartFile file) {
|
||||
ObjectMetadata metadata = new ObjectMetadata();
|
||||
metadata.setContentLength(file.getSize());
|
||||
metadata.setContentType(file.getContentType());
|
||||
return metadata;
|
||||
}
|
||||
}
|
||||
@@ -13,8 +13,8 @@ import java.io.IOException;
|
||||
/**
|
||||
* 깃헙 레포 이미지 업로드 전략 구현체
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
//@RequiredArgsConstructor
|
||||
//@Service
|
||||
public class GithubRepoImgUploadStrategy implements ImgUploadStrategy {
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user