From 4a503fcead75e09bd01a46f353bd38f673302854 Mon Sep 17 00:00:00 2001 From: unanchoi Date: Sat, 21 Oct 2023 15:27:22 +0900 Subject: [PATCH] =?UTF-8?q?[FIX]=20S3Service=20=EC=9D=98=EC=A1=B4=EC=84=B1?= =?UTF-8?q?=20=EC=A3=BC=EC=9E=85=EC=9D=84=20=EC=9C=84=ED=95=9C=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../katchupserver/external/s3/AWSConfig.java | 50 +++++++++++++++++++ .../katchupserver/external/s3/S3Service.java | 30 +++-------- 2 files changed, 57 insertions(+), 23 deletions(-) create mode 100644 src/main/java/site/katchup/katchupserver/external/s3/AWSConfig.java diff --git a/src/main/java/site/katchup/katchupserver/external/s3/AWSConfig.java b/src/main/java/site/katchup/katchupserver/external/s3/AWSConfig.java new file mode 100644 index 0000000..f1fab61 --- /dev/null +++ b/src/main/java/site/katchup/katchupserver/external/s3/AWSConfig.java @@ -0,0 +1,50 @@ +package site.katchup.katchupserver.external.s3; + +import jakarta.annotation.PostConstruct; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import software.amazon.awssdk.auth.credentials.SystemPropertyCredentialsProvider; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.s3.presigner.S3Presigner; + + +@Configuration +public class AWSConfig { + + @Value("${cloud.aws.credentials.accessKey}") + private String accessKey; + @Value("${cloud.aws.credentials.secretKey}") + private String secretKey; + + @Value("${cloud.aws.region.static}") + private String regionString; + + @PostConstruct + public void setEnv() { + System.setProperty("aws.accessKeyId", accessKey); + System.setProperty("aws.secretAccessKey", secretKey); + } + + @Bean + public Region getRegion() { + return Region.of(regionString); + } + + @Bean + S3Presigner getS3Presigner() { + return S3Presigner.builder() + .region(getRegion()) + .credentialsProvider(SystemPropertyCredentialsProvider.create()) + .build(); + } + + @Bean + S3Client getS3Client() { + return S3Client.builder() + .region(getRegion()) + .credentialsProvider(SystemPropertyCredentialsProvider.create()) + .build(); + } +} diff --git a/src/main/java/site/katchup/katchupserver/external/s3/S3Service.java b/src/main/java/site/katchup/katchupserver/external/s3/S3Service.java index 7ac4d6b..1bd2a81 100644 --- a/src/main/java/site/katchup/katchupserver/external/s3/S3Service.java +++ b/src/main/java/site/katchup/katchupserver/external/s3/S3Service.java @@ -7,6 +7,7 @@ import site.katchup.katchupserver.common.exception.InternalServerException; import site.katchup.katchupserver.common.response.ErrorCode; import software.amazon.awssdk.auth.credentials.SystemPropertyCredentialsProvider; +import software.amazon.awssdk.core.signer.Presigner; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.DeleteObjectRequest; @@ -28,36 +29,16 @@ public class S3Service { @Value("${cloud.aws.s3.bucket}") private String bucketName; - @Value("${cloud.aws.region.static}") - private String regionString; - @Value("${cloud.aws.credentials.accessKey}") - private String accessKey; - @Value("${cloud.aws.credentials.secretKey}") - private String secretKey; - - @PostConstruct - public void setEnv() { - System.setProperty("aws.accessKeyId", accessKey); - System.setProperty("aws.secretAccessKey", secretKey); - } - - private final Region region = Region.of(regionString); - - private final S3Presigner preSigner = S3Presigner.builder() - .region(region) - .credentialsProvider(SystemPropertyCredentialsProvider.create()) - .build(); - private final S3Client s3Client = S3Client.builder() - .region(region) - .credentialsProvider(SystemPropertyCredentialsProvider.create()) - .build(); + private final AWSConfig awsConfig; private static final Long PRE_SIGNED_URL_EXPIRE_MINUTE = 1L; public PreSignedUrlVO getUploadPreSignedUrl(final String prefix, final String fileName) { final String uuidFileName = getUUIDFile(); final String key = prefix + "/" + getDateFolder() + "/" + uuidFileName + fileName; + S3Presigner preSigner = awsConfig.getS3Presigner(); + PutObjectRequest putObjectRequest = PutObjectRequest.builder() .bucket(bucketName) .key(key) @@ -72,11 +53,13 @@ public PreSignedUrlVO getUploadPreSignedUrl(final String prefix, final String fi } public String findUrlByName(String path) { + Region region = awsConfig.getRegion(); return "https://" + bucketName + ".s3." + region + ".amazonaws.com/" + path; } public String getDownloadPreSignedUrl(final String key, final String fileName) { try { + S3Presigner preSigner = awsConfig.getS3Presigner(); String encodedFileName = URLEncoder.encode(fileName, "UTF-8"); String replacedFileName = encodedFileName.replaceAll("\\+", "%20"); @@ -113,6 +96,7 @@ private String getDateFolder() { } public void deleteFile(String key) { + S3Client s3Client = awsConfig.getS3Client(); DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest .builder() .bucket(bucketName)