简介
Minio 分布式文件服务的安装与SpringBoot集成 封装Utils
Minio安装
mkdir /home/app/minio
cd /home/app/minio
wget https://dl.min.io/server/minio/release/linux-amd64/minio
#启动 设置用户名 密码 控制台端口 API端口 配置文件地址 数据地址
MINIO_ACCESS_KEY=root MINIO_SECRET_KEY=cR1eI7jI nohup ./minio server --console-address 0.0.0.0:8006 --address 0.0.0.0:8003 --config-dir /home/app/minio /home/app/minio/data > /opt/logs/minio.log 2>&1 &
SpringBoot 集成
添加依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.3.3</version>
</dependency>
添加配置文件
spring
minio:
url: ip:9016
access-key: root
secret-key: cR1eI7jI
bucket: test
Java配置文件
import com.iscm.utils.MinioUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MinioConfig {
/**
* url
*/
@Value(value = "${spring.minio.url}")
private String minioUrl;
/**
* username
*/
@Value(value = "${spring.minio.access-key}")
private String minioName;
/**
* password
*/
@Value(value = "${spring.minio.secret-key}")
private String minioPass;
@Value(value = "${spring.minio.bucket}")
private String bucketName;
@Bean
public void initMinio() {
if (!minioUrl.startsWith("http")) {
minioUrl = "http://" + minioUrl;
}
if (!minioUrl.endsWith("/")) {
minioUrl = minioUrl.concat("/");
}
MinioUtils.setMinioUrl(minioUrl);
MinioUtils.setMinioName(minioName);
MinioUtils.setMinioPass(minioPass);
MinioUtils.setBucketName(bucketName);
}
}
工具类
import io.minio.BucketExistsArgs;
import io.minio.GetObjectArgs;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.ListObjectsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import io.minio.Result;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;
import io.minio.http.Method;
import io.minio.messages.Item;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.h2.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/**
* @ClassName MinioUtils
* @Description 文件服务工具类
* @Author zhanghan
* @Date 2022/6/21 15:11
* @Version V1.0
*/
public class MinioUtils {
private static Log log = LogFactory.getLog(MinioUtils.class);
private static String minioUrl;
private static String minioName;
private static String minioPass;
private static String bucketName;
public static void setMinioUrl(String minioUrl) {
MinioUtils.minioUrl = minioUrl;
}
public static void setMinioName(String minioName) {
MinioUtils.minioName = minioName;
}
public static void setMinioPass(String minioPass) {
MinioUtils.minioPass = minioPass;
}
public static void setBucketName(String bucketName) {
MinioUtils.bucketName = bucketName;
}
private static MinioClient minioClient = null;
/**
* 判断文件名是否带盘符,重新处理
*
* @param fileName
* @return
*/
public static String getFileName(String fileName) {
//判断是否带有盘符信息
int unixSep = fileName.lastIndexOf('/');
int winSep = fileName.lastIndexOf('\\');
int pos = (winSep > unixSep ? winSep : unixSep);
if (pos != -1) {
fileName = fileName.substring(pos + 1);
}
//替换上传文件名字的特殊字符
fileName = fileName.replace("=", "").replace(",", "").replace("&", "")
.replace("#", "").replace("“", "").replace("”", "");
//替换上传文件名字中的空格
fileName = fileName.replaceAll("\\s", "");
return fileName;
}
/**
* 清除掉所有特殊字符
*
* @param str
* @return
* @throws PatternSyntaxException
*/
public static String filter(String str) throws PatternSyntaxException {
if (StringUtils.isNullOrEmpty(str)) {
return str;
}
// 清除掉所有特殊字符
String regEx = "[`_《》~!@#$%^&*()+=|{}':;',\\[\\].<>?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.replaceAll("").trim();
}
/**
* 上传文件
*
* @param file
* @return
*/
public static String upload(MultipartFile file, String bizPath, String customBucket) {
String fileUrl = "";
bizPath = filter(bizPath);
String newBucket = bucketName;
if (customBucket != null) {
newBucket = customBucket;
}
try {
initMinio(minioUrl, minioName, minioPass);
// 检查存储桶是否已经存在
if (minioClient.bucketExists(BucketExistsArgs.builder().bucket(newBucket).build())) {
log.info("Bucket already exists.");
} else {
// 创建一个名为ota的存储桶
minioClient.makeBucket(MakeBucketArgs.builder().bucket(newBucket).build());
log.info("create a new bucket.");
}
InputStream stream = file.getInputStream();
// 获取文件名
String orgName = file.getOriginalFilename();
if ("".equals(orgName)) {
orgName = file.getName();
}
orgName = getFileName(orgName);
String objectName = bizPath + "/"
+ (orgName.indexOf(".") == -1
? orgName + "_" + System.currentTimeMillis()
: orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."))
);
// 使用putObject上传一个本地文件到存储桶中。
if (objectName.startsWith("/")) {
objectName = objectName.substring(1);
}
PutObjectArgs objectArgs = PutObjectArgs.builder().object(objectName)
.bucket(newBucket)
.contentType("application/octet-stream")
.stream(stream, stream.available(), -1).build();
minioClient.putObject(objectArgs);
stream.close();
fileUrl = objectName;
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return fileUrl;
}
/**
* 文件上传
*
* @param file
* @param bizPath
* @return
*/
public static String upload(MultipartFile file, String bizPath) {
return upload(file, bizPath, null);
}
/**
* 文件上传
*
* @param file
* @return
*/
public static String upload(MultipartFile file) {
return upload(file, "", null);
}
/**
* 获取文件流
*
* @param bucketName
* @param objectName
* @return
*/
public static InputStream getMinioFile(String bucketName, String objectName) {
InputStream inputStream = null;
try {
initMinio(minioUrl, minioName, minioPass);
GetObjectArgs objectArgs = GetObjectArgs.builder().object(objectName)
.bucket(bucketName).build();
inputStream = minioClient.getObject(objectArgs);
} catch (Exception e) {
log.info("文件获取失败" + e.getMessage());
}
return inputStream;
}
/**
* 删除文件
*
* @param bucketName
* @param objectName
* @throws Exception
*/
public static void removeObject(String bucketName, String objectName) {
try {
initMinio(minioUrl, minioName, minioPass);
RemoveObjectArgs objectArgs = RemoveObjectArgs.builder().object(objectName)
.bucket(bucketName).build();
minioClient.removeObject(objectArgs);
} catch (Exception e) {
log.info("文件删除失败" + e.getMessage());
}
}
/**
* 获取文件外链
*
* @param bucketName
* @param objectName
* @param expires
* @return
*/
public static String getObjectURL(String bucketName, String objectName, Integer expires) {
initMinio(minioUrl, minioName, minioPass);
try {
GetPresignedObjectUrlArgs objectArgs = GetPresignedObjectUrlArgs.builder().object(objectName)
.bucket(bucketName)
.expiry(expires).build();
String url = minioClient.getPresignedObjectUrl(objectArgs);
return URLDecoder.decode(url, "UTF-8");
} catch (Exception e) {
log.info("文件路径获取失败" + e.getMessage());
}
return null;
}
/**
* 初始化客户端
*
* @param minioUrl
* @param minioName
* @param minioPass
* @return
*/
private static MinioClient initMinio(String minioUrl, String minioName, String minioPass) {
if (minioClient == null) {
try {
minioClient = MinioClient.builder()
.endpoint(minioUrl)
.credentials(minioName, minioPass)
.build();
} catch (Exception e) {
e.printStackTrace();
}
}
return minioClient;
}
/**
* 上传文件到minio
*
* @param stream
* @param relativePath
* @return
*/
public static String upload(InputStream stream, String relativePath) throws Exception {
initMinio(minioUrl, minioName, minioPass);
if (minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
log.info("Bucket already exists.");
} else {
// 创建一个名为ota的存储桶
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
log.info("create a new bucket.");
}
PutObjectArgs objectArgs = PutObjectArgs.builder().object(relativePath)
.bucket(bucketName)
.contentType("application/octet-stream")
.stream(stream, stream.available(), -1).build();
minioClient.putObject(objectArgs);
stream.close();
return minioUrl + bucketName + "/" + relativePath;
}
/**
* 列出桶内所有对象(added by caixibei)
*
* @param bucketName 存储桶名称
* @param prefix 对象名称的前缀,列出有该前缀的对象,如果为null ,表示查全部
* @param recursive 是否递归查找,如果是false,就模拟文件夹结构查找
*/
public static List<Map<String, String>> listObjects(String bucketName, String prefix, boolean recursive, boolean useVersion1) throws Exception {
initMinio(minioUrl, minioName, minioPass);
List<Map<String, String>> objLists = new ArrayList<>();
// 检查桶是否存在
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (found) {
// 列出桶里的对象
Iterable<Result<Item>> myObjects = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(prefix).recursive(recursive).useApiVersion1(useVersion1).build());
for (Result<Item> result : myObjects) {
Map<String, String> map = new HashMap<>();
Item item = result.get();
map.put("lastModified", item.lastModified().format(DateTimeFormatter.BASIC_ISO_DATE));
map.put("size", String.valueOf(item.size()));
map.put("fileName", item.objectName());
map.put("url", getObjectUrl(bucketName, Method.GET, item.objectName()));
objLists.add(map);
}
} else {
throw new Exception("【Error】不存在桶:" + bucketName);
}
return objLists;
}
/**
* 获取某一个存储对象的下载链接(added by caixibei)
*
* @param bucketName 桶名
* @param method 方法类型
* @param objectName 对象名
* @return url 下载链接
* @throws ServerException 服务异常
*/
public static String getObjectUrl(String bucketName, Method method, String objectName) throws Exception {
return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
.method(method)
.bucket(bucketName)
.object(objectName).build());
}
public static String getObjectUrl(String objectName) {
try {
return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
.method(Method.GET)
.bucket(bucketName)
.expiry(1, TimeUnit.DAYS)
.object(objectName).build());
} catch (ErrorResponseException e) {
e.printStackTrace();
} catch (InsufficientDataException e) {
e.printStackTrace();
} catch (InternalException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidResponseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (XmlParserException e) {
e.printStackTrace();
} catch (ServerException e) {
e.printStackTrace();
}
return null;
}
}
评论区