javafx-tools-minio/src/main/java/com/zhangmeng/minio/utils/MinioUtils.java

225 lines
7.6 KiB
Java
Raw Normal View History

2024-03-08 10:20:49 +00:00
package com.zhangmeng.minio.utils;
import com.zhangmeng.minio.model.BucketFile;
import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import javafx.application.Platform;
import javafx.scene.control.TextField;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author zhangmeng
* @version 1.0
* @date 2024-03-07 15:45
*/
public class MinioUtils {
private static MinioClient minioClient = null;
2024-03-10 08:16:07 +00:00
// private static String endpoint = "http://192.168.1.254:9000";
// private static String accessKey = "minioadmin";
// private static String secretKey = "minioadmin";
2024-03-08 10:20:49 +00:00
public static Map<String,Object> objectMap = new HashMap<>();
public static String current_bucket = "current_bucket";
public static String primaryStage = "primaryStage";
public static String bucket_choose_action_stage = "bucket_choose_action_stage";
public static String minioController = "minioController";
public static MinioClient getDefault() {
if (minioClient == null) {
2024-03-10 08:16:07 +00:00
Object endpoint = objectMap.get("endpoint");
Object accessKey = objectMap.get("accessKey");
Object secretKey = objectMap.get("secretKey");
if (endpoint == null ){
Platform.runLater(()->{
AlertUtils.alert_warning("配置文件endpoint 为空");
});
return null;
}
if (accessKey == null ){
Platform.runLater(()->{
AlertUtils.alert_warning("配置文件accessKey 为空");
});
return null;
}
if (secretKey == null ){
Platform.runLater(()->{
AlertUtils.alert_warning("配置文件secretKey 为空");
});
return null;
}
minioClient = MinioClient.builder().endpoint(endpoint.toString()).credentials(accessKey.toString(), secretKey.toString()).build();
2024-03-08 10:20:49 +00:00
}
return minioClient;
}
/**
* Bucket
*
* @return
*/
@SneakyThrows(Exception.class)
public static List<Bucket> getAllBuckets() {
return getDefault().listBuckets();
}
/**
* SpringBootBucket
* Bucket
*
* @param bucketName
*/
@SneakyThrows(Exception.class)
public static void createBucket(String bucketName) {
if (!bucketExists(bucketName)) {
getDefault().makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
}else {
Platform.runLater(()->{
AlertUtils.alert_warning("bucket_name : "+ bucketName + "已存在");
});
}
}
/**
* Buckettruefalse
*
* @param bucketName
* @return
*/
@SneakyThrows(Exception.class)
public static boolean bucketExists(String bucketName) {
return getDefault().bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
}
public String DateFormatString(ZonedDateTime zonedDateTime){
// 创建一个 DateTimeFormatter 对象,用于定义日期和时间的格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 使用 formatter 来格式化 ZonedDateTime 对象
return zonedDateTime.format(formatter);
}
/**
*
*
* @param bucketName
* @param prefix
* @param recursive 使
* @return MinioItem
*/
@SneakyThrows(Exception.class)
public static List<Item> getAllObjectsByPrefix(String bucketName,
String prefix,
boolean recursive) {
List<Item> list = new ArrayList<>();
Iterable<Result<Item>> objectsIterator = getDefault().listObjects(
ListObjectsArgs.builder().bucket(bucketName).prefix(prefix).recursive(recursive).build());
if (objectsIterator != null) {
for (Result<Item> o : objectsIterator) {
Item item = o.get();
list.add(item);
}
}
return list;
}
/**
*
*
* @param bucketName
* @param objectName
* @return url
*/
@SneakyThrows(Exception.class)
public static String getPresignedObjectUrl(String bucketName, String objectName) {
GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder()
.bucket(bucketName)
.object(objectName)
.method(Method.GET).build();
return getDefault() .getPresignedObjectUrl(args);
}
@SneakyThrows(Exception.class)
public static List<BucketFile> getFileList(String bucketName){
List<Item> itemList = getAllObjectsByPrefix(bucketName, null, true);
List<BucketFile> list = new ArrayList<>();
if (itemList.size() > 0 ){
list = itemList.stream().map(i -> {
BucketFile bucketFile = new BucketFile();
2024-03-10 08:16:07 +00:00
bucketFile.setBucketName(bucketName);
2024-03-08 10:20:49 +00:00
bucketFile.setDir(i.isDir());
bucketFile.setFileName(i.objectName());
bucketFile.setSize(i.size());
//获取url
String presignedObjectUrl = getPresignedObjectUrl(bucketName, i.objectName());
bucketFile.setUrl(presignedObjectUrl);
return bucketFile;
}).collect(Collectors.toList());
}
return list;
}
public static void upload_file(File file){
//文件名
String fileName = file.getName();
String newFileName = System.currentTimeMillis() + "." + StringUtils.substringAfterLast(fileName, ".");
String contentType = getContentType(file);
String bucketName = objectMap.get(current_bucket).toString();
if (bucketName == null){
AlertUtils.alert_warning("上传的bucket不能为空!");
return;
}
uploadFile(bucketName, file, newFileName, contentType);
}
/**
* 使MultipartFile
*
* @param bucketName
* @param file
* @param objectName
* @param contentType
* @return
*/
@SneakyThrows(Exception.class)
public static ObjectWriteResponse uploadFile(String bucketName, File file, String objectName, String contentType) {
FileInputStream inputStream = new FileInputStream(file);
return minioClient.putObject(
PutObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.contentType(contentType)
.stream(inputStream,inputStream.available(), -1)
.build());
}
public static String getContentType(File file){
Tika tika = new Tika();
try {
return tika.detect(file);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}