|
@@ -0,0 +1,93 @@
|
|
|
|
+package com.energy.manage.service.util;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
|
+import com.energy.manage.service.config.MinIoClientConfig;
|
|
|
|
+import io.minio.GetObjectArgs;
|
|
|
|
+import io.minio.ListObjectsArgs;
|
|
|
|
+import io.minio.MinioClient;
|
|
|
|
+import io.minio.Result;
|
|
|
|
+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.stereotype.Component;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 批量下载minio文件
|
|
|
|
+ */
|
|
|
|
+@Slf4j
|
|
|
|
+@Component
|
|
|
|
+public class BatchDownloadUtil {
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private MinioClient minioClient;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private MinIoClientConfig minIoClientConfig;
|
|
|
|
+
|
|
|
|
+// private static final String BUCKET_NAME = "wof085500002";
|
|
|
|
+// private static final String ROOT_PATH = "WOF085500002-WOB00002/";
|
|
|
|
+
|
|
|
|
+ private static final String LOCAL_DIR = "/Users/shiyue/Downloads/zn/minio-data/";
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 下载minio中一个桶下de资源
|
|
|
|
+ * @param bucketName 风场编号
|
|
|
|
+ * @param resoucePath 桶下资源路径(批次编号/)
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public String backResourceAddress(String bucketName,String resoucePath){
|
|
|
|
+ ListObjectsArgs listObjectsArgs = ListObjectsArgs.builder().bucket(bucketName).prefix(resoucePath).build();
|
|
|
|
+ try {
|
|
|
|
+ return findAndSaveFile(listObjectsArgs,bucketName );
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String findAndSaveFile(ListObjectsArgs listObjectsArgs,String bucketName) throws Exception {
|
|
|
|
+
|
|
|
|
+ File ifile = new File(LOCAL_DIR + bucketName);
|
|
|
|
+ ifile.mkdirs();
|
|
|
|
+
|
|
|
|
+ Iterable<Result<Item>> results = minioClient.listObjects(listObjectsArgs);
|
|
|
|
+ if(results == null){
|
|
|
|
+ ifile.delete();
|
|
|
|
+ System.out.println("Iterable is null");
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ for (Result<Item> result : results) {
|
|
|
|
+ Item item = result.get();
|
|
|
|
+ if (item.isDir()) {
|
|
|
|
+ System.out.println("文件夹:" + item.objectName());
|
|
|
|
+ ListObjectsArgs args = ListObjectsArgs.builder().bucket(bucketName).prefix(item.objectName()).build();
|
|
|
|
+ findAndSaveFile(args,bucketName);
|
|
|
|
+ } else {
|
|
|
|
+ GetObjectArgs getObjectArgs = GetObjectArgs.builder().bucket(bucketName).object(item.objectName()).build();
|
|
|
|
+ String objectName = item.objectName();
|
|
|
|
+ // Create a local file with the same name as the object
|
|
|
|
+ File file = new File(LOCAL_DIR +File.separator + bucketName+ File.separator + objectName);
|
|
|
|
+ // 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", item.objectName());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return ifile.getPath();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // public void main(String[] args) throws Exception {
|
|
|
|
+// ListObjectsArgs listObjectsArgs = ListObjectsArgs.builder().bucket(BUCKET_NAME).prefix(ROOT_PATH).build();
|
|
|
|
+// findAndSaveFile(listObjectsArgs);
|
|
|
|
+// }
|
|
|
|
+}
|