Skip to content

Commit

Permalink
Feature: External Module 추가 및 이미지 업로드 및 삭제 로직 추가
Browse files Browse the repository at this point in the history
Feature: �External Module 추가 및 이미지 업로드 및 삭제 로직 추가
  • Loading branch information
YongsHub authored May 7, 2024
2 parents 23129fe + 82c37db commit 6a5678d
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 1 deletion.
1 change: 1 addition & 0 deletions cakk-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dependencies {
implementation project(':cakk-common')
implementation project(':cakk-domain')
implementation project(':cakk-client')
implementation project(':cakk-external')

// basic
implementation('org.springframework.boot:spring-boot-starter-web')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.cakk.api.controller.s3;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;

import com.cakk.common.response.ApiResponse;
import com.cakk.external.service.S3Service;
import com.cakk.external.vo.PresignedUrl;

@RestController
@RequiredArgsConstructor
@RequestMapping("/aws")
public class AwsS3Controller {

private final S3Service s3Service;

@GetMapping("/img")
public ApiResponse<PresignedUrl> getImageUrl() {
return ApiResponse.success(s3Service.getPresignedUrlWithImagePath());
}

}
19 changes: 19 additions & 0 deletions cakk-external/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
description = "external module"


dependencies {
implementation project(':cakk-common')
implementation('org.springframework:spring-context')

// AWS
implementation('com.amazonaws:aws-java-sdk-s3:1.12.715')
}


bootJar {
enabled = false
}

jar {
enabled = true
}
46 changes: 46 additions & 0 deletions cakk-external/src/main/java/com/cakk/external/config/S3Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.cakk.external.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;

@Configuration
public class S3Config {

private final String accessKey;
private final String secretKey;
private final String region;

public S3Config(
@Value("${cloud.aws.credentials.access-key}")
String accessKey,
@Value("${cloud.aws.credentials.secret-key}")
String secretKey,
@Value("${cloud.aws.region.static}")
String region) {
this.accessKey = accessKey;
this.secretKey = secretKey;
this.region = region;
}

@Bean
@Primary
public BasicAWSCredentials awsCredentialsProvider() {
return new BasicAWSCredentials(accessKey, secretKey);
}

@Bean
public AmazonS3 amazonS3() {
return AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(awsCredentialsProvider()))
.build();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.cakk.external.service;

import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.HttpMethod;
import com.amazonaws.SdkClientException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;

import com.cakk.common.enums.ReturnCode;
import com.cakk.common.exception.CakkException;
import com.cakk.external.vo.PresignedUrl;

@Service
public class S3Service {

private final AmazonS3 amazonS3;
private final String bucket;
private final String expiredIn;
private final String objectKey;

public S3Service(
@Value("${cloud.aws.s3.bucket}") String bucket,
@Value("${cloud.aws.s3.expire-in}") String expiredIn,
@Value("${cloud.aws.s3.objectKey}") String objectKey,
AmazonS3 amazonS3
) {
this.bucket = bucket;
this.expiredIn = expiredIn;
this.objectKey = objectKey;
this.amazonS3 = amazonS3;
}

public PresignedUrl getPresignedUrlWithImagePath() {
try {
String imagePath = makeObjectKey();
String imageUrl = getImageUrl(imagePath);
GeneratePresignedUrlRequest generatePresignedUrlRequest = createGeneratePresignedUrlRequestInstance(
imagePath);
String presignedUrl = generatePresignedUrlRequest(generatePresignedUrlRequest);
return new PresignedUrl(imagePath, imageUrl, presignedUrl);
} catch (SdkClientException e) {
throw new CakkException(ReturnCode.EXTERNAL_SERVER_ERROR);
}
}

public void deleteObject(String imagePath) {
try {
amazonS3.deleteObject(bucket, imagePath);
} catch (AmazonServiceException e) {
throw new CakkException(ReturnCode.EXTERNAL_SERVER_ERROR);
}
}

private GeneratePresignedUrlRequest createGeneratePresignedUrlRequestInstance(String imagePath) {
Date expiration = new Date();
long expirationInMs = expiration.getTime();
expirationInMs += Long.parseLong(expiredIn);
expiration.setTime(expirationInMs);

return new GeneratePresignedUrlRequest(bucket, imagePath)
.withMethod(HttpMethod.PUT)
.withExpiration(expiration);
}

private String generatePresignedUrlRequest(GeneratePresignedUrlRequest generatePresignedUrlRequest)
throws SdkClientException {
return amazonS3.generatePresignedUrl(generatePresignedUrlRequest).toString();
}

private String makeObjectKey() {
return new StringBuffer().append(objectKey).append("/").append(UUID.randomUUID()).toString();
}

private String getImageUrl(String imagePath) {
return amazonS3.getUrl(bucket, imagePath).toString();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.cakk.external.vo;

public record PresignedUrl(
String imagePath,
String imageUrl,
String presignedUrl
) {
}
4 changes: 3 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ include(
'cakk-api',
'cakk-client',
'cakk-domain',
'cakk-common'
'cakk-common',
'cakk-external'
)

0 comments on commit 6a5678d

Please sign in to comment.