-
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.
* feat: Ncp Object Storage 연결 * feat: Exnternal API 구축하기 * feat: Space 생성 시 배너 url 추가하기 * clean: 미사용 코드 제거 * fix: 누락된 MemberId 어노테이션 추가
- Loading branch information
1 parent
6ce1eb1
commit 6e07516
Showing
19 changed files
with
254 additions
and
8 deletions.
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
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
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
33 changes: 33 additions & 0 deletions
33
layer-api/src/main/java/org/layer/domain/external/api/ExternalApi.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,33 @@ | ||
package org.layer.domain.external.api; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import org.layer.common.annotation.MemberId; | ||
import org.layer.domain.external.dto.ExternalRequest; | ||
import org.layer.domain.external.dto.ExternalResponse; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
@Tag(name = "외부 API") | ||
public interface ExternalApi { | ||
@Operation(summary = "Presigned URL 발급받기", | ||
method = "POST", description = """ | ||
이미지 업로드를 위한 Presigned URL 발급합니다. | ||
""" | ||
) | ||
@ApiResponses({ | ||
@ApiResponse(responseCode = "200", | ||
content = { | ||
@Content( | ||
mediaType = "application/json", | ||
schema = @Schema(implementation = ExternalResponse.GetPreSignedURLResponse.class) | ||
) | ||
} | ||
) | ||
}) | ||
ResponseEntity<ExternalResponse.GetPreSignedURLResponse> getPresignedURL(@MemberId Long memberId, ExternalRequest.GetPreSignedURLRequest getPreSignedURLRequest); | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
layer-api/src/main/java/org/layer/domain/external/controller/ExternalController.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,30 @@ | ||
package org.layer.domain.external.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.layer.common.annotation.MemberId; | ||
import org.layer.domain.external.api.ExternalApi; | ||
import org.layer.domain.external.dto.ExternalRequest; | ||
import org.layer.domain.external.dto.ExternalResponse; | ||
import org.layer.external.ncp.service.NcpService; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/external") | ||
public class ExternalController implements ExternalApi { | ||
|
||
private final NcpService ncpService; | ||
|
||
@Override | ||
@GetMapping("/image/presigned") | ||
@PreAuthorize("isAuthenticated()") | ||
public ResponseEntity<ExternalResponse.GetPreSignedURLResponse> getPresignedURL(@MemberId Long memberId, ExternalRequest.GetPreSignedURLRequest getPreSignedURLRequest) { | ||
String url = ncpService.getPreSignedUrl(memberId, getPreSignedURLRequest.domain()); | ||
|
||
return ResponseEntity.ok(ExternalResponse.GetPreSignedURLResponse.toResponse(url)); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
layer-api/src/main/java/org/layer/domain/external/dto/ExternalRequest.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,14 @@ | ||
package org.layer.domain.external.dto; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import org.layer.external.ncp.enums.ImageDomain; | ||
|
||
public class ExternalRequest { | ||
|
||
@Schema(description = "Presigned URL 발급받기") | ||
public record GetPreSignedURLRequest( | ||
@Schema(description = "발급받을 이미지의 활용 도메인") | ||
ImageDomain domain | ||
) { | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
layer-api/src/main/java/org/layer/domain/external/dto/ExternalResponse.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 org.layer.domain.external.dto; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Builder; | ||
import org.layer.external.ncp.exception.ExternalExeption; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.layer.common.exception.ExternalExceptionType.INTERNAL_SERVER_ERROR; | ||
|
||
public class ExternalResponse { | ||
|
||
@Builder | ||
@Schema(description = "Presigned URL 응답") | ||
public record GetPreSignedURLResponse( | ||
@Schema(description = "생성된 Presigned URL") | ||
@NotNull | ||
String url | ||
) { | ||
public static GetPreSignedURLResponse toResponse(String url) { | ||
return Optional.ofNullable(url).map(it -> GetPreSignedURLResponse.builder().url(url).build()).orElseThrow(() -> new ExternalExeption(INTERNAL_SERVER_ERROR)); | ||
} | ||
} | ||
} |
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
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
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
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
22 changes: 22 additions & 0 deletions
22
layer-common/src/main/java/org/layer/common/exception/ExternalExceptionType.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,22 @@ | ||
package org.layer.common.exception; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@RequiredArgsConstructor | ||
public enum ExternalExceptionType implements ExceptionType { | ||
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "알수 없는 에러에요."); | ||
|
||
private final HttpStatus status; | ||
private final String message; | ||
|
||
@Override | ||
public HttpStatus httpStatus() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String message() { | ||
return null; | ||
} | ||
} |
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
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
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
37 changes: 37 additions & 0 deletions
37
layer-external/src/main/java/org/layer/external/ncp/config/NcpConfig.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,37 @@ | ||
package org.layer.external.ncp.config; | ||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.client.builder.AwsClientBuilder; | ||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class NcpConfig { | ||
@Value("${ncp.storage.accessKey}") | ||
private String accessKey; | ||
|
||
@Value("${ncp.storage.secretKey}") | ||
private String secretKey; | ||
|
||
@Value("${ncp.storage.region}") | ||
private String region; | ||
|
||
@Value("${ncp.storage.endpoint}") | ||
private String endPoint; | ||
|
||
|
||
@Bean | ||
public AmazonS3Client amazonS3Client() { | ||
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, | ||
secretKey); | ||
return (AmazonS3Client) AmazonS3ClientBuilder.standard() | ||
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) | ||
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, | ||
region)) | ||
.build(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
layer-external/src/main/java/org/layer/external/ncp/enums/ImageDomain.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,5 @@ | ||
package org.layer.external.ncp.enums; | ||
|
||
public enum ImageDomain { | ||
SPACE, | ||
} |
10 changes: 10 additions & 0 deletions
10
layer-external/src/main/java/org/layer/external/ncp/exception/ExternalExeption.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,10 @@ | ||
package org.layer.external.ncp.exception; | ||
|
||
import org.layer.common.exception.BaseCustomException; | ||
import org.layer.common.exception.ExceptionType; | ||
|
||
public class ExternalExeption extends BaseCustomException { | ||
public ExternalExeption(ExceptionType exceptionType) { | ||
super(exceptionType); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
layer-external/src/main/java/org/layer/external/ncp/service/NcpService.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,53 @@ | ||
package org.layer.external.ncp.service; | ||
|
||
import com.amazonaws.HttpMethod; | ||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.Headers; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.layer.external.ncp.enums.ImageDomain; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Date; | ||
import java.util.UUID; | ||
|
||
@Service | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class NcpService { | ||
|
||
@Value("${ncp.storage.bucketName}") | ||
private String bucket; | ||
|
||
private final AmazonS3Client amazonS3Client; | ||
|
||
public String getPreSignedUrl(Long memberId, ImageDomain imageDomain) { | ||
String fileName = imageDomain + "/" + memberId.toString() + "/" + UUID.randomUUID(); | ||
|
||
GeneratePresignedUrlRequest generatePresignedUrlRequest = getGeneratePreSignedUrlRequest(bucket, fileName); | ||
|
||
return amazonS3Client.generatePresignedUrl(generatePresignedUrlRequest).toString(); | ||
} | ||
|
||
private GeneratePresignedUrlRequest getGeneratePreSignedUrlRequest(String bucket, String fileName) { | ||
GeneratePresignedUrlRequest generatePresignedUrlRequest = | ||
new GeneratePresignedUrlRequest(bucket, fileName) | ||
.withMethod(HttpMethod.PUT) | ||
.withExpiration(getPreSignedUrlExpiration()); | ||
generatePresignedUrlRequest.addRequestParameter( | ||
Headers.S3_CANNED_ACL, | ||
CannedAccessControlList.PublicRead.toString()); | ||
return generatePresignedUrlRequest; | ||
} | ||
|
||
private Date getPreSignedUrlExpiration() { | ||
Date expiration = new Date(); | ||
long expTimeMillis = expiration.getTime(); | ||
expTimeMillis += 1000 * 60 * 2; | ||
expiration.setTime(expTimeMillis); | ||
return expiration; | ||
} | ||
} |