MinIO 是一个基于Apache License v2.0开源协议的对象存储服务。它兼容亚马逊S3云存储服务接口,非常适合于存储大容量非结构化的数据,例如图片、视频、日志文件、备份数据和容器/虚拟机镜像等,而一个对象文件可以是任意大小,从几kb到最大5T不等。
MinIO是一个非常轻量的服务,可以很简单的和其他应用的结合,类似 NodeJS, Redis 或者 MySQL。
参考:https://docs.min.io/cn/minio-quickstart-guide.html
MinIO环境搭建,官网案例是在docker安装,因为本地没安装docker,所以直接在centos7 中安装MinIO
1.创建目录
mkdir /usr/local/minio
mkdir /usr/local/minio/log
mkdir /usr/local/minio/data
进入 minio目录 cd /usr/local/minio
wget https://dl.min.io/server/minio/release/linux-amd64/minio
3.授权
chmod +x minio
4.检查9000端口号是否打开
firewall-cmd --list-ports ##查询开放端口
firewall-cmd --zone=public --add-port=9000/tcp --permanent ##打开端口
systemctl restart firewalld ##重启防火墙
5.启动服务
/usr/local/minio/minio server /usr/local/minio/data
默认账号密码:minioadmin minioadmin
启动成功,访问http://192.168.3.68:9000/
进行MinIO首页,创建空间
6.后台启动服务 (第5点启动服务,关闭linux的连接会关闭MinIO服务,跟java项目同理),建议使用以下命令:
nohup /usr/local/minio/minio server /usr/local/minio/data > /usr/local/minio/log/minio.log 2>&1 &
7.Java代码调用
引用maven依赖
io.minio
minio
4.0.0
Java代码:
package com.kamkiu.common.util;
import io.minio.MinioClient;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
/**
* minio文件上传工具类
*/
@Slf4j
public class MinioUtil {
private static String minioUrl="http://192.168.3.68:9000";
private static String minioName="minioadmin";
private static String minioPass="minioadmin";
private static String bucketName="kap-minin";
public static void setMinioUrl(String minioUrl) {
MinioUtil.minioUrl = minioUrl;
}
public static void setMinioName(String minioName) {
MinioUtil.minioName = minioName;
}
public static void setMinioPass(String minioPass) {
MinioUtil.minioPass = minioPass;
}
public static void setBucketName(String bucketName) {
MinioUtil.bucketName = bucketName;
}
public static String getMinioUrl() {
return minioUrl;
}
public static String getBucketName() {
return bucketName;
}
private static MinioClient minioClient = null;
/**
* 上传文件
* @param file
* @return
*/
public static String upload(MultipartFile file, String bizPath, String customBucket) {
String file_url = "";
String newBucket = bucketName;
if(oConvertUtils.isNotEmpty(customBucket)){
newBucket = customBucket;
}
try {
initMinio(minioUrl, minioName,minioPass);
// 检查存储桶是否已经存在
if(minioClient.bucketExists(newBucket)) {
log.info("Bucket already exists.");
} else {
// 创建一个名为ota的存储桶
minioClient.makeBucket(newBucket);
log.info("create a new bucket.");
}
InputStream stream = file.getInputStream();
// 获取文件名
String orgName = file.getOriginalFilename();
if("".equals(orgName)){
orgName=file.getName();
}
orgName = CommonUtils.getFileName(orgName);
String objectName = bizPath+"/"+orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.indexOf("."));
// 使用putObject上传一个本地文件到存储桶中。
minioClient.putObject(newBucket,objectName, stream,stream.available(),"application/octet-stream");
stream.close();
file_url = minioUrl+newBucket+"/"+objectName;
}catch (IOException e){
log.error(e.getMessage(), e);
} catch (InvalidKeyException e) {
log.error(e.getMessage(), e);
} catch (NoSuchAlgorithmException e) {
log.error(e.getMessage(), e);
} catch (NoResponseException e) {
log.error(e.getMessage(), e);
} catch (XmlPullParserException e) {
log.error(e.getMessage(), e);
} catch (InvalidArgumentException e) {
log.error(e.getMessage(), e);
} catch (RegionConflictException e) {
log.error(e.getMessage(), e);
} catch (InvalidBucketNameException e) {
log.error(e.getMessage(), e);
} catch (ErrorResponseException e) {
log.error(e.getMessage(), e);
} catch (InternalException e) {
log.error(e.getMessage(), e);
} catch (InsufficientDataException e) {
log.error(e.getMessage(), e);
}
return file_url;
}
/**
* 文件上传
* @param file
* @param bizPath
* @return
*/
public static String upload(MultipartFile file, String bizPath) {
return upload(file,bizPath,null);
}
/**
* 获取文件流
* @param bucketName
* @param objectName
* @return
*/
public static InputStream getMinioFile(String bucketName,String objectName){
InputStream inputStream = null;
try {
initMinio(minioUrl, minioName, minioPass);
inputStream = minioClient.getObject(bucketName, objectName);
} 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);
minioClient.removeObject(bucketName, objectName);
}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{
String url = minioClient.presignedGetObject(bucketName, objectName, expires);
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 = new MinioClient(minioUrl, minioName,minioPass);
} catch (InvalidEndpointException e) {
e.printStackTrace();
} catch (InvalidPortException e) {
e.printStackTrace();
}
}
return minioClient;
}
/**
* 上传文件到minio
* @param stream
* @param relativePath
* @return
*/
public static String upload(InputStream stream,String relativePath) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException, RegionConflictException, InvalidArgumentException {
initMinio(minioUrl, minioName,minioPass);
if(minioClient.bucketExists(bucketName)) {
log.info("Bucket already exists.");
} else {
// 创建一个名为ota的存储桶
minioClient.makeBucket(bucketName);
log.info("create a new bucket.");
}
minioClient.putObject(bucketName,relativePath, stream, stream.available(),"application/octet-stream");
stream.close();
return minioUrl+bucketName+"/"+relativePath;
}
}
更多配置可参考:https://www.cnblogs.com/newz/p/12598899.html