|
|
|
@@ -1,31 +1,38 @@ |
|
|
|
package com.ruoyi.platform.utils; |
|
|
|
|
|
|
|
|
|
|
|
import com.ruoyi.common.security.utils.SecurityUtils; |
|
|
|
import io.minio.*; |
|
|
|
import io.minio.errors.MinioException; |
|
|
|
import io.minio.messages.DeleteObject; |
|
|
|
import io.minio.messages.Item; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.io.FileUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
import java.io.*; |
|
|
|
import java.net.URLDecoder; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.nio.file.Path; |
|
|
|
import java.nio.file.Paths; |
|
|
|
import java.util.*; |
|
|
|
import java.util.zip.ZipEntry; |
|
|
|
import java.util.zip.ZipOutputStream; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@Component |
|
|
|
public class MinioUtil { |
|
|
|
|
|
|
|
private static MinioClient minioClient; |
|
|
|
|
|
|
|
@Value("${git.localPath}") |
|
|
|
String localPath; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
public MinioUtil(@Value("${minio.endpoint}")String minioEndpoint,@Value("${minio.accessKey}")String minioAccessKey,@Value("${minio.secretKey}") String minioSecretKey) { |
|
|
|
public MinioUtil(@Value("${minio.endpoint}") String minioEndpoint, @Value("${minio.accessKey}") String minioAccessKey, @Value("${minio.secretKey}") String minioSecretKey) { |
|
|
|
this.minioClient = MinioClient.builder() |
|
|
|
.endpoint(minioEndpoint) |
|
|
|
.credentials(minioAccessKey, minioSecretKey) |
|
|
|
@@ -192,7 +199,7 @@ public class MinioUtil { |
|
|
|
* @param srcObjectName 目标文件名称 |
|
|
|
*/ |
|
|
|
public ObjectWriteResponse copyObject(String bucketName, String objectName, |
|
|
|
String srcBucketName, String srcObjectName) throws Exception { |
|
|
|
String srcBucketName, String srcObjectName) throws Exception { |
|
|
|
return minioClient.copyObject( |
|
|
|
CopyObjectArgs.builder() |
|
|
|
.source(CopySource.builder().bucket(bucketName).object(objectName).build()) |
|
|
|
@@ -202,14 +209,13 @@ public class MinioUtil { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 递归拷贝 |
|
|
|
* |
|
|
|
* @param sourceBucketName 源bucket名称 |
|
|
|
* @param sourceKeyPrefix 源目录路径 |
|
|
|
* @param sourceBucketName 源bucket名称 |
|
|
|
* @param sourceKeyPrefix 源目录路径 |
|
|
|
* @param targetBucketName 目标bucket名称 |
|
|
|
* @param targetKeyPrefix 目标目录名称 |
|
|
|
* @param targetKeyPrefix 目标目录名称 |
|
|
|
*/ |
|
|
|
public void copyDirectory(String sourceBucketName, String sourceKeyPrefix, |
|
|
|
String targetBucketName, String targetKeyPrefix) throws Exception { |
|
|
|
@@ -238,9 +244,6 @@ public class MinioUtil { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 获取文件流 |
|
|
|
* |
|
|
|
@@ -267,6 +270,36 @@ public class MinioUtil { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void downloadFiles(String bucketName, String root_path, String targetPath) throws Exception { |
|
|
|
ListObjectsArgs listObjectsArgs = ListObjectsArgs.builder().bucket(bucketName).prefix(root_path).build(); |
|
|
|
findAndSaveFile(listObjectsArgs, bucketName, targetPath); |
|
|
|
} |
|
|
|
|
|
|
|
private void findAndSaveFile(ListObjectsArgs listObjectsArgs, String bucketName, String targetPath) throws Exception { |
|
|
|
// List all objects in the bucket |
|
|
|
Iterable<Result<Item>> results = minioClient.listObjects(listObjectsArgs); |
|
|
|
for (Result<Item> result : results) { |
|
|
|
Item item = result.get(); |
|
|
|
if (item.isDir()) { |
|
|
|
System.out.println("文件夹:" + URLDecoder.decode(item.objectName(), "UTF-8"));//输出日志 |
|
|
|
ListObjectsArgs args = ListObjectsArgs.builder().bucket(bucketName).prefix(item.objectName()).build(); |
|
|
|
findAndSaveFile(args, bucketName, targetPath); |
|
|
|
} else { |
|
|
|
GetObjectArgs getObjectArgs = GetObjectArgs.builder().bucket(bucketName).object(URLDecoder.decode(item.objectName())).build(); |
|
|
|
String objectName = URLDecoder.decode(item.objectName(), "UTF-8"); |
|
|
|
// Create a local file with the same name as the object |
|
|
|
File file = new File(targetPath); |
|
|
|
// Create parent directories if needed |
|
|
|
file.getParentFile().mkdirs(); |
|
|
|
// Get the object as an input stream |
|
|
|
try (InputStream stream = minioClient.getObject(getObjectArgs)) { |
|
|
|
// Copy the input stream to the file |
|
|
|
FileUtils.copyInputStreamToFile(stream, file); |
|
|
|
} |
|
|
|
System.out.printf("文件:%s 下载成功!\n", URLDecoder.decode(item.objectName()));//输出日志 |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public List<Map> listFilesInDirectory(String bucketName, String prefix) throws Exception { |
|
|
|
List<Map> fileInfoList = new ArrayList<>(); |
|
|
|
@@ -322,8 +355,6 @@ public class MinioUtil { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String readObjectAsString(String bucketName, String objectName) throws Exception { |
|
|
|
try (InputStream inputStream = minioClient.getObject( |
|
|
|
GetObjectArgs.builder() |
|
|
|
@@ -341,6 +372,7 @@ public class MinioUtil { |
|
|
|
return content.toString(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Downloads files and folders from the specified bucket and path, and creates a zip archive. |
|
|
|
* |
|
|
|
@@ -350,7 +382,7 @@ public class MinioUtil { |
|
|
|
* @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{ |
|
|
|
public InputStream downloadAndZip(String bucketName, String path) throws Exception { |
|
|
|
try (ByteArrayOutputStream zipOutputStream = new ByteArrayOutputStream(); |
|
|
|
ZipOutputStream zip = new ZipOutputStream(zipOutputStream)) { |
|
|
|
|
|
|
|
|