|
|
@@ -2,12 +2,13 @@ package com.ruoyi.platform.utils; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import io.minio.*; |
|
|
import io.minio.*; |
|
|
|
|
|
import io.minio.errors.MinioException; |
|
|
import io.minio.messages.Item; |
|
|
import io.minio.messages.Item; |
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
|
|
import org.springframework.core.io.InputStreamResource; |
|
|
import org.springframework.stereotype.Component; |
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
import java.io.InputStream; |
|
|
|
|
|
import java.io.OutputStream; |
|
|
|
|
|
|
|
|
import java.io.*; |
|
|
import java.nio.charset.StandardCharsets; |
|
|
import java.nio.charset.StandardCharsets; |
|
|
import java.nio.file.Path; |
|
|
import java.nio.file.Path; |
|
|
import java.nio.file.Paths; |
|
|
import java.nio.file.Paths; |
|
|
@@ -15,20 +16,12 @@ import java.util.ArrayList; |
|
|
import java.util.HashMap; |
|
|
import java.util.HashMap; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
import java.util.Map; |
|
|
import java.util.Map; |
|
|
|
|
|
import java.util.zip.ZipEntry; |
|
|
|
|
|
import java.util.zip.ZipOutputStream; |
|
|
|
|
|
|
|
|
@Component |
|
|
@Component |
|
|
public class MinioUtil { |
|
|
public class MinioUtil { |
|
|
private MinioClient minioClient; |
|
|
private MinioClient minioClient; |
|
|
|
|
|
|
|
|
@Value("${minio.endpoint}") |
|
|
|
|
|
private String endpoint; |
|
|
|
|
|
|
|
|
|
|
|
@Value("${minio.accessKey}") |
|
|
|
|
|
private String accessKey; |
|
|
|
|
|
|
|
|
|
|
|
@Value("${minio.secretKey}") |
|
|
|
|
|
private String secretKey; |
|
|
|
|
|
|
|
|
|
|
|
public MinioUtil() { |
|
|
public MinioUtil() { |
|
|
this.minioClient = MinioClient.builder() |
|
|
this.minioClient = MinioClient.builder() |
|
|
.endpoint("http://172.20.32.181:30164") |
|
|
.endpoint("http://172.20.32.181:30164") |
|
|
@@ -109,4 +102,44 @@ public class MinioUtil { |
|
|
return content.toString(); |
|
|
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()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |