Skip to content

Commit

Permalink
[FIX] S3Service 의존성 주입을 위한 코드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
unanchoi committed Oct 21, 2023
1 parent 1710291 commit 4a503fc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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");

Expand Down Expand Up @@ -113,6 +96,7 @@ private String getDateFolder() {
}

public void deleteFile(String key) {
S3Client s3Client = awsConfig.getS3Client();
DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest
.builder()
.bucket(bucketName)
Expand Down

0 comments on commit 4a503fc

Please sign in to comment.