최초 커밋!
This commit is contained in:
14
aws/src/main/java/app/module/aws/AwsApplication.java
Normal file
14
aws/src/main/java/app/module/aws/AwsApplication.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package app.module.aws;
|
||||
|
||||
import app.module.core.config.application.component.YamlPropertySourceFactory;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@SpringBootApplication
|
||||
@PropertySource(value = "classpath:aws.yml", factory = YamlPropertySourceFactory.class)
|
||||
@PropertySource(
|
||||
value = "classpath:aws-${spring.profiles.active}.yml",
|
||||
factory = YamlPropertySourceFactory.class)
|
||||
public class AwsApplication {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package app.module.aws.config.application.aws;
|
||||
|
||||
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
|
||||
//@Profile(value = {"local", "dev", "prod"})
|
||||
public class AwsConfig {
|
||||
|
||||
@Value("${app.aws.s3.accessKey}")
|
||||
private String s3AccessKey;
|
||||
|
||||
@Value("${app.aws.s3.secretKey}")
|
||||
private String s3SecretKey;
|
||||
|
||||
@Value("${cloud.aws.region.static}")
|
||||
private String region;
|
||||
|
||||
@Bean
|
||||
public AmazonS3Client amazonS3Client() {
|
||||
BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(s3AccessKey, s3SecretKey);
|
||||
return (AmazonS3Client)
|
||||
AmazonS3ClientBuilder.standard()
|
||||
.withRegion(region)
|
||||
.withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package app.module.aws.usecase.file.upload.strategy;
|
||||
|
||||
import app.module.core.config.application.enums.FileStorageType;
|
||||
import app.module.core.usecase.file.saveToStorage.ports.application.strategy.FileUploadStrategy;
|
||||
import com.amazonaws.services.s3.AmazonS3Client;
|
||||
import com.amazonaws.services.s3.model.CannedAccessControlList;
|
||||
import com.amazonaws.services.s3.model.ObjectMetadata;
|
||||
import com.amazonaws.services.s3.model.PutObjectRequest;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class S3FileUploadStrategy implements FileUploadStrategy {
|
||||
|
||||
private final AmazonS3Client amazonS3Client;
|
||||
|
||||
@Value("${app.aws.s3.bucketName}")
|
||||
private String bucket;
|
||||
|
||||
@Value("${app.aws.s3.bucketFolder}")
|
||||
private String bucketFolder;
|
||||
|
||||
@Override
|
||||
public FileStorageType getStorageType() {
|
||||
return FileStorageType.S3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operate(MultipartFile multipartFile, String createSubPath, String createFilename)
|
||||
throws IOException {
|
||||
if (multipartFile == null || multipartFile.isEmpty()) {
|
||||
throw new RuntimeException("Failed to store empty file.");
|
||||
}
|
||||
|
||||
String bucketPath = bucket + bucketFolder;
|
||||
String createPath = createSubPath + "/" + createFilename;
|
||||
if (createPath.startsWith("/")) {
|
||||
createPath = createPath.replaceFirst("/", "");
|
||||
}
|
||||
|
||||
ObjectMetadata objMetadata = new ObjectMetadata();
|
||||
objMetadata.setContentType(multipartFile.getContentType());
|
||||
objMetadata.setContentLength(multipartFile.getSize());
|
||||
|
||||
try (InputStream inputStream = multipartFile.getInputStream()) {
|
||||
amazonS3Client.putObject(
|
||||
new PutObjectRequest(bucketPath, createPath, inputStream, objMetadata)
|
||||
.withCannedAcl(CannedAccessControlList.PublicRead));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package app.module.aws.usecase.file.url.strategy;
|
||||
|
||||
import app.module.core.config.application.enums.FileStorageType;
|
||||
import app.module.core.usecase.file.url.strategy.FileUrlStrategy;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class S3FileUrlStrategy implements FileUrlStrategy {
|
||||
|
||||
@Value("${app.aws.s3.domain}")
|
||||
private String cloudFrontDomain;
|
||||
|
||||
@Value("${app.aws.s3.bucketFolder}")
|
||||
private String bucketFolder;
|
||||
|
||||
@Override
|
||||
public FileStorageType getStorageType() {
|
||||
return FileStorageType.S3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInlineURL(String subPath, String filename) {
|
||||
return "https://" + cloudFrontDomain + bucketFolder + subPath + "/" + filename;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAttachmentURL(Long fileId) {
|
||||
return String.format("/content-disposition/attachment/%s", fileId);
|
||||
}
|
||||
}
|
||||
11
aws/src/main/resources/aws-dev.yml
Normal file
11
aws/src/main/resources/aws-dev.yml
Normal file
@@ -0,0 +1,11 @@
|
||||
app:
|
||||
aws:
|
||||
s3:
|
||||
# CDN Service Domain (e.g., https://your-cdn-domain.com)
|
||||
domain: ""
|
||||
|
||||
# File Upload Storage Configuration
|
||||
bucketName: "" # S3 bucket name
|
||||
bucketFolder: "" # Folder path within bucket (optional)
|
||||
accessKey: "" # AWS access key (recommend using environment variables)
|
||||
secretKey: "" # AWS secret key (recommend using environment variables)
|
||||
10
aws/src/main/resources/aws-local.yml
Normal file
10
aws/src/main/resources/aws-local.yml
Normal file
@@ -0,0 +1,10 @@
|
||||
app:
|
||||
aws:
|
||||
s3:
|
||||
# CDN Service Domain
|
||||
domain:
|
||||
# File Upload Storage 설정
|
||||
bucketName:
|
||||
bucketFolder:
|
||||
accessKey:
|
||||
secretKey:
|
||||
11
aws/src/main/resources/aws-prod.yml
Normal file
11
aws/src/main/resources/aws-prod.yml
Normal file
@@ -0,0 +1,11 @@
|
||||
app:
|
||||
aws:
|
||||
s3:
|
||||
# CDN Service Domain (e.g., https://your-cdn-domain.com)
|
||||
domain: ""
|
||||
|
||||
# File Upload Storage Configuration
|
||||
bucketName: "" # S3 bucket name
|
||||
bucketFolder: "" # Folder path within bucket (optional)
|
||||
accessKey: "" # AWS access key (recommend using environment variables)
|
||||
secretKey: "" # AWS secret key (recommend using environment variables)
|
||||
7
aws/src/main/resources/aws.yml
Normal file
7
aws/src/main/resources/aws.yml
Normal file
@@ -0,0 +1,7 @@
|
||||
# AWS 공용 속성
|
||||
cloud:
|
||||
aws:
|
||||
region:
|
||||
static: ap-northeast-2
|
||||
stack:
|
||||
auto: false
|
||||
Reference in New Issue
Block a user