-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from choitaedong2/model_utility
update model utility
- Loading branch information
Showing
8 changed files
with
167 additions
and
0 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
21 changes: 21 additions & 0 deletions
21
app/user-server/src/main/java/kr/co/automl/api/UrlApi.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,21 @@ | ||
package kr.co.automl.api; | ||
|
||
import kr.co.automl.dto.PreSignedUrlResponse; | ||
import kr.co.automl.service.PreSignedUrlProvider; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import javax.websocket.server.PathParam; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class UrlApi { | ||
|
||
private final PreSignedUrlProvider preSignedUrlProvider; | ||
|
||
@GetMapping("/url/presigned") | ||
public PreSignedUrlResponse getPreSignedUrls(@PathParam("filename") String filename) { | ||
return preSignedUrlProvider.getUploadAndDownloadUrl(filename); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
app/user-server/src/main/java/kr/co/automl/dto/PreSignedUrlResponse.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 kr.co.automl.dto; | ||
|
||
public record PreSignedUrlResponse( | ||
String uploadUrl, String downloadUrl) { | ||
} |
24 changes: 24 additions & 0 deletions
24
app/user-server/src/main/java/kr/co/automl/global/config/swagger/OpenApiConfig.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,24 @@ | ||
package kr.co.automl.global.config.swagger; | ||
|
||
import io.swagger.v3.oas.models.Components; | ||
import io.swagger.v3.oas.models.OpenAPI; | ||
import io.swagger.v3.oas.models.info.Info; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* Swagger springdoc-ui 구성 파일 | ||
*/ | ||
@Configuration | ||
public class OpenApiConfig { | ||
@Bean | ||
public OpenAPI openAPI() { | ||
Info info = new Info() | ||
.title("WS-AutoML User API Document") | ||
.version("v0.0.1") | ||
.description("WS-AutoML 사용자 API 명세서입니다."); | ||
return new OpenAPI() | ||
.components(new Components()) | ||
.info(info); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
app/user-server/src/main/java/kr/co/automl/global/utils/DateUtils.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 kr.co.automl.global.utils; | ||
|
||
import org.joda.time.DateTime; | ||
|
||
import java.text.DateFormat; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
public class DateUtils { | ||
|
||
private static final String DATE_FORMAT = "yyyyMMdd-HHmmss"; | ||
|
||
/** | ||
* 날짜를 포맷된 문자열로 리턴합니다. | ||
* @param dateTime 포맷할 날짜 | ||
* @return 포맷된 문자열 | ||
*/ | ||
public static String getFormattedDateString(DateTime dateTime) { | ||
Date date = dateTime.toDate(); | ||
|
||
DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); | ||
|
||
return dateFormat.format(date); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
app/user-server/src/main/java/kr/co/automl/service/PreSignedUrlProvider.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,73 @@ | ||
package kr.co.automl.service; | ||
|
||
import com.amazonaws.HttpMethod; | ||
import com.amazonaws.services.s3.AmazonS3Client; | ||
|
||
import kr.co.automl.dto.PreSignedUrlResponse; | ||
import kr.co.automl.global.utils.DateUtils; | ||
import lombok.RequiredArgsConstructor; | ||
import org.joda.time.DateTime; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.ObjectUtils; | ||
|
||
import java.net.URL; | ||
import java.util.Date; | ||
|
||
/** | ||
* PreSignedUrl 제공을 담당합니다. | ||
*/ | ||
@Component | ||
@RequiredArgsConstructor | ||
public class PreSignedUrlProvider { | ||
|
||
private static final int EXPIRATION_DAYS = 1; | ||
|
||
private final AmazonS3Client amazonS3Client; | ||
private final String bucketName; | ||
|
||
/** | ||
* 파일 이름을 통해 생성한 PreSignedUrl을 리턴합니다. | ||
* | ||
* @param filename 생성할 파일 이름 | ||
* @return 생성한 PreSignedUrl | ||
*/ | ||
public PreSignedUrlResponse getUploadAndDownloadUrl(String filename) { | ||
validateEmpty(filename); | ||
|
||
Date expiration = DateTime.now().plusDays(EXPIRATION_DAYS).toDate(); | ||
String key = getKey(filename); | ||
|
||
// 업로드를 위한 PUT 요청용 URL 생성 | ||
URL uploadUrl = amazonS3Client.generatePresignedUrl(bucketName, key, expiration, HttpMethod.PUT); | ||
|
||
// 다운로드를 위한 GET 요청용 URL 생성 | ||
URL downloadUrl = amazonS3Client.generatePresignedUrl(bucketName, key, expiration, HttpMethod.GET); | ||
|
||
return new PreSignedUrlResponse(uploadUrl.toString(), downloadUrl.toString()); | ||
} | ||
|
||
/** | ||
* 비어있는지 검증합니다. | ||
* | ||
* @param filename 검증할 파일 이름 | ||
* | ||
* @throws IllegalArgumentException 파일 이름 검증에 실패할경우 | ||
*/ | ||
private void validateEmpty(String filename) { | ||
if (ObjectUtils.isEmpty(filename)) { | ||
throw new IllegalArgumentException("파일이름은 비어있을 수 없습니다."); | ||
} | ||
} | ||
|
||
/** | ||
* 키를 리턴합니다. 키는 파일 저장소에서 유일한 값이자 저장되는 파일 이름입니다. | ||
* | ||
* @param filename 파일 이름 | ||
* @return 키 | ||
*/ | ||
String getKey(String filename) { | ||
String keyPrefix = DateUtils.getFormattedDateString(DateTime.now()); | ||
|
||
return "TmpFile-" + keyPrefix + "-" + filename; | ||
} | ||
} |
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