[ADD] ExternalAuthController에 MinIO 파일 다운로드 및 S3 업로드 기능 추가, 관련 API 및 EdgeSWUploadService 지원 메서드 구현

main
bjkim 8 months ago
parent 0b2ffd978f
commit d2e515ffeb

@ -9,6 +9,7 @@ import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct;
import kr.re.etri.autoflow.payload.request.EdgeSWVO; import kr.re.etri.autoflow.payload.request.EdgeSWVO;
import kr.re.etri.autoflow.service.DynamicMinioAttachmentService;
import kr.re.etri.autoflow.service.EdgeSWUploadService; import kr.re.etri.autoflow.service.EdgeSWUploadService;
import kr.re.etri.autoflow.service.ExternalAuthService; import kr.re.etri.autoflow.service.ExternalAuthService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -44,6 +45,8 @@ public class ExternalAuthController {
private final ExternalAuthService externalAuthService; private final ExternalAuthService externalAuthService;
private final EdgeSWUploadService edgeSWUploadService; private final EdgeSWUploadService edgeSWUploadService;
private final DynamicMinioAttachmentService minioService;
private RestTemplate restTemplate; private RestTemplate restTemplate;
@PostConstruct @PostConstruct
@ -163,7 +166,7 @@ public class ExternalAuthController {
@PostMapping(value = "/register-with-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @PostMapping(value = "/register-with-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> registerWithFile( public ResponseEntity<String> registerWithFile(
@RequestParam("sw_id") String swId, @RequestParam("sw_id") String swId,
@RequestParam("sw_version") int swVersion, @RequestParam("sw_version") float swVersion,
@RequestParam("sw_name") String swName, @RequestParam("sw_name") String swName,
@RequestParam("auth_id") String authId, @RequestParam("auth_id") String authId,
@ -239,6 +242,103 @@ public class ExternalAuthController {
} }
} }
@PostMapping("/register-with-minio-file")
@Operation(
summary = "외부 DB 등록 + MinIO 다운로드 파일 S3 업로드",
description = "MinIO에서 파일을 다운로드하고, S3 업로드 후 외부 DB에 등록합니다.",
security = @SecurityRequirement(name = "bearerAuth")
)
public ResponseEntity<String> registerWithMinioFile(
@RequestParam("sw_id") String swId,
@RequestParam("sw_version") float swVersion,
@RequestParam("sw_name") String swName,
@RequestParam
@Schema(description = "auth token", example = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsImV4cCI6MTc2NDIxOTA5MSwiaWF0IjoxNzYxNjI3MDkxfQ.OGDxMSEOsS36IEDEKTXTgACMS5r0nW3ylVskCmqbQYY", required = true)
String authId,
@RequestParam
@Schema(description = "SW/엣지 패키지 정보", example = "14", required = true)
Integer pkg_serial,
@RequestParam
@Schema(description = "압축여부(0:단일,1:묶음)", example = "1", required = true)
Integer archiveType,
@RequestParam
@Schema(description = "실행여부 (0:실행하지않음,1:실행)", example = "1", required = true)
String execYn,
@RequestParam
@Schema(description = "비밀글 여부 (true/false)", example = "false", required = true)
String secretAt,
@RequestParam
@Schema(description = "설치 위치", example = "/etc/test/", required = true)
String downloadLocation,
@RequestParam("user_id")
@Schema(description = "사용자 ID", example = "admin", required = true)
String userId,
@RequestParam("sw_type")
@Schema(description = "0 = sw, 1 = edge로 DB 전송", example = "1", required = true)
int sw_type,
@RequestParam("creation_datetime")
@Schema(description = "생성 일시 (ISO-8601 형식, UTC)", example = "2025-10-20T11:46:49.848Z", required = true)
String creationDatetime,
@Parameter(description = "다운로드할 MinIO 객체 이름", example="4/9d08fa7973cf4c39a0979bb4d70c640b/artifacts/sklearn-model/model.pkl", required = true)
@RequestParam String objectName,
@Parameter(description = "MINIO 서버 타입 (type1[kubeflow],type2[mlflow])", example="type2", required = true)
@RequestParam String type,
@Parameter(description = "서버에 저장할 로컬 경로", example="downloads/temp", required = true)
@RequestParam String localPath
) {
try {
// objectName에서 파일명 추출
String fileName = objectName.substring(objectName.lastIndexOf("/") + 1);
// localPath가 디렉토리라면 파일명 붙이기
String fullLocalPath = localPath.endsWith("/") ? localPath + fileName : localPath + "/" + fileName;
// 1. MinIO에서 서버로 다운로드
minioService.downloadFileToServer(objectName, type, localPath);
// 2. VO 생성
EdgeSWVO edgeSWVO = new EdgeSWVO();
edgeSWVO.setSw_id(swId);
edgeSWVO.setSw_version(swVersion);
edgeSWVO.setSw_name(swName);
edgeSWVO.setAuth_id(authId);
edgeSWVO.setUser_id(userId);
edgeSWVO.setCreation_datetime(creationDatetime);
edgeSWVO.setPkg_serial(pkg_serial);
edgeSWVO.setArchive_type(archiveType);
edgeSWVO.setDownload_location(downloadLocation);
edgeSWVO.setExec_yn(execYn);
edgeSWVO.setSecret_at(secretAt);
// 3. 다운로드한 파일을 S3로 업로드
edgeSWUploadService.uploadFileToS3Only(edgeSWVO, fullLocalPath);
// 4. DB 등록
String result = edgeSWUploadService.registerMetadata(edgeSWVO, sw_type);
return ResponseEntity.ok(result);
} catch (IOException ioe) {
log.error("S3 파일 업로드 실패", ioe);
return ResponseEntity.internalServerError().body("파일 업로드 실패: " + ioe.getMessage());
} catch (Exception e) {
log.error("DB 등록 실패", e);
return ResponseEntity.internalServerError().body("DB 등록 실패: " + e.getMessage());
}
}
// DTO: 요청 // DTO: 요청
public static record SigninRequest(String id, String password) { } public static record SigninRequest(String id, String password) { }

@ -64,9 +64,36 @@ public class EdgeSWUploadService {
return key; // S3 경로 반환 return key; // S3 경로 반환
} }
public String uploadFileToS3Only(EdgeSWVO edgeSWVO, String localPath) throws IOException {
File file = new File(localPath);
if (!file.exists() || !file.isFile()) {
throw new IOException("파일이 존재하지 않거나 유효하지 않습니다: " + localPath);
}
// S3 경로 지정: files/edge_sw/<원본파일명>
String fileName = file.getName();
String key = "files/edge_sw/" + fileName;
// S3 업로드
s3Client.putObject(
PutObjectRequest.builder()
.bucket(s3Bucket)
.key(key)
.build(),
RequestBody.fromFile(file)
);
// 실제 저장된 S3 경로를 VO에 세팅
edgeSWVO.setFile_location(key); // S3 경로
edgeSWVO.setFile_type(fileName.substring(fileName.lastIndexOf(".") + 1)); // 확장자
edgeSWVO.setFile_name(key); // 실제 S3 경로를 파일명으로 사용
edgeSWVO.setFile_size(file.length()); // 파일 크기
return key; // S3 경로 반환
}
//sw_type 0 = sw // sw_type 0 = sw
// sw_type 1 = edge // sw_type 1 = edge
public String registerMetadata(EdgeSWVO edgeSWVO, int sw_type) { public String registerMetadata(EdgeSWVO edgeSWVO, int sw_type) {

Loading…
Cancel
Save