Springboot에서AWS s3에 csv파일을 업로드하는 내용을 정리하겠습니다.
build.gradle 설정
dependencies {
implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.4'
implementation 'mysql:mysql-connector-java:8.0.23'
// aws
implementation group: 'software.amazon.awssdk', name: 's3', version: '2.15.41'
// aws s3
implementation group: 'com.amazonaws', name: 'aws-java-sdk-s3', version: '1.11.897'
// opencsv
implementation group: 'com.opencsv', name: 'opencsv', version: '3.7'
}
AWS s3 Properties 설정 -> application.yml에 등록된 정보를 가져온다
@Getter
@Setter
@ToString
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties("application.aws")
public class AwsProperties {
// AWS 환경설정
Config config;
// AWS S3 정보
S3 s3;
// AWS RTM S3 정보
Rtms3 rtms3;
@Getter
@Setter
@ToString
public static class Config {
// 지역
String region;
// 프로필
String profileName;
}
@Getter
@Setter
@ToString
public static class S3 {
// 버킷명
String bucket;
// 버킷 경로
String prefix;
// 클라우드 프론트 도메인
String domain;
}
@Getter
@Setter
@ToString
public static class Rtms3 {
// 버킷명
String bucket;
}
/**
* S3에 저장될 prefixSet 경로 생성
*
* @param prefix
* @param fileNm
* @return
*/
public String getPrefixSet(String prefix, String fileNm) {
return prefix.concat("/").concat(fileNm);
}
}
Aws S3 config 작성
@Configuration
public class AwsS3Config {
private final AwsProperties awsProperties;
private final TransferManager transferManager;
@Autowired
private LogUtils logUtils;
@Autowired
public AwsS3Config(AwsProperties awsProperties) {
this.awsProperties = awsProperties;
this.transferManager = s3TransferManager();
}
/**
* S3 전송매니저 연결을 초기화 합니다.
*
* @return
*/
private TransferManager s3TransferManager() {
return new TransferManager(new DefaultAWSCredentialsProviderChain());
}
/**
* S3 파일을 저장 합니다.
*
* @param prefix
* @param multipartFile
* @param uploadContent
* @return
*/
public UploadContent putObject(String prefix, MultipartFile multipartFile, UploadContent uploadContent, String gubun) {
String prefixSet = awsProperties.getS3().getPrefix().concat(prefix);
AccessControlList acl = new AccessControlList();
acl.grantPermission(GroupGrantee.AllUsers, Permission.FullControl);
File file = null;
try {
file = convertMultiPartToFile(multipartFile);
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(file.length());
Upload upload = transferManager.upload(
new PutObjectRequest(
awsProperties.getS3().getBucket()
,prefixSet
, file
)
.withAccessControlList(acl)
.withCannedAcl(CannedAccessControlList.PublicRead)
.withMetadata(meta)
);
upload.waitForCompletion();
} catch (AmazonClientException ace) {
// 업로드 에러 로그 등록
uploadContent.setSttusCd(Status.FAILURE.getCode());
if ("ETC".equals(gubun)) {
uploadContent.setSttusDtlCn(StatusDtl.STATUS_S3.getCn());
logUtils.insertLog(uploadContent, "ERROR", LogMsg.S3_ERROR.getMessage());
} else {
logUtils.insertRtmLog(uploadContent, "ERROR", LogMsg.S3_ERROR.getMessage());
}
ace.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
file.deleteOnExit();
return uploadContent;
}
}
}