MinIO的安装与使用
一、MinIO是什么?
- MinIO 是一款高性能、分布式的对象存储系统. 它是一款软件产品, 可以100%的运行在标准硬件。即X86等低成本机器也能够很好的运行MinIO。
- MinIO与传统的存储和其他的对象存储不同的是:它一开始就针对性能要求更高的私有云标准进行软件架构设计。因为MinIO一开始就只为对象存储而设计。所以他采用了更易用的方式进行设计,它能实现对象存储所需要的全部功能,在性能上也更加强劲,它不会为了更多的业务功能而妥协,失去MinIO的易用性、高效性。 这样的结果所带来的好处是:它能够更简单的实现局有弹性伸缩能力的原生对象存储服务。
- MinIO在传统对象存储用例(例如辅助存储,灾难恢复和归档)方面表现出色。同时,它在机器学习、大数据、私有云、混合云等方面的存储技术上也独树一帜。当然,也不排除数据分析、高性能应用负载、原生云的支持。
- MinIO主要采用Golang语言实现,,客户端与存储服务器之间采用http/https通信协议。
- 它与 Amazon S3 云存储服务 API 兼容MinIO的相关信息中文官网: http://www.minio.org.cn/ 中文文档: http://docs.minio.org.cn/docs/ 中文下载地址:http://www.minio.org.cn/download.shtml#/linux 英文官网: https://min.io/ 英文文档: https://docs.min.io/ 英文下载地址:https://min.io/download#/linux Github地址:https://github.com/minio/minio
二、MinIO安装(centos7)
2.1 下载MinIO
演示以官网下载二进制文件配置,下载地址见上方,,,,docker安装更简单哦~
#创建目录mkdir /usr/local/minio/
cd /usr/local/minio/
#下载并添加权限wget http://dl.minio.org.cn/server/minio/release/linux-amd64/minio
#赋值权限chmod +x minio
2.2 启动MinIO
#创建数据目录,数据目录存储需要大点mkdir -p /home/data/minio
#创建日志目录mkdir -p /home/data/minio/log
touch /home/data/minio/log/minio.log
#前台启动minio
./minio server /home/data/minio
#后台启动minionohup ./minio server /home/data/minio > /home/data/minio/log/minio.log &# nohup端口自定义启动服务 指定文件存放路径 /home/data/minio 还有设置日志文件路径 /home/data/minio/log/minio.lognohup ./minio server --address :9000 --console-address :9001 /home/data/minio > /home/data/minio/log/minio.log 2>&1&
–address :9000 --console-address :9001 是配置端口,默认minio端口是9000,如果9000端口被占用了,那就加上这一串配置,端口号的冒号之前不需要特意写出ip,当然如果你的ip的动态变化的,而不是静态的话,前边的ip不用写上,当然最好是用静态的ip
关闭防火墙
如果未关闭,输入以下命令:
systemctl stop firewalld
注意:如果不想关闭防火墙,需要开放ip端口9000和设置静态控制访问IP
访问:
http://192.168.92.100:9000
查看控制台
这里我们都使用的默认配置账号密码
minioadmin:minioadmin
2.3 修改配置
这里账号和密码都是用的默认的,如果我们要修改可以在环境变量里设置
修改环境变量
- 打开 /etc/profile 文件
vim /etc/profile
- 在文件的最末尾加上以下信息(启动的时候看提示,新版本需要用
MINIO_ROOT_USER
和MINIO_ROOT_PASSWORD
,旧版需要用MINIO_ACCESS_KEY
和MINIO_SECRET_KEY
)。 按 i 键后,在文档末尾输入 (1)新版:exportMINIO_ROOT_USER=minioadminexportMINIO_ROOT_PASSWORD=admin123
(2)旧版exportMINIO_ACCESS_KEY=minioadminexportMINIO_SECRET_KEY=admin123
- 保存退出,刷新重载环境变量
source /etc/profile
2.4 编写启动脚本,以及加入到systemctl中
为了方便管理,我们这里给命令添加到脚本中
- 在
/usr/local/minio/
目录下新建run.sh``````vim run.sh
- 然后将以下内容保存到
run.sh
,并为其赋予执行权限chmod +x run.sh``````#!/bin/bash#配置登陆账号密码exportMINIO_ROOT_USER=minioadminexportMINIO_ROOT_PASSWORD=admin123# nohup启动服务 指定文件存放路径 /home/data/minio 还有设置日志文件路径 /home/data/minio/log/minio.lognohup ./minio server --address :9000 --console-address :9001 /home/data/minio > /home/data/minio/log/minio.log 2>&1&
- 然后启动minio
# 启动minio服务bash run.sh# 查看日志tail -200f /home/data/minio/log/minio.log
然后会有日志打印信息,然后可以看到minio服务器地址,和控制台信息地址然后在浏览器中访问地址http://192.168.92.100:9000
,输入这个地址后会重定向到控制台登录地址http://192.168.92.100:9001/login
添加到systemctl启动命令中
- 编写minio.service文件
vim /usr/lib/systemd/system/minio.service
填写以下内容[Unit]Description=minioDocumentation=https://docs.min.ioWants=network-online.targetAfter=network-online.targetAssertFileIsExecutable=/usr/local/minio/minio[Service]#User and groupUser=rootGroup=rootExecStart=/usr/local/minio/run.sh#Let systemd restart this service alwaysRestart=always#Specifies the maximum file descriptor number that can be opened by this processLimitNOFILE=65536#Disable timeout logic and wait until process is stoppedTimeoutStopSec=infinitySendSIGKILL=no[Install]WantedBy=multi-user.target
- 赋值权限并且启动
chmod +x /usr/lib/systemd/system/minio.service
启动、查看、设置开机启动systemctl daemon-reloadsystemctl start miniosystemctl enable miniosystemctl status minio
启动报错处理systemctl start minioAssertion failed on job for minio.service.# 是 minio.service 的 AssertFileIsExecutable 路径错误AssertFileIsExecutable=/usr/local/minio# 改为AssertFileIsExecutable=/usr/local/minio/minio
到这里单机版的已经安装完毕!
三、Springboot集成MinIO
3.1 项目应用
- 项目的pom文件中引入minio依赖
<properties><minio.version>7.1.0</minio.version><dependencies><dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>${minio.version}</version></dependency></dependencies>
- 在
application.yml
文件中配置minio
minio:
endpoint: http://192.168.92.100
port:9000
accessKey: minioadmin
secretKey: admin123
bucketName: test
secure:false
spring:
#设置文件上传大小限制
servlet:
multipart:
max-file-size:100MB
max-request-size:150MB
- 创建minio配置类和工具类配置类:
packagecn.cvzhanshi.wechatpush.config;importio.minio.MinioClient;importio.minio.errors.InvalidPortException;importlombok.Getter;importlombok.Setter;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.stereotype.Component;@Configuration@Component@ConfigurationProperties(prefix ="minio")@Getter@SetterpublicclassMinioConfig{privateString endpoint;privateint port;privateString accessKey;privateString secretKey;privateBoolean secure;privateString bucketName;@BeanpublicMinioClientgetMinioClient()throwsInvalidPortException{MinioClient minioClient =MinioClient.builder().endpoint(endpoint, port, secure).credentials(accessKey, secretKey).build();return minioClient;}//// @Bean(name = "multipartResolver")// public MultipartResolver multipartResolver(){// CommonsMultipartResolver resolver = new CommonsMultipartResolver();// resolver.setDefaultEncoding("UTF-8");// //resolveLazily属性启用是为了推迟文件解析,以在在UploadAction中捕获文件大小异常// resolver.setResolveLazily(true);// resolver.setMaxInMemorySize(40960);// //上传文件大小 50M 50*1024*1024// resolver.setMaxUploadSize(50*1024*1024);// return resolver;// }}
工具类:
packagecn.cvzhanshi.wechatpush.utils;importio.minio.*;importio.minio.errors.*;importio.minio.http.Method;importio.minio.messages.Bucket;importio.minio.messages.DeleteError;importio.minio.messages.DeleteObject;importio.minio.messages.Item;importlombok.extern.slf4j.Slf4j;importorg.apache.commons.lang3.StringUtils;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Component;importorg.springframework.web.multipart.MultipartFile;importjavax.servlet.ServletOutputStream;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;importjava.io.InputStream;importjava.nio.charset.StandardCharsets;importjava.security.InvalidKeyException;importjava.security.NoSuchAlgorithmException;importjava.util.ArrayList;importjava.util.List;/**
* MinIO 客户端工具类
*/@Component@Slf4jpublicclassMinioClientUtils{@AutowiredprivateMinioClient minioClient;privatestaticfinalint DEFAULT_EXPIRY_TIME =7*24*3600;/**
* 检查存储桶是否存在
*
* @param bucketName 存储桶名称
* @return boolean
*/publicbooleanbucketExists(String bucketName)throwsIOException,InvalidKeyException,InvalidResponseException,InsufficientDataException,NoSuchAlgorithmException,ServerException,InternalException,XmlParserException,InvalidBucketNameException,ErrorResponseException{boolean flag =false;
flag = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());if(flag){returntrue;}returnfalse;}/**
* 创建存储桶
*
* @param bucketName 存储桶名称
*/publicbooleanmakeBucket(String bucketName)throwsIOException,InvalidKeyException,InvalidResponseException,RegionConflictException,InsufficientDataException,NoSuchAlgorithmException,ServerException,InternalException,XmlParserException,InvalidBucketNameException,ErrorResponseException{boolean flag =bucketExists(bucketName);if(!flag){
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());returntrue;}else{returnfalse;}}/**
* 列出所有存储桶名称
*
* @return List<String>
*/publicList<String>listBucketNames()throwsIOException,InvalidResponseException,InvalidKeyException,NoSuchAlgorithmException,ServerException,ErrorResponseException,XmlParserException,InvalidBucketNameException,InsufficientDataException,InternalException{List<Bucket> bucketList =listBuckets();List<String> bucketListName =newArrayList<>();for(Bucket bucket : bucketList){
bucketListName.add(bucket.name());}return bucketListName;}/**
* 列出所有存储桶
*
* @return List<Bucket>
*/publicList<Bucket>listBuckets()throwsIOException,InvalidKeyException,InvalidResponseException,InsufficientDataException,NoSuchAlgorithmException,ServerException,InternalException,XmlParserException,InvalidBucketNameException,ErrorResponseException{return minioClient.listBuckets();}/**
* 删除存储桶
*
* @param bucketName 存储桶名称
* @return boolean
*/publicbooleanremoveBucket(String bucketName)throwsIOException,InvalidKeyException,InvalidResponseException,InsufficientDataException,NoSuchAlgorithmException,ServerException,InternalException,XmlParserException,InvalidBucketNameException,ErrorResponseException{boolean flag =bucketExists(bucketName);if(flag){Iterable<Result<Item>> myObjects =listObjects(bucketName);for(Result<Item> result : myObjects){Item item = result.get();// 有对象文件,则删除失败if(item.size()>0){returnfalse;}}// 删除存储桶,注意,只有存储桶为空时才能删除成功。
minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
flag =bucketExists(bucketName);if(!flag){returntrue;}}returnfalse;}/**
* 列出存储桶中的所有对象名称
*
* @param bucketName 存储桶名称
* @return List<String>
*/publicList<String>listObjectNames(String bucketName)throwsXmlParserException,IOException,InvalidResponseException,InvalidKeyException,NoSuchAlgorithmException,ServerException,ErrorResponseException,InvalidBucketNameException,InsufficientDataException,InternalException{List<String> listObjectNames =newArrayList<>();boolean flag =bucketExists(bucketName);if(flag){Iterable<Result<Item>> myObjects =listObjects(bucketName);for(Result<Item> result : myObjects){Item item = result.get();
listObjectNames.add(item.objectName());}}return listObjectNames;}/**
* 列出存储桶中的所有对象
*
* @param bucketName 存储桶名称
* @return Iterable<Result<Item>>
*/publicIterable<Result<Item>>listObjects(String bucketName)throwsXmlParserException,IOException,InvalidResponseException,InvalidKeyException,NoSuchAlgorithmException,ServerException,ErrorResponseException,InvalidBucketNameException,InsufficientDataException,InternalException{boolean flag =bucketExists(bucketName);if(flag){return minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build());}returnnull;}/**
* 通过文件上传到对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param fileName File name
* @return boolean
*/publicbooleanuploadObject(String bucketName,String objectName,String fileName)throwsIOException,InvalidKeyException,InvalidResponseException,InsufficientDataException,NoSuchAlgorithmException,ServerException,InternalException,XmlParserException,InvalidBucketNameException,ErrorResponseException{boolean flag =bucketExists(bucketName);if(flag){
minioClient.uploadObject(UploadObjectArgs.builder().bucket(bucketName).object(objectName).filename(fileName).build());ObjectStat statObject =statObject(bucketName, objectName);if(statObject !=null&& statObject.length()>0){returntrue;}}returnfalse;}/**
* 文件上传
*
* @param bucketName 存储捅名称
* @param multipartFile 文件
* @param filename 文件名
*/publicvoidputObject(String bucketName,MultipartFile multipartFile,String filename)throwsIOException,InvalidKeyException,InvalidResponseException,InsufficientDataException,NoSuchAlgorithmException,ServerException,InternalException,XmlParserException,InvalidBucketNameException,ErrorResponseException{PutObjectOptions putObjectOptions =newPutObjectOptions(multipartFile.getSize(),PutObjectOptions.MIN_MULTIPART_SIZE);
putObjectOptions.setContentType(multipartFile.getContentType());
minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(filename).stream(
multipartFile.getInputStream(), multipartFile.getSize(),-1).contentType(multipartFile.getContentType()).build());}/**
* 通过InputStream上传对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param inputStream 要上传的流
* @param contentType 上传的文件类型 例如 video/mp4 image/jpg
* @return boolean
*/publicbooleanputObject(String bucketName,String objectName,InputStream inputStream,String contentType)throwsIOException,InvalidResponseException,InvalidKeyException,NoSuchAlgorithmException,ServerException,ErrorResponseException,XmlParserException,InvalidBucketNameException,InsufficientDataException,InternalException{boolean flag =bucketExists(bucketName);if(flag){
minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(//不清楚文件的大小时,可以传-1,10485760。如果知道大小也可以传入size,partsize。
inputStream,-1,10485760).contentType(contentType).build());ObjectStat statObject =statObject(bucketName, objectName);if(statObject !=null&& statObject.length()>0){returntrue;}}returnfalse;}/**
* 以流的形式获取一个文件对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return InputStream
*/publicInputStreamgetObject(String bucketName,String objectName)throwsIOException,InvalidKeyException,InvalidResponseException,InsufficientDataException,NoSuchAlgorithmException,ServerException,InternalException,XmlParserException,InvalidBucketNameException,ErrorResponseException{boolean flag =bucketExists(bucketName);if(flag){ObjectStat statObject =statObject(bucketName, objectName);if(statObject !=null&& statObject.length()>0){InputStream stream = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());return stream;}}returnnull;}/**
* 以流的形式获取一个文件对象(断点下载)
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param offset 起始字节的位置
* @param length 要读取的长度 (可选,如果无值则代表读到文件结尾)
* @return InputStream
*/publicInputStreamgetObject(String bucketName,String objectName,long offset,Long length)throwsIOException,InvalidKeyException,InvalidResponseException,InsufficientDataException,NoSuchAlgorithmException,ServerException,InternalException,XmlParserException,InvalidBucketNameException,ErrorResponseException{boolean flag =bucketExists(bucketName);if(flag){ObjectStat statObject =statObject(bucketName, objectName);if(statObject !=null&& statObject.length()>0){InputStream stream = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).offset(1024L).length(4096L).build());return stream;}}returnnull;}/**
* 下载并将文件保存到本地
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param fileName File name
* @return boolean
*/publicbooleandownloadObject(String bucketName,String objectName,String fileName)throwsIOException,InvalidKeyException,InvalidResponseException,InsufficientDataException,NoSuchAlgorithmException,ServerException,InternalException,XmlParserException,InvalidBucketNameException,ErrorResponseException{boolean flag =bucketExists(bucketName);if(flag){ObjectStat statObject =statObject(bucketName, objectName);if(statObject !=null&& statObject.length()>0){
minioClient.downloadObject(DownloadObjectArgs.builder().bucket(bucketName).object(objectName).filename(fileName).build());returntrue;}}returnfalse;}/**
* 删除一个对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
*/publicbooleanremoveObject(String bucketName,String objectName)throwsIOException,InvalidKeyException,InvalidResponseException,InsufficientDataException,NoSuchAlgorithmException,ServerException,InternalException,XmlParserException,InvalidBucketNameException,ErrorResponseException{boolean flag =bucketExists(bucketName);if(flag){
minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());returntrue;}returnfalse;}/**
* 删除指定桶的多个文件对象,返回删除错误的对象列表,全部删除成功,返回空列表
*
* @param bucketName 存储桶名称
* @param objectNames 含有要删除的多个object名称的迭代器对象
* @return
* eg:
* List<DeleteObject> objects = new LinkedList<>();
* objects.add(new DeleteObject("my-objectname1"));
* objects.add(new DeleteObject("my-objectname2"));
* objects.add(new DeleteObject("my-objectname3"));
*/publicList<String>removeObjects(String bucketName,List<DeleteObject> objectNames)throwsIOException,InvalidKeyException,InvalidResponseException,InsufficientDataException,NoSuchAlgorithmException,ServerException,InternalException,XmlParserException,InvalidBucketNameException,ErrorResponseException{List<String> deleteErrorNames =newArrayList<>();boolean flag =bucketExists(bucketName);if(flag){Iterable<Result<DeleteError>> results = minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(objectNames).build());for(Result<DeleteError> result : results){DeleteError error = result.get();
deleteErrorNames.add(error.objectName());}}return deleteErrorNames;}/**
* 生成一个给HTTP GET请求用的presigned URL。
* 浏览器/移动端的客户端可以用这个URL进行下载,即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间,默认值是7天。
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param expires 失效时间(以秒为单位),默认是7天,不得大于七天
* @return
*/publicStringgetPresignedObjectUrl(String bucketName,String objectName,Integer expires)throwsInvalidExpiresRangeException,IOException,InvalidResponseException,InvalidKeyException,NoSuchAlgorithmException,ServerException,ErrorResponseException,XmlParserException,InvalidBucketNameException,InsufficientDataException,InternalException{boolean flag =bucketExists(bucketName);String url ="";if(flag){if(expires <1|| expires > DEFAULT_EXPIRY_TIME){thrownewInvalidExpiresRangeException(expires,"expires must be in range of 1 to "+ DEFAULT_EXPIRY_TIME);}try{
url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().method(Method.GET).bucket(bucketName).object(objectName).expiry(expires)//动态参数// .expiry(24 * 60 * 60)//用秒来计算一天时间有效期// .expiry(1, TimeUnit.DAYS)//按天传参// .expiry(1, TimeUnit.HOURS)//按小时传参数.build());}catch(ErrorResponseException|InsufficientDataException|InternalException|InvalidBucketNameException|InvalidExpiresRangeException|InvalidKeyException|InvalidResponseException|IOException|NoSuchAlgorithmException|ServerException|XmlParserException e){
e.printStackTrace();}}return url;}/**
* 生成一个给HTTP PUT请求用的presigned URL。
* 浏览器/移动端的客户端可以用这个URL进行上传,即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间,默认值是7天。
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param expires 失效时间(以秒为单位),默认是7天,不得大于七天
* @return String
*/publicStringpresignedPutObject(String bucketName,String objectName,Integer expires)throwsIOException,InvalidResponseException,InvalidKeyException,NoSuchAlgorithmException,ServerException,ErrorResponseException,XmlParserException,InvalidBucketNameException,InsufficientDataException,InternalException{boolean flag =bucketExists(bucketName);String url ="";if(flag){if(expires <1|| expires > DEFAULT_EXPIRY_TIME){try{thrownewInvalidExpiresRangeException(expires,"expires must be in range of 1 to "+ DEFAULT_EXPIRY_TIME);}catch(InvalidExpiresRangeException e){
e.printStackTrace();}}try{
url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().method(Method.PUT).bucket(bucketName).object(objectName).expiry(expires)//动态参数// .expiry(24 * 60 * 60)//用秒来计算一天时间有效期// .expiry(1, TimeUnit.DAYS)//按天传参// .expiry(1, TimeUnit.HOURS)//按小时传参数.build());}catch(ErrorResponseException|InsufficientDataException e){
e.printStackTrace();}catch(InternalException e){
log.error("InternalException",e);}catch(InvalidBucketNameException e){
log.error("InvalidBucketNameException",e);}catch(InvalidExpiresRangeException e){
log.error("InvalidExpiresRangeException",e);}catch(InvalidKeyException e){
log.error("InvalidKeyException",e);}catch(InvalidResponseException e){
log.error("InvalidResponseException",e);}catch(IOException e){
log.error("IOException",e);}catch(NoSuchAlgorithmException e){
log.error("NoSuchAlgorithmException",e);}catch(ServerException e){
log.error("ServerException",e);}catch(XmlParserException e){
log.error("XmlParserException",e);}}return url;}/**
* 获取对象的元数据
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return
*/publicObjectStatstatObject(String bucketName,String objectName)throwsIOException,InvalidResponseException,InvalidKeyException,NoSuchAlgorithmException,ServerException,ErrorResponseException,XmlParserException,InvalidBucketNameException,InsufficientDataException,InternalException{boolean flag =bucketExists(bucketName);if(flag){ObjectStat statObject =null;try{
statObject = minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());}catch(ErrorResponseException e){
log.error("ErrorResponseException",e);}catch(InsufficientDataException e){
log.error("ErrorResponseException",e);
e.printStackTrace();}catch(InternalException e){
log.error("InternalException",e);}catch(InvalidBucketNameException e){
log.error("InvalidBucketNameException",e);}catch(InvalidKeyException e){
log.error("InvalidKeyException",e);}catch(InvalidResponseException e){
log.error("InvalidResponseException",e);}catch(IOException e){
log.error("IOException",e);}catch(NoSuchAlgorithmException e){
log.error("NoSuchAlgorithmException",e);}catch(ServerException e){
log.error("ServerException",e);}catch(XmlParserException e){
log.error("XmlParserException",e);}return statObject;}returnnull;}/**
* 文件访问路径
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return String
*/publicStringgetObjectUrl(String bucketName,String objectName)throwsIOException,InvalidResponseException,InvalidKeyException,NoSuchAlgorithmException,ServerException,ErrorResponseException,XmlParserException,InvalidBucketNameException,InsufficientDataException,InternalException{boolean flag =bucketExists(bucketName);String url ="";if(flag){try{
url = minioClient.getObjectUrl(bucketName, objectName);}catch(ErrorResponseException e){
log.error("XmlParserException",e);}catch(InsufficientDataException e){
log.error("InsufficientDataException",e);}catch(InternalException e){
log.error("InternalException",e);}catch(InvalidBucketNameException e){
log.error("InvalidBucketNameException",e);}catch(InvalidKeyException e){
log.error("InvalidKeyException",e);}catch(InvalidResponseException e){
log.error("InvalidResponseException",e);}catch(IOException e){
log.error("IOException",e);}catch(NoSuchAlgorithmException e){
log.error("NoSuchAlgorithmException",e);}catch(ServerException e){
log.error("ServerException",e);}catch(XmlParserException e){
log.error("XmlParserException",e);}}return url;}publicvoiddownloadFile(String bucketName,String fileName,String originalName,HttpServletResponse response){try{InputStream file = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());String filename =newString(fileName.getBytes("ISO8859-1"),StandardCharsets.UTF_8);if(StringUtils.isNotEmpty(originalName)){
fileName = originalName;}
response.setHeader("Content-Disposition","attachment;filename="+ filename);ServletOutputStream servletOutputStream = response.getOutputStream();int len;byte[] buffer =newbyte[1024];while((len = file.read(buffer))>0){
servletOutputStream.write(buffer,0, len);}
servletOutputStream.flush();
file.close();
servletOutputStream.close();}catch(ErrorResponseException e){
log.error("ErrorResponseException",e);}catch(Exception e){
log.error("Exception",e);}}}
- 创建一个数据表,用于保存上传到minio的文件的信息
CREATETABLE`minio_file`(`id`bigint(20)NOTNULLCOMMENT'文件id',`original_file_name`varchar(255)COLLATE utf8mb4_bin DEFAULTNULLCOMMENT'原始文件名称',`file_ext_name`varchar(15)COLLATE utf8mb4_bin DEFAULTNULLCOMMENT'文件拓展名',`file_size`bigint(20)DEFAULTNULLCOMMENT'文件大小(单位:字节)',`file_name`varchar(35)COLLATE utf8mb4_bin DEFAULTNULLCOMMENT'存入minio时的文件名称',`mime`varchar(50)COLLATE utf8mb4_bin DEFAULTNULLCOMMENT'文件的content-type',`file_url`varchar(255)COLLATE utf8mb4_bin DEFAULTNULLCOMMENT'文件路径',`is_delete`tinyint(1)DEFAULTNULLCOMMENT'是否删除 0 否 1 是',`create_by`varchar(25)COLLATE utf8mb4_bin DEFAULTNULL,`create_time`datetimeDEFAULTNULL,`update_by`varchar(25)COLLATE utf8mb4_bin DEFAULTNULL,`update_time`datetimeDEFAULTNULL,PRIMARYKEY(`id`))ENGINE=InnoDBDEFAULTCHARSET=utf8mb4 COLLATE=utf8mb4_bin;
- 创建minio上传接口
packagecn.cvzhanshi.wechatpush.controller;importcn.cvzhanshi.wechatpush.config.MinioConfig;importcn.cvzhanshi.wechatpush.utils.MinioClientUtils;importcn.hutool.core.io.FileUtil;importio.swagger.annotations.Api;importio.swagger.annotations.ApiImplicitParam;importio.swagger.annotations.ApiImplicitParams;importio.swagger.annotations.ApiOperation;importlombok.AllArgsConstructor;importlombok.extern.slf4j.Slf4j;importorg.apache.commons.lang3.RandomStringUtils;importorg.apache.commons.lang3.math.NumberUtils;importorg.springframework.stereotype.Controller;importorg.springframework.util.CollectionUtils;importorg.springframework.web.bind.annotation.*;importorg.springframework.web.multipart.MultipartFile;importjava.io.FileInputStream;importjava.time.Instant;importjava.util.ArrayList;importjava.util.List;@RestController@RequestMapping("/fileHandle")@Slf4j@AllArgsConstructor@Api(tags ="文件处理模块")publicclassFileHandleController{privateMinioClientUtils minioClientUtils;privateMinioConfig minioConfig;@ApiOperation(value ="上传文件,支持批量上传")@ApiImplicitParams({@ApiImplicitParam(name ="files", value ="文件流对象,接收数组格式", paramType ="query",required =true, dataType ="MultipartFile", allowMultiple =true)})@PostMapping("/uploadFile")publicApiResultuploadFile(@RequestParam(value ="files", required =true)MultipartFile[] files){ log.info(files.toString());/* if (CollectionUtils.isEmpty(files)){ return ApiResult.error("未选择文件!"); }*/List<String>MinioResponseDTOList=newArrayList<>();for(MultipartFile file : files){String originalFilename = file.getOriginalFilename();// 获取文件拓展名String extName =FileUtil.extName(originalFilename); log.info("文件拓展名:"+ extName);// 生成新的文件名,存入到miniolong millSeconds =Instant.now().toEpochMilli();String minioFileName = millSeconds +RandomStringUtils.randomNumeric(12)+"."+ extName;String contentType = file.getContentType(); log.info("文件mime:{}", contentType);// 返回文件大小,单位字节long size = file.getSize(); log.info("文件大小:"+ size);try{String bucketName = minioConfig.getBucketName(); minioClientUtils.putObject(bucketName, file, minioFileName);String fileUrl = minioClientUtils.getObjectUrl(bucketName, minioFileName);/* MinioFile minioFile = new MinioFile(); minioFile.setOriginalFileName(originalFilename); minioFile.setFileExtName(extName); minioFile.setFileName(minioFileName); minioFile.setFileSize(size); minioFile.setMime(contentType); minioFile.setIsDelete(NumberUtils.INTEGER_ZERO); minioFile.setFileUrl(fileUrl); boolean insert = minioFile.insert(); if (insert) { MinioResponseDTO minioResponseDTO = new MinioResponseDTO(); minioResponseDTO.setFileId(minioFile.getId()); minioResponseDTO.setOriginalFileName(originalFilename); minioResponseDTO.setFileUrl(fileUrl); MinioResponseDTOList.add(minioResponseDTO); }*/MinioResponseDTOList.add(fileUrl);}catch(Exception e){ log.error("上传文件出错:{}", e);returnApiResult.error("上传文件出错");}}returnApiResult.success(MinioResponseDTOList);}/** * 仅仅用于测试,是否可以正常上传文件 * * @return * @throws Exception */@GetMapping("/test")@ApiOperation(value ="测试minio文件上传")publicApiResulttestPutObject()throwsException{FileInputStream fileInputStream =newFileInputStream("C:\\Users\\MSI\\Desktop\\新建文本文档.txt");boolean bs = minioClientUtils.putObject("fsp-dev","新建文本文档.txt", fileInputStream,"image/jpg"); log.info("上传成功?"+ bs);returnApiResult.success("上传成功");}}
- 测试接口
四、Java中图片压缩上传
4.1 背景
现在大家都是用的智能手机拍照,拍出来的照片小则 2-3 M,大则十几 M,所以导致图片显示较慢。思考再三,决定将图片进行压缩再上传图片服务器来解决图片显示慢的问题
4.2 开发准备
- 引入 maven 依赖
<!-- 图片压缩 --><dependency><groupId>net.coobird</groupId><artifactId>thumbnailator</artifactId><version>0.4.8</version></dependency>
本次我们选择了使用 thumbnailator 来作为压缩的工具 - thumbnailator 简介> Thumbnailator 是一个用来生成图像缩略图的 Java 类库,通过很简单的代码即可生成图片缩略图,也可直接对一整个目录的图片生成缩略图> 支持图片缩放,区域裁剪,水印,旋转,保持比例
- 压缩准备判断是否是图片方法
/** * 判断文件是否为图片 */publicbooleanisPicture(String imgName){boolean flag =false;if(StringUtils.isBlank(imgName)){returnfalse;}String[] arr ={"bmp","dib","gif","jfif","jpe","jpeg","jpg","png","tif","tiff","ico"};for(String item : arr){if(item.equals(imgName)){ flag =true;break;}}return flag;}
4.3 压缩上传
/**
* 上传文件
*
* @param file 文件
* @return
*/publicJSONObjectuploadFile(MultipartFile file)throwsException{JSONObject res =newJSONObject();
res.put("code",500);// 判断上传文件是否为空if(null== file ||0== file.getSize()){
res.put("msg","上传文件不能为空");return res;}// 判断存储桶是否存在if(!client.bucketExists("test")){
client.makeBucket("test");}// 拿到文件后缀名,例如:pngString suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);// UUID 作为文件名String uuid =String.valueOf(UUID.randomUUID());// 新的文件名String fileName =DateUtils.getYyyymmdd()+"/"+ uuid +"."+ suffix;/**
* 判断是否是图片
* 判断是否超过了 100K
*/if(isPicture(suffix)&&(1024*1024*0.1)<= file.getSize()){// 在项目根目录下的 upload 目录中生成临时文件File newFile =newFile(ClassUtils.getDefaultClassLoader().getResource("upload").getPath()+ uuid +"."+ suffix);// 小于 1M 的if((1024*1024*0.1)<= file.getSize()&& file.getSize()<=(1024*1024)){Thumbnails.of(file.getInputStream()).scale(1f).outputQuality(0.3f).toFile(newFile);}// 1 - 2M 的elseif((1024*1024)< file.getSize()&& file.getSize()<=(1024*1024*2)){Thumbnails.of(file.getInputStream()).scale(1f).outputQuality(0.2f).toFile(newFile);}// 2M 以上的elseif((1024*1024*2)< file.getSize()){Thumbnails.of(file.getInputStream()).scale(1f).outputQuality(0.1f).toFile(newFile);}// 获取输入流FileInputStream input =newFileInputStream(newFile);// 转为 MultipartFileMultipartFile multipartFile =newMockMultipartFile("file", newFile.getName(),"text/plain", input);// 开始上传
client.putObject("test", fileName, multipartFile.getInputStream(), file.getContentType());// 删除临时文件
newFile.delete();// 返回状态以及图片路径
res.put("code",200);
res.put("msg","上传成功");
res.put("url", minioProp.getEndpoint()+"/"+"test"+"/"+ fileName);}// 不需要压缩,直接上传else{// 开始上传
client.putObject("test", fileName, file.getInputStream(), file.getContentType());// 返回状态以及图片路径
res.put("code",200);
res.put("msg","上传成功");
res.put("url", minioProp.getEndpoint()+"/"+"test"+"/"+ fileName);}return res;}
- 这里我们判断了当文件为图片的时候,且当它大小超过了 (1024 * 1024 * 0.1),约为 100K 的时候,才进行压缩
- 我们首先在根目录下的 upload 目录中创建了一个临时文件 newFile
- Thumbnails.of(file.getInputStream()).scale(1f).outputQuality(0.3f).toFile(newFile);将压缩后的文件输出到临时文件中
- 然后将 FileInputStream 转为 MultipartFile 上传
- 最后删除临时文件 newFile.delete();
- 完成图片压缩上传
遇到的问题:
Thumbnails.scale效果会导致图片大小变大
Thumbnails应该是存在bug,但是也一直没有更新版本,所以根据多次测试得来的结果:用jpg转成jpg效果最佳。所以当图片为png时,先改成jpg格式,再进行压缩。
publicstaticStringimgConvert(String tempDirPath,String fileName,String fileExt)throwsIOException{String srcPath = tempDirPath + fileName;//原始图片路径if("png".equals(fileExt)){//生成新图片名称SimpleDateFormat df =newSimpleDateFormat("yyyyMMddHHmmss");String fileString = df.format(newDate())+"_"+newRandom().nextInt(1000)+".jpg";//新图片全路径String newJpg = tempDirPath + fileString;// 1、先转换成jpg Thumbnails.of(srcPath).scale(1f).toFile(newJpg);//2.jpg图片压缩Thumbnails.of(newJpg).scale(1f).outputQuality(0.25d).toFile(newJpg);//压缩成功后,删除png图片File f =newFile(srcPath);
f.delete();return fileString;}else{Thumbnails.of(srcPath).scale(1f).outputQuality(0.25d).toFile(srcPath);}returnnull;}
五、MinIO集群搭建(完善中~)
版权归原作者 哒不死的小小强 所有, 如有侵权,请联系我们删除。