minio对接
This commit is contained in:
@@ -0,0 +1,240 @@
|
|||||||
|
package minio;
|
||||||
|
|
||||||
|
import com.aliyun.oss.ServiceException;
|
||||||
|
import io.minio.MinioClient;
|
||||||
|
import io.minio.errors.InvalidEndpointException;
|
||||||
|
import io.minio.errors.InvalidPortException;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import net.coobird.thumbnailator.Thumbnails;
|
||||||
|
import org.jeecg.modules.oss.dto.WaterMarkDTO;
|
||||||
|
import org.jeecg.modules.system.entity.SysDepartInterfaceInfo;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import org.springframework.web.util.UriUtils;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description: minio oss 上传工具类
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class MinioBootUtil {
|
||||||
|
|
||||||
|
private static String bucket;
|
||||||
|
private static String host;
|
||||||
|
private static String accessKey;
|
||||||
|
private static String secretKey;
|
||||||
|
/**
|
||||||
|
* minio 工具客户端
|
||||||
|
*/
|
||||||
|
private static MinioClient minioClient = null;
|
||||||
|
|
||||||
|
private static String FILE_URL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Description 强制初始化minio
|
||||||
|
* @Author Alex
|
||||||
|
* @Date 2023/10/25 11:12
|
||||||
|
*/
|
||||||
|
public static MinioClient forceInitMinio(List<SysDepartInterfaceInfo> sysDepartInterfaceInfoList) throws InvalidPortException, InvalidEndpointException {
|
||||||
|
for (SysDepartInterfaceInfo sysDepartInterfaceInfo : sysDepartInterfaceInfoList) {
|
||||||
|
String interfaceTypeKey = sysDepartInterfaceInfo.getInterfaceTypeKey();//接口参数
|
||||||
|
String interfaceTypeValue = sysDepartInterfaceInfo.getInterfaceTypeValue();//接口参数值
|
||||||
|
switch (interfaceTypeKey) {
|
||||||
|
case "bucket":
|
||||||
|
bucket = interfaceTypeValue;
|
||||||
|
break;
|
||||||
|
case "host":
|
||||||
|
host = interfaceTypeValue;
|
||||||
|
break;
|
||||||
|
case "accessKey":
|
||||||
|
accessKey = interfaceTypeValue;
|
||||||
|
break;
|
||||||
|
case "secretKey":
|
||||||
|
secretKey = interfaceTypeValue;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
minioClient = new MinioClient(host, accessKey, secretKey);
|
||||||
|
return minioClient;
|
||||||
|
}
|
||||||
|
private static MinioClient initMinio() throws InvalidPortException, InvalidEndpointException {
|
||||||
|
if (minioClient == null) {
|
||||||
|
minioClient = new MinioClient(host, accessKey, secretKey);
|
||||||
|
}
|
||||||
|
return minioClient;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 上传文件
|
||||||
|
* 文件上传成功,返回文件完整访问路径
|
||||||
|
* 文件上传失败,返回 null
|
||||||
|
*
|
||||||
|
* @param multipartFile 待上传文件
|
||||||
|
* @param fileDir 文件保存目录
|
||||||
|
* @return oss 中的相对文件路径
|
||||||
|
*/
|
||||||
|
public static String upload(MultipartFile multipartFile, String fileDir, List<SysDepartInterfaceInfo> sysDepartInterfaceInfoList) {
|
||||||
|
for (SysDepartInterfaceInfo sysDepartInterfaceInfo : sysDepartInterfaceInfoList) {
|
||||||
|
String interfaceTypeKey = sysDepartInterfaceInfo.getInterfaceTypeKey();//接口参数
|
||||||
|
String interfaceTypeValue = sysDepartInterfaceInfo.getInterfaceTypeValue();//接口参数值
|
||||||
|
switch (interfaceTypeKey) {
|
||||||
|
case "bucket":
|
||||||
|
bucket = interfaceTypeValue;
|
||||||
|
break;
|
||||||
|
case "host":
|
||||||
|
host = interfaceTypeValue;
|
||||||
|
break;
|
||||||
|
case "accessKey":
|
||||||
|
accessKey = interfaceTypeValue;
|
||||||
|
break;
|
||||||
|
case "secretKey":
|
||||||
|
secretKey = interfaceTypeValue;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try (InputStream inputStream = multipartFile.getInputStream()) {
|
||||||
|
//初始化minio
|
||||||
|
initMinio();
|
||||||
|
// bucket 不存在,创建
|
||||||
|
if (!minioClient.bucketExists(bucket)) {
|
||||||
|
minioClient.makeBucket(bucket);
|
||||||
|
}
|
||||||
|
// 上传文件的名称
|
||||||
|
String fileName = multipartFile.getOriginalFilename();
|
||||||
|
long timestamp = Calendar.getInstance().getTimeInMillis();
|
||||||
|
// PutObjectOptions,上传配置(文件大小,内存中文件分片大小)
|
||||||
|
// PutObjectOptions putObjectOptions = new PutObjectOptions(multipartFile.getSize(), PutObjectOptions.MIN_MULTIPART_SIZE);
|
||||||
|
// // 文件的ContentType
|
||||||
|
// putObjectOptions.setContentType(multipartFile.getContentType());
|
||||||
|
|
||||||
|
SimpleDateFormat sdYear = new SimpleDateFormat("yyyy");
|
||||||
|
SimpleDateFormat sdMonth = new SimpleDateFormat("MM");
|
||||||
|
SimpleDateFormat sdDay = new SimpleDateFormat("dd");
|
||||||
|
String year = sdYear.format(new Date());
|
||||||
|
String month = sdMonth.format(new Date());
|
||||||
|
String day = sdDay.format(new Date());
|
||||||
|
String filrDir = "wlhy/" + year + "/" + month + "/" + day + "/";
|
||||||
|
|
||||||
|
// minioClient.putObject(bucket, filrDir+timestamp+fileName, inputStream, putObjectOptions);
|
||||||
|
minioClient.putObject(bucket, filrDir+timestamp+fileName, inputStream, multipartFile.getContentType());
|
||||||
|
// 返回访问路径
|
||||||
|
String fileurl = host+ "/"+bucket + "/"+filrDir+timestamp+ UriUtils.encode(fileName, StandardCharsets.UTF_8);
|
||||||
|
System.out.println("fileurl===="+fileurl);
|
||||||
|
FILE_URL = fileurl;
|
||||||
|
return fileurl;
|
||||||
|
} catch (ServiceException e){
|
||||||
|
log.error(e.getErrorMessage());
|
||||||
|
} catch (Exception e){
|
||||||
|
log.error(e.getMessage());
|
||||||
|
}
|
||||||
|
return FILE_URL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件 带时间水印
|
||||||
|
* 文件上传成功,返回文件完整访问路径
|
||||||
|
* 文件上传失败,返回 null
|
||||||
|
*
|
||||||
|
* @param multipartFile 待上传文件
|
||||||
|
* @param fileDir 文件保存目录
|
||||||
|
* @return oss 中的相对文件路径
|
||||||
|
*/
|
||||||
|
public static String uploadWaterMark(MultipartFile multipartFile, WaterMarkDTO waterMarkDTO, String fileDir, List<SysDepartInterfaceInfo> sysDepartInterfaceInfoList) {
|
||||||
|
for (SysDepartInterfaceInfo sysDepartInterfaceInfo : sysDepartInterfaceInfoList) {
|
||||||
|
String interfaceTypeKey = sysDepartInterfaceInfo.getInterfaceTypeKey();//接口参数
|
||||||
|
String interfaceTypeValue = sysDepartInterfaceInfo.getInterfaceTypeValue();//接口参数值
|
||||||
|
switch (interfaceTypeKey) {
|
||||||
|
case "bucket":
|
||||||
|
bucket = interfaceTypeValue;
|
||||||
|
break;
|
||||||
|
case "host":
|
||||||
|
host = interfaceTypeValue;
|
||||||
|
break;
|
||||||
|
case "accessKey":
|
||||||
|
accessKey = interfaceTypeValue;
|
||||||
|
break;
|
||||||
|
case "secretKey":
|
||||||
|
secretKey = interfaceTypeValue;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 压缩代码
|
||||||
|
InputStream inputStream = null;
|
||||||
|
try {
|
||||||
|
//初始化minio
|
||||||
|
initMinio();
|
||||||
|
// bucket 不存在,创建
|
||||||
|
if (!minioClient.bucketExists(bucket)) {
|
||||||
|
minioClient.makeBucket(bucket);
|
||||||
|
}
|
||||||
|
String OriginalFilename = multipartFile.getOriginalFilename();
|
||||||
|
inputStream = multipartFile.getInputStream();
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
//水印内容
|
||||||
|
String waterMarkContent = sdf.format(new Date());
|
||||||
|
//以下两种方法加水印
|
||||||
|
//方法1
|
||||||
|
if (OriginalFilename.indexOf(".pdf") < 0) {
|
||||||
|
ByteArrayOutputStream byteArrayOutputStreamut = new ByteArrayOutputStream();
|
||||||
|
Thumbnails.of(inputStream).width(1024).keepAspectRatio(true).outputQuality(0.9).toOutputStream(byteArrayOutputStreamut);
|
||||||
|
inputStream = new ByteArrayInputStream(byteArrayOutputStreamut.toByteArray());
|
||||||
|
//添加水印
|
||||||
|
inputStream = OssBootUtil.buildWatermarkOfText(inputStream, waterMarkDTO, OssBootUtil.WaterMarkLocation.SOUTH_WEST, null, 0.5f);
|
||||||
|
}
|
||||||
|
//方法2
|
||||||
|
/*if (ImageUtil.isImage(multipartFile.getOriginalFilename())) {
|
||||||
|
inputStream = ImageUtil.getInputStream(
|
||||||
|
ImageUtil.setWatermark(
|
||||||
|
ImageUtil.compress(
|
||||||
|
ImageIO.read(inputStream)),waterMarkContent),
|
||||||
|
ImageUtil.getFileExtention(multipartFile.getOriginalFilename()));
|
||||||
|
}*/
|
||||||
|
StringBuilder fileUrl = new StringBuilder();
|
||||||
|
String suffix = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf('.'));
|
||||||
|
// 上传文件的名称
|
||||||
|
String fileName = multipartFile.getOriginalFilename();
|
||||||
|
long timestamp = Calendar.getInstance().getTimeInMillis();
|
||||||
|
// PutObjectOptions,上传配置(文件大小,内存中文件分片大小)
|
||||||
|
// PutObjectOptions putObjectOptions = new PutObjectOptions(multipartFile.getSize(), PutObjectOptions.MIN_MULTIPART_SIZE);
|
||||||
|
// // 文件的ContentType
|
||||||
|
// putObjectOptions.setContentType(multipartFile.getContentType());
|
||||||
|
|
||||||
|
SimpleDateFormat sdYear = new SimpleDateFormat("yyyy");
|
||||||
|
SimpleDateFormat sdMonth = new SimpleDateFormat("MM");
|
||||||
|
SimpleDateFormat sdDay = new SimpleDateFormat("dd");
|
||||||
|
String year = sdYear.format(new Date());
|
||||||
|
String month = sdMonth.format(new Date());
|
||||||
|
String day = sdDay.format(new Date());
|
||||||
|
String filrDir = "wlhy/" + year + "/" + month + "/" + day + "/";
|
||||||
|
|
||||||
|
// minioClient.putObject(bucket, filrDir+timestamp+fileName, inputStream, putObjectOptions);
|
||||||
|
minioClient.putObject(bucket, filrDir+timestamp+fileName, inputStream, multipartFile.getContentType());
|
||||||
|
// 返回访问路径
|
||||||
|
String fileurl = host+ "/"+bucket + "/"+filrDir+timestamp+ UriUtils.encode(fileName, StandardCharsets.UTF_8);
|
||||||
|
System.out.println("fileurl===="+fileurl);
|
||||||
|
FILE_URL = fileurl;
|
||||||
|
return fileurl;
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error(e.getMessage());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage());
|
||||||
|
}
|
||||||
|
return FILE_URL;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user