You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
autoflow-server-mgmt/src/main/java/kr/re/etri/autoflow/controllers/MinIOController.java

172 lines
7.2 KiB

package kr.re.etri.autoflow.controllers;
import io.minio.*;
import io.minio.messages.Item;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/api/minio")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "MinIO Controller", description = "MinIO 버킷/파일 관리 API")
public class MinIOController {
private final MinioClient minioClient;
@Value("${minio.bucket}")
private String bucketName;
@Value("${minio.endpoint}")
private String minioEndpoint;
@Operation(summary = "버킷 존재 여부 체크", description = "기본 버킷이 존재하는지 확인합니다.")
@GetMapping("/bucket/check")
public boolean bucketExists() {
try {
return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
} catch (Exception e) {
log.error("버킷 체크 실패", e);
return false;
}
}
@Operation(summary = "파일 목록 조회", description = "버킷 내 파일 목록 조회, recursive 옵션으로 하위 폴더 포함 여부 지정 가능")
@GetMapping("/files")
public List<String> listFiles(
@Parameter(description = "파일 경로 접두사") @RequestParam(required = false) String prefix,
@Parameter(description = "하위 폴더 포함 여부") @RequestParam(required = false, defaultValue = "false") boolean recursive
) {
List<String> files = new ArrayList<>();
try {
Iterable<Result<Item>> results = minioClient.listObjects(
ListObjectsArgs.builder()
.bucket(bucketName)
.prefix(prefix)
.recursive(recursive)
.build()
);
for (Result<Item> result : results) {
Item item = result.get();
files.add(item.objectName());
}
} catch (Exception e) {
log.error("파일 목록 조회 실패", e);
}
return files;
}
@Operation(summary = "파일 업로드", description = "MultipartFile을 MinIO 버킷에 업로드합니다. path를 지정하면 하위 폴더에 저장 가능합니다.")
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String uploadFile(
@Parameter(description = "업로드할 파일")
@RequestPart("file") MultipartFile file,
@Parameter(description = "저장 경로 (선택)")
@RequestPart(value = "path", required = false) String path
) {
try (InputStream is = file.getInputStream()) {
String objectName = (path == null || path.isEmpty())
? file.getOriginalFilename()
: path + "/" + file.getOriginalFilename();
minioClient.putObject(
PutObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.stream(is, is.available(), -1)
.contentType(file.getContentType())
.build()
);
// MinIO 경로 URL 생성 (NodePort 기반)
String minioUrl = String.format("%s/%s/%s", minioEndpoint, bucketName, objectName);
return "파일 업로드 성공: " + minioUrl;
} catch (Exception e) {
log.error("파일 업로드 실패", e);
return "파일 업로드 실패: " + e.getMessage();
}
}
@Operation(summary = "다중 파일 업로드", description = "MultipartFile 배열을 MinIO 버킷에 업로드합니다. path를 지정하면 하위 폴더에 저장 가능합니다.")
@PostMapping(value = "/upload-multiple", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public List<String> uploadMultipleFiles(
@Parameter(description = "업로드할 파일들")
@RequestPart("files") MultipartFile[] files,
@Parameter(description = "저장 경로 (선택)")
@RequestPart(value = "path", required = false) String path
) {
List<String> uploadedUrls = new ArrayList<>();
for (MultipartFile file : files) {
try (InputStream is = file.getInputStream()) {
String objectName = (path == null || path.isEmpty())
? file.getOriginalFilename()
: path + "/" + file.getOriginalFilename();
minioClient.putObject(
PutObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.stream(is, is.available(), -1)
.contentType(file.getContentType())
.build()
);
String minioUrl = String.format("%s/%s/%s", minioEndpoint, bucketName, objectName);
uploadedUrls.add(minioUrl);
} catch (Exception e) {
log.error("파일 업로드 실패: {}", file.getOriginalFilename(), e);
uploadedUrls.add("업로드 실패: " + file.getOriginalFilename() + " (" + e.getMessage() + ")");
}
}
return uploadedUrls;
}
@Operation(summary = "파일 다운로드", description = "MinIO에서 파일을 다운로드합니다.")
@GetMapping("/download")
public ResponseEntity<byte[]> downloadFile(@RequestParam String objectName) {
try (InputStream is = minioClient.getObject(
GetObjectArgs.builder().bucket(bucketName).object(objectName).build()
)) {
byte[] bytes = is.readAllBytes();
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + objectName + "\"")
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(bytes);
} catch (Exception e) {
log.error("파일 다운로드 실패", e);
return ResponseEntity.internalServerError().build();
}
}
@Operation(summary = "파일 삭제", description = "MinIO 버킷에서 파일을 삭제합니다.")
@DeleteMapping("/delete")
public String deleteFile(@RequestParam String objectName) {
try {
minioClient.removeObject(
RemoveObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build()
);
return "삭제 성공: " + objectName;
} catch (Exception e) {
log.error("파일 삭제 실패", e);
return "삭제 실패: " + e.getMessage();
}
}
}