|
@@ -1,8 +1,6 @@
|
|
|
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;
|
|
@@ -11,6 +9,7 @@ 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 java.io.File;
|
|
@@ -23,13 +22,10 @@ import java.io.InputStream;
|
|
|
@Component
|
|
|
public class BatchDownloadUtil {
|
|
|
|
|
|
- @Autowired
|
|
|
- private MinioClient minioClient;
|
|
|
|
|
|
- private static final String LOCAL_DIR = "/Users/shiyue/Downloads/zn/minio-data/";
|
|
|
+ private static MinioClient minioClient;
|
|
|
|
|
|
-// private static final String BUCKET_NAME = "wof085500002";
|
|
|
-// private static final String ROOT_PATH = "WOF085500002-WOB00002/";
|
|
|
+ private static String localDir;
|
|
|
|
|
|
/**
|
|
|
* 下载minio中一个桶下de资源
|
|
@@ -37,7 +33,7 @@ public class BatchDownloadUtil {
|
|
|
* @param resoucePath 桶下资源路径(批次编号/)
|
|
|
* @return
|
|
|
*/
|
|
|
- public String backResourceAddress(String bucketName,String resoucePath){
|
|
|
+ public static String backResourceAddress(String bucketName,String resoucePath){
|
|
|
ListObjectsArgs listObjectsArgs = ListObjectsArgs.builder().bucket(bucketName).prefix(resoucePath).build();
|
|
|
try {
|
|
|
return findAndSaveFile(listObjectsArgs,bucketName );
|
|
@@ -47,28 +43,26 @@ public class BatchDownloadUtil {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- private String findAndSaveFile(ListObjectsArgs listObjectsArgs,String bucketName) throws Exception {
|
|
|
+ private static String findAndSaveFile(ListObjectsArgs listObjectsArgs,String bucketName) throws Exception {
|
|
|
|
|
|
- File ifile = new File(LOCAL_DIR + bucketName);
|
|
|
+ File ifile = new File(localDir + 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);
|
|
|
+ File file = new File(localDir +File.separator + bucketName+ File.separator + objectName);
|
|
|
// Create parent directories if needed
|
|
|
file.getParentFile().mkdirs();
|
|
|
// Get the object as an input stream
|
|
@@ -83,8 +77,17 @@ public class BatchDownloadUtil {
|
|
|
return ifile.getPath();
|
|
|
}
|
|
|
|
|
|
- // public void main(String[] args) throws Exception {
|
|
|
-// ListObjectsArgs listObjectsArgs = ListObjectsArgs.builder().bucket(BUCKET_NAME).prefix(ROOT_PATH).build();
|
|
|
-// findAndSaveFile(listObjectsArgs);
|
|
|
-// }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setMinioClient(MinioClient minioClient){
|
|
|
+ BatchDownloadUtil.minioClient = minioClient;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Value("${file.download.dir:/Users/baiyahui/Downloads/zn/minio-data/}")
|
|
|
+ public void setLocalDir(String localDir) {
|
|
|
+ BatchDownloadUtil.localDir = localDir;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|