完成实例节点下载fil的结果接口

This commit is contained in:
fanshuai 2024-03-07 09:47:37 +08:00
parent f60bde9016
commit 74526da9a2
5 changed files with 130 additions and 14 deletions

View File

@ -139,7 +139,5 @@ public class ExperimentInsController {
return AjaxResult.success(this.experimentInsService.getNodeResult(id,nodeId));
}
}

View File

@ -0,0 +1,34 @@
package com.ruoyi.platform.controller.minio;
import com.ruoyi.platform.service.MinioService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* minio 控制层
* 提供通用的minio 存储上传下载接口
* @author makejava
* @since 2023-11-07 15:08:22
*
* */
@RestController
@RequestMapping("minioStorage")
@Api("minio存储管理")
public class MinioStorageController {
@Resource
private MinioService minioService;
@GetMapping("/download")
@ApiOperation(value = "minio存储下载", notes = "minio存储下载文件为zip包")
public ResponseEntity<InputStreamResource> downloadDataset(@RequestParam("path") String path) {
String bucketName = path.substring(0, path.indexOf("/"));
String prefix = path.substring(path.indexOf("/")+1,path.length())+"/";
return minioService.downloadZipFile(bucketName,prefix);
}
}

View File

@ -0,0 +1,8 @@
package com.ruoyi.platform.service;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
public interface MinioService {
public ResponseEntity<InputStreamResource> downloadZipFile(String bucketName , String path);
}

View File

@ -0,0 +1,43 @@
package com.ruoyi.platform.service.impl;
import com.ruoyi.platform.service.MinioService;
import com.ruoyi.platform.utils.MinioUtil;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
@Service("MinioService")
public class MinioServiceImpl implements MinioService {
private final MinioUtil minioUtil;
public MinioServiceImpl(MinioUtil minioUtil) {
this.minioUtil = minioUtil;
}
@Override
public ResponseEntity<InputStreamResource> downloadZipFile(String bucketName,String path) {
try {
// 使用ByteArrayOutputStream来捕获下载的数据
InputStream inputStream = minioUtil.downloadAndZip(bucketName, path);
Path p = Paths.get(path);
String fileName = p.getFileName().toString()+".zip";
InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(inputStreamResource);
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
}

View File

@ -2,12 +2,13 @@ package com.ruoyi.platform.utils;
import io.minio.*;
import io.minio.errors.MinioException;
import io.minio.messages.Item;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.InputStreamResource;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -15,20 +16,12 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@Component
public class MinioUtil {
private MinioClient minioClient;
@Value("${minio.endpoint}")
private String endpoint;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
public MinioUtil() {
this.minioClient = MinioClient.builder()
.endpoint("http://172.20.32.181:30164")
@ -109,4 +102,44 @@ public class MinioUtil {
return content.toString();
}
}
/**
* Downloads files and folders from the specified bucket and path, and creates a zip archive.
*
* @param bucketName The name of the bucket.
* @param path The path within the bucket.
* @return InputStream containing the zip archive.
* @throws MinioException If an error occurs while communicating with Minio.
* @throws IOException If an I/O error occurs during zip creation.
*/
public InputStream downloadAndZip(String bucketName, String path) throws Exception{
try (ByteArrayOutputStream zipOutputStream = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(zipOutputStream)) {
Iterable<Result<Item>> results = minioClient.listObjects(
ListObjectsArgs.builder().bucket(bucketName).prefix(path).recursive(true).build());
for (Result<Item> result : results) {
Item item = result.get();
String objectName = item.objectName();
InputStream objectStream = minioClient.getObject(
GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
// Create a zip entry for each object
ZipEntry zipEntry = new ZipEntry(objectName);
zip.putNextEntry(zipEntry);
// Write object data to zip stream
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = objectStream.read(buffer)) != -1) {
zip.write(buffer, 0, bytesRead);
}
zip.closeEntry();
objectStream.close();
}
zip.finish();
return new ByteArrayInputStream(zipOutputStream.toByteArray());
}
}
}