|
@@ -28,10 +28,7 @@ import org.springframework.util.ObjectUtils;
|
|
|
import java.io.*;
|
|
|
import java.net.URI;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Iterator;
|
|
|
-import java.util.List;
|
|
|
-import java.util.UUID;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* 阿里云操作模版类, 简化常见操作
|
|
@@ -59,6 +56,11 @@ public class MinioTemplate implements FileStorageTemplate {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ public String uploadFile(String pathKey, String filePath, Map<String, String> headers) {
|
|
|
+ return uploadFile(minioProperties.getBucket(), pathKey, filePath,headers);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
public String uploadFile(String bucket, String pathKey, String filePath) {
|
|
|
InnerUtils.checkArgument(bucket != null && bucket.length() > 0, "bucket can't be empty");
|
|
|
InnerUtils.checkArgument(pathKey != null && pathKey.length() > 0, "pathKey can't be empty");
|
|
@@ -67,6 +69,34 @@ public class MinioTemplate implements FileStorageTemplate {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ public String uploadFile(String bucket, String pathKey, String filePath, Map<String, String> headers) {
|
|
|
+ InnerUtils.checkArgument(bucket != null && bucket.length() > 0, "bucket can't be empty");
|
|
|
+ InnerUtils.checkArgument(pathKey != null && pathKey.length() > 0, "pathKey can't be empty");
|
|
|
+ BufferedInputStream stream =null;
|
|
|
+ try {
|
|
|
+ stream=FileUtil.getInputStream(new File(filePath));
|
|
|
+ PutObjectArgs objectArgs = PutObjectArgs.builder().object(pathKey)
|
|
|
+ .bucket(bucket)
|
|
|
+ .headers(headers)
|
|
|
+ .stream(stream, stream.available(), -1).build();
|
|
|
+ ObjectWriteResponse objectWriteResponse = minioClient.putObject(objectArgs);
|
|
|
+ log.info(objectWriteResponse.object());
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }finally {
|
|
|
+ if (stream != null) {
|
|
|
+ try {
|
|
|
+ stream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ // Ignore
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return calculateUrl(bucket, pathKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
public String uploadFileText(String pathKey, String content) {
|
|
|
return uploadFileText(minioProperties.getBucket(), pathKey, content);
|
|
|
}
|
|
@@ -136,6 +166,14 @@ public class MinioTemplate implements FileStorageTemplate {
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
log.error(e.getMessage(), e);
|
|
|
+ }finally {
|
|
|
+ if (stream != null) {
|
|
|
+ try {
|
|
|
+ stream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ // Ignore
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
return calculateUrl(bucket, pathKey);
|
|
|
}
|
|
@@ -401,8 +439,12 @@ public class MinioTemplate implements FileStorageTemplate {
|
|
|
|
|
|
@Override
|
|
|
public String getFileContent(String bucket, String keyName) throws Exception {
|
|
|
- try (GetObjectResponse object = minioClient.getObject(GetObjectArgs.builder().bucket(bucket).object(keyName).build());
|
|
|
- BufferedReader reader = new BufferedReader(new InputStreamReader(object))){
|
|
|
+ BufferedReader reader =null;
|
|
|
+ InputStreamReader in =null;
|
|
|
+ try {
|
|
|
+ GetObjectResponse object = minioClient.getObject(GetObjectArgs.builder().bucket(bucket).object(keyName).build());
|
|
|
+ in = new InputStreamReader(object);
|
|
|
+ reader = new BufferedReader(in);
|
|
|
StringBuilder contentJson = new StringBuilder();
|
|
|
while (true) {
|
|
|
String line = reader.readLine();
|
|
@@ -412,6 +454,9 @@ public class MinioTemplate implements FileStorageTemplate {
|
|
|
return contentJson.toString();
|
|
|
} catch (Exception e) {
|
|
|
throw e;
|
|
|
+ }finally {
|
|
|
+ IoUtil.close(in);
|
|
|
+ IoUtil.close(reader);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -426,7 +471,7 @@ public class MinioTemplate implements FileStorageTemplate {
|
|
|
List<String> list = new ArrayList<>();
|
|
|
Long total = 0L;
|
|
|
ListObjectsArgs objectArgs = ListObjectsArgs.builder()
|
|
|
- .bucket(bucket).recursive(true).prefix(key + "/").build();
|
|
|
+ .bucket(bucket).recursive(true).prefix(key).build();
|
|
|
Iterator<Result<Item>> iterator = minioClient.listObjects(objectArgs).iterator();
|
|
|
while (iterator.hasNext()){
|
|
|
Item item = iterator.next().get();
|
|
@@ -449,9 +494,7 @@ public class MinioTemplate implements FileStorageTemplate {
|
|
|
@SneakyThrows
|
|
|
@Override
|
|
|
public String getInternalEndpoint(String bucket ,String key) {
|
|
|
- URI uri = new URI(minioProperties.getInternalEndpoint());
|
|
|
- String host = uri.getHost();
|
|
|
- return new URI(uri.getScheme(), host, uri.getPath()+"/"+key, uri.getFragment()).toString();
|
|
|
+ return minioProperties.getInternalEndpoint()+"/"+bucket+"/"+key;
|
|
|
}
|
|
|
|
|
|
@Override
|