-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: External Module 추가 및 이미지 업로드 및 삭제 로직 추가
Feature: �External Module 추가 및 이미지 업로드 및 삭제 로직 추가
- Loading branch information
Showing
7 changed files
with
186 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
cakk-api/src/main/java/com/cakk/api/controller/s3/AwsS3Controller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
cakk-external/src/main/java/com/cakk/external/config/S3Config.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
84 changes: 84 additions & 0 deletions
84
cakk-external/src/main/java/com/cakk/external/service/S3Service.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
8 changes: 8 additions & 0 deletions
8
cakk-external/src/main/java/com/cakk/external/vo/PresignedUrl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,7 @@ include( | |
'cakk-api', | ||
'cakk-client', | ||
'cakk-domain', | ||
'cakk-common' | ||
'cakk-common', | ||
'cakk-external' | ||
) | ||
|