From fb7a77202ff2a7d775a32464e269c33f47450868 Mon Sep 17 00:00:00 2001 From: bjkim Date: Mon, 22 Sep 2025 15:55:29 +0900 Subject: [PATCH] =?UTF-8?q?[ADD]=20=ED=8C=8C=EC=9D=BC=20=EB=8B=A4=EC=9A=B4?= =?UTF-8?q?=EB=A1=9C=EB=93=9C=20=EB=B0=8F=20YAML=20=ED=85=8D=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=9D=BD=EA=B8=B0=20API=20=EC=B6=94=EA=B0=80=20(Mi?= =?UTF-8?q?nioAttachmentController)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MinioAttachmentController.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/main/java/kr/re/etri/autoflow/controllers/MinioAttachmentController.java b/src/main/java/kr/re/etri/autoflow/controllers/MinioAttachmentController.java index 20c07d9..7b51a4b 100644 --- a/src/main/java/kr/re/etri/autoflow/controllers/MinioAttachmentController.java +++ b/src/main/java/kr/re/etri/autoflow/controllers/MinioAttachmentController.java @@ -1,5 +1,7 @@ package kr.re.etri.autoflow.controllers; +import io.minio.GetObjectArgs; +import io.minio.MinioClient; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; @@ -21,6 +23,8 @@ import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; import java.io.InputStream; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.*; @RestController @@ -32,6 +36,8 @@ public class MinioAttachmentController { private final MinioAttachmentService minioAttachmentService; + private final MinioClient minioClient; + @Value("${kubeflow.url}") private String kubeflowBaseUrl; @@ -77,6 +83,48 @@ public class MinioAttachmentController { return ResponseEntity.notFound().build(); } + @Operation(summary = "파일 다운로드", description = "MinIO에서 파일을 다운로드합니다.") + @GetMapping("/download") + public ResponseEntity downloadFile(@RequestParam String objectName) { + try (InputStream is = minioClient.getObject( + GetObjectArgs.builder().bucket("mlpipeline").object(objectName).build() + )) { + byte[] bytes = is.readAllBytes(); + + // 파일명을 UTF-8로 URL 인코딩 + String encodedFileName = URLEncoder.encode(objectName, StandardCharsets.UTF_8) + .replaceAll("\\+", "%20"); // 공백 처리 + + return ResponseEntity.ok() + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename*=UTF-8''" + encodedFileName) + .contentType(MediaType.APPLICATION_OCTET_STREAM) + .body(bytes); + } catch (Exception e) { + log.error("파일 다운로드 실패", e); + return ResponseEntity.internalServerError().build(); + } + } + + @Operation(summary = "MinIO YAML 파일 읽기", description = "MinIO에서 YAML 파일을 다운로드하여 텍스트로 반환합니다.") + @GetMapping(value = "/readYamlText", produces = MediaType.TEXT_PLAIN_VALUE) + public ResponseEntity readYamlTextFromMinio(@RequestParam String objectName) { + try (InputStream is = minioClient.getObject( + GetObjectArgs.builder() + .bucket("mlpipeline") + .object(objectName) + .build() + )) { + // InputStream을 문자열로 변환 (UTF-8) + String content = new String(is.readAllBytes(), StandardCharsets.UTF_8); + + return ResponseEntity.ok(content); + } catch (Exception e) { + log.error("MinIO 파일 읽기 실패: " + objectName, e); + return ResponseEntity.internalServerError() + .body("Error reading file: " + e.getMessage()); + } + } + @Operation(summary = "파일 업로드", description = "MultipartFile을 MinIO 버킷에 업로드하고 DB에 기록합니다.") @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public ResponseEntity> uploadFile(