Skip to content

Commit

Permalink
[FEAT] AWS S3 Component 추가(#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
eunsol-an committed Sep 21, 2024
1 parent 7adbccc commit cc1b12c
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ dependencies {
implementation 'com.google.code.gson:gson:2.10.1'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-validation'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-mail', version: '3.0.5'

implementation 'com.google.apis:google-api-services-androidpublisher:v3-rev20231030-2.0.0'
implementation 'com.google.auth:google-auth-library-oauth2-http:1.19.0'

implementation group: 'software.amazon.awssdk', name: 'bom', version: '2.28.4'
implementation group: 'software.amazon.awssdk', name: 's3', version: '2.28.4'

compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.mysql:mysql-connector-j'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.nextroom.nextRoomServer.util.aws;

import com.nextroom.nextRoomServer.exceptions.CustomException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.*;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest;
import software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest;

import java.time.Duration;
import java.util.UUID;

import static com.nextroom.nextRoomServer.exceptions.StatusCode.INTERNAL_SERVER_ERROR;

@Component
@RequiredArgsConstructor
public class S3Component {
@Value("${spring.config.activate.on-profile}")
private String profile;
@Value("${spring.cloud.aws.s3.bucket}")
private String bucketName;

private final S3Client s3Client;
private final S3Presigner presigner;

private final static String EXTENSION = ".png";

public String createPresignedUrl(String fileName) {
validateFileName(fileName);

PutObjectRequest objectRequest = PutObjectRequest.builder()
.bucket(bucketName)
.key(fileName)
.contentType("image/png")
.build();

PutObjectPresignRequest presignRequest = PutObjectPresignRequest.builder()
.signatureDuration(Duration.ofMinutes(10))
.putObjectRequest(objectRequest)
.build();

return presigner.presignPutObject(presignRequest).url().toExternalForm();
}

public String createPresignedGetUrl(String fileName) {
validateFileName(fileName);

GetObjectRequest objectRequest = GetObjectRequest.builder()
.bucket(bucketName)
.key(fileName)
.build();

GetObjectPresignRequest presignRequest = GetObjectPresignRequest.builder()
.signatureDuration(Duration.ofMinutes(10))
.getObjectRequest(objectRequest)
.build();

return presigner.presignGetObject(presignRequest).url().toExternalForm();
}

public void deleteObject(String fileName) {
validateFileName(fileName);

try {
DeleteObjectRequest objectRequest = DeleteObjectRequest.builder()
.bucket(bucketName)
.key(fileName)
.build();

s3Client.deleteObject(objectRequest);
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
throw new CustomException(INTERNAL_SERVER_ERROR);
}
}

public String createFileName(Long shopId, Long themeId, String type, int i) {
return String.format("%s/%s/%s/%s/%s_%s%s", profile, shopId, themeId, type, i, UUID.randomUUID(), EXTENSION);
}

private void validateFileName(String fileName) {
if (fileName == null || fileName.isEmpty()) {
throw new CustomException(INTERNAL_SERVER_ERROR);
}
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/nextroom/nextRoomServer/util/aws/S3Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.nextroom.nextRoomServer.util.aws;

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.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;

@Configuration
public class S3Config {

@Value("${spring.cloud.aws.s3.credentials.accessKey}")
private String accessKey;

@Value("${spring.cloud.aws.s3.credentials.secretKey}")
private String secretKey;

@Value("${spring.cloud.aws.region.static}")
private String region;

@Bean
public S3Client s3Client() {
return S3Client.builder()
.credentialsProvider(StaticCredentialsProvider.create(awsCredentials()))
.region(Region.of(region))
.build();
}

@Bean
public S3Presigner presigner() {
return S3Presigner.builder()
.credentialsProvider(StaticCredentialsProvider.create(awsCredentials()))
.region(Region.of(region))
.build();
}

private AwsBasicCredentials awsCredentials() {
return AwsBasicCredentials.create(accessKey, secretKey);
}
}

0 comments on commit cc1b12c

Please sign in to comment.