package org.dromara.demo.util;
|
|
|
import cn.hutool.core.util.IdUtil;
|
import io.minio.MinioClient;
|
import io.minio.PutObjectOptions;
|
import jakarta.annotation.PostConstruct;
|
import lombok.Data;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
import org.dromara.demo.domain.MinioResult;
|
import org.dromara.demo.exception.UploadFailException;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.stereotype.Component;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.io.ByteArrayInputStream;
|
|
/**
|
* @author gonghl
|
* @date 2024-3-6
|
*/
|
@Component
|
@ConfigurationProperties(prefix = "minio")
|
@Slf4j
|
@Data
|
public class MinioUtil {
|
|
|
private String url;
|
private String accessKey;
|
private String secretKey;
|
private MinioClient client;
|
|
|
public MinioResult uploadFile(MultipartFile file, String bucketName) {
|
try {
|
if (!client.bucketExists(bucketName)) {
|
client.makeBucket(bucketName);
|
client.setBucketPolicy(bucketName, "public");
|
}
|
|
String[] split = file.getOriginalFilename().split("\\.");
|
String suffix = split[split.length - 1];
|
|
if (split.length < 2) {
|
suffix = "";
|
}
|
|
String fileName = StringUtils.join(IdUtil.randomUUID(), ".", suffix);
|
|
client.putObject(bucketName, fileName, file.getInputStream(), new PutObjectOptions(file.getSize(), 5 * 1024 * 1024));
|
String fileUrl = getFilePath(bucketName, fileName);
|
if (StringUtils.isNotBlank(fileUrl)) {
|
return new MinioResult(bucketName, fileName, fileUrl, file.getOriginalFilename(), null);
|
}
|
} catch (Exception e) {
|
log.error("上传失败,失败详情信息:{}", e.getMessage());
|
throw new UploadFailException();
|
}
|
return null;
|
|
}
|
|
public MinioResult uploadFile(ByteArrayInputStream in, String name, String bucketName) {
|
|
String[] split = name.split("\\.");
|
String suffix = split[split.length - 1];
|
if (split.length < 2) {
|
suffix = "";
|
}
|
String fileName = StringUtils.join(IdUtil.randomUUID(), ".", suffix);
|
try {
|
client.putObject(bucketName, fileName, in, new PutObjectOptions(in.available(), 5 * 1024 * 1024));
|
String fileUrl = getFilePath(bucketName, fileName);
|
if (StringUtils.isNotBlank(fileUrl)) {
|
return new MinioResult(bucketName, fileName, fileUrl, name, null);
|
}
|
} catch (Exception e) {
|
log.error("上传失败,失败详情信息:{}", e.getMessage());
|
throw new UploadFailException();
|
}
|
|
return null;
|
|
}
|
|
public String uploadImage(ByteArrayInputStream in, String bucketName) {
|
try {
|
String fileName = IdUtil.randomUUID() + ".jpg";
|
client.putObject(bucketName, fileName, in, new PutObjectOptions(in.available(), 5 * 1024 * 1024));
|
String fileUrl = getFilePath(bucketName, fileName);
|
if (StringUtils.isNotBlank(fileUrl)) {
|
return fileUrl;
|
}
|
} catch (Exception e) {
|
log.error("上传失败,失败详情信息:{}", e.getMessage());
|
throw new UploadFailException();
|
}
|
|
return null;
|
|
}
|
|
public String getFilePath(String bucketName, String fileName) {
|
String wordUrl = null;
|
try {
|
if (client.getObject(bucketName, fileName) == null) {
|
return null;
|
}
|
|
return StringUtils.join("/", bucketName, "/", fileName);
|
} catch (Exception e) {
|
log.error("获取bucket为:{},文件名为{}时出错,原因:{}", bucketName, fileName, e.getMessage());
|
}
|
|
return null;
|
}
|
|
|
@PostConstruct
|
public void creatConnect() throws Exception {
|
this.client = new MinioClient(url, accessKey, secretKey);
|
|
|
}
|
|
}
|