|
|
package com.supervision.nxllmcommon.util;
|
|
|
|
|
|
import io.minio.*;
|
|
|
import io.minio.http.Method;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
@Slf4j
|
|
|
public class MinioUtil {
|
|
|
private static final MinioClient minioClient = SpringBeanUtil.getBean(MinioClient.class);
|
|
|
|
|
|
private static final String bucketName = SpringBeanUtil.getBean(MinioProperties.class).getBucketName();
|
|
|
|
|
|
static {
|
|
|
try {
|
|
|
if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
|
|
|
|
|
|
log.info("未找到bucket,自动建立");
|
|
|
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 上传一个文件
|
|
|
*/
|
|
|
public static String uploadFile(InputStream stream) throws Exception {
|
|
|
ObjectWriteResponse objectWriteResponse = minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(UUID.randomUUID().toString())
|
|
|
.stream(stream, stream.available(), -1).build());
|
|
|
return objectWriteResponse.object();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 上传文件
|
|
|
* @param stream 文件流
|
|
|
* @param contentType 文件类型
|
|
|
* @return 文件id
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public static String uploadFile(InputStream stream,String contentType) throws Exception {
|
|
|
ObjectWriteResponse objectWriteResponse = minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(UUID.randomUUID().toString())
|
|
|
.stream(stream, stream.available(), -1).contentType(contentType).build());
|
|
|
return objectWriteResponse.object();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 下载一个文件
|
|
|
*/
|
|
|
public static InputStream download(String fileId) throws Exception {
|
|
|
return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileId).build());
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 删除一个对象
|
|
|
*/
|
|
|
public static void deleteObject(String fileId) throws Exception {
|
|
|
minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(fileId).build());
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 获取文件信息
|
|
|
*
|
|
|
* @Param: [bucket, objectName]
|
|
|
* @return: java.lang.String
|
|
|
* @Author: MrFugui
|
|
|
* @Date: 2021/11/15
|
|
|
*/
|
|
|
public static String getObjectInfo(String fileId) throws Exception {
|
|
|
return minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(fileId).build()).toString();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 生成一个给HTTP GET请求用的presigned URL。浏览器/移动端的客户端可以用这个URL进行下载,即使其所在的存储桶是私有的。
|
|
|
*
|
|
|
* @Param: [bucketName, objectName, expires]
|
|
|
* @return: java.lang.String
|
|
|
* @Author: MrFugui
|
|
|
* @Date: 2021/11/15
|
|
|
*/
|
|
|
public static String getPresignedObjectUrl(String bucketName, String objectName, Integer expires) throws Exception {
|
|
|
GetPresignedObjectUrlArgs build = GetPresignedObjectUrlArgs
|
|
|
.builder().bucket(bucketName).object(objectName).expiry(expires).method(Method.GET).build();
|
|
|
return minioClient.getPresignedObjectUrl(build);
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|