|
@@ -22,6 +22,7 @@ import org.springframework.util.Base64Utils;
|
|
|
import java.io.*;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.UUID;
|
|
|
|
|
@@ -155,13 +156,25 @@ public class MinioTemplate implements FileStorageTemplate {
|
|
|
InnerUtils.checkArgument(bucket != null && bucket.length() > 0, "bucket can't be empty");
|
|
|
InnerUtils.checkArgument(pathKey != null && pathKey.length() > 0, "pathKey can't be empty");
|
|
|
InnerUtils.checkArgument(file != null && file.length() > 0, "file can't be empty");
|
|
|
- GetObjectArgs objectArgs = GetObjectArgs.builder().object(pathKey)
|
|
|
- .bucket(bucket).build();
|
|
|
+
|
|
|
+ InputStream in=null;
|
|
|
+
|
|
|
log.info("下载开始:下载bucket={},下载pathKey={},下载filePath={}", bucket, pathKey, file);
|
|
|
try {
|
|
|
- return minioClient.getObject(objectArgs);
|
|
|
+ GetObjectArgs objectArgs = GetObjectArgs.builder().object(pathKey)
|
|
|
+ .bucket(bucket).build();
|
|
|
+ in = minioClient.getObject(objectArgs);
|
|
|
+ return null;
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
+ }finally {
|
|
|
+ if(in!=null){
|
|
|
+ try {
|
|
|
+ in.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
@@ -178,12 +191,20 @@ public class MinioTemplate implements FileStorageTemplate {
|
|
|
InnerUtils.checkArgument(handler != null, "handler can't be null");
|
|
|
GetObjectArgs objectArgs = GetObjectArgs.builder().object(pathKey)
|
|
|
.bucket(bucket).build();
|
|
|
- GetObjectResponse ossObject = null;
|
|
|
+ InputStream in=null;
|
|
|
try {
|
|
|
- ossObject = minioClient.getObject(objectArgs);
|
|
|
- return ossObject;
|
|
|
+ in = minioClient.getObject(objectArgs);
|
|
|
+ return null;
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
+ }finally {
|
|
|
+ if(in!=null){
|
|
|
+ try {
|
|
|
+ in.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
@@ -270,9 +291,9 @@ public class MinioTemplate implements FileStorageTemplate {
|
|
|
try {
|
|
|
ListObjectsArgs objectArgs = ListObjectsArgs.builder()
|
|
|
.bucket(bucket).recursive(true).prefix(keyName + "/").build();
|
|
|
- Iterable<Result<Item>> results = minioClient.listObjects(objectArgs);
|
|
|
- for (Result<Item> result : results) {
|
|
|
- Item item = result.get();
|
|
|
+ Iterator<Result<Item>> iterator = minioClient.listObjects(objectArgs).iterator();
|
|
|
+ while (iterator.hasNext()){
|
|
|
+ Item item = iterator.next().get();
|
|
|
list.add(item.objectName());
|
|
|
}
|
|
|
log.info("获取文件夹集合={}", JSON.toJSONString(list));
|
|
@@ -289,8 +310,10 @@ public class MinioTemplate implements FileStorageTemplate {
|
|
|
.bucket(bucket)
|
|
|
.object(keyName)
|
|
|
.build();
|
|
|
+ InputStream in=null;
|
|
|
+
|
|
|
try {
|
|
|
- GetObjectResponse response = minioClient.getObject(getArgs);
|
|
|
+ in = minioClient.getObject(getArgs);
|
|
|
return true;
|
|
|
} catch (ErrorResponseException e) {
|
|
|
if (e.errorResponse().code().equals("NoSuchKey")) {
|
|
@@ -300,6 +323,14 @@ public class MinioTemplate implements FileStorageTemplate {
|
|
|
} catch (Exception e) {
|
|
|
log.error("连接异常", e.getMessage());
|
|
|
return false;
|
|
|
+ }finally {
|
|
|
+ if(in!=null){
|
|
|
+ try {
|
|
|
+ in.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
@@ -365,5 +396,29 @@ public class MinioTemplate implements FileStorageTemplate {
|
|
|
return calculateUrl(bucket, pathKey);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Long getSpace(String bucket, String key) {
|
|
|
+ try {
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ Long total = 0L;
|
|
|
+ ListObjectsArgs objectArgs = ListObjectsArgs.builder()
|
|
|
+ .bucket(bucket).recursive(true).prefix(key + "/").build();
|
|
|
+ Iterator<Result<Item>> iterator = minioClient.listObjects(objectArgs).iterator();
|
|
|
+ while (iterator.hasNext()){
|
|
|
+ Item item = iterator.next().get();
|
|
|
+ Long space =item.size();
|
|
|
+ total += space;
|
|
|
+ }
|
|
|
+ log.info("获取文件夹大小={}", total);
|
|
|
+ return total;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取文件夹大小失败={}", e.getMessage());
|
|
|
+ }
|
|
|
+ return 0L;
|
|
|
+ }
|
|
|
|
|
|
+ @Override
|
|
|
+ public Long getSpace(String key) {
|
|
|
+ return getSpace(minioProperties.getBucket(), key);
|
|
|
+ }
|
|
|
}
|