手把手教你SpringBoot轻松整合Minio
前言
使用SpringBoot可以非常方便、快速搭建项目,使我们不用关心框架之间的兼容性,适用版本等各种问题,我们想使用任何东西,仅仅添加一个配置就可以。
提示:以下是本篇文章正文内容,下面案例可供参考
一、技术介绍
1.Minio是什么?
MinIO是一个基于ApacheLicensev2.0开源协议的对象存储服务。它兼容亚马逊S3云存储服务接口,非常适合于存储大容量非结构化的数据,例如图片、视频、日志文件、备份数据和容器/虚拟机镜像等,而一个对象文件可以是任意大小,从几kb到最大5T不等。
二、使用步骤
1.引入maven库
代码如下(示例):
org.springframework.boot spring-boot-starter-parent 2.4.1 io.minio minio 8.0.3
2.封装Minio
IFile类:
packagecom.hyh.minio; /** *File接口类 * *@Author:heyuhua *@Date:2021/1/1210:33 */ publicinterfaceIFile{ /** *上传 * *@paramfilename文件名 */ voidupload(Stringfilename); /** *上传 * *@paramfilename文件名 *@paramobject保存对象文件名称 */ voidupload(Stringfilename,Stringobject); }
File接口类:
packagecom.hyh.minio.service; importcom.hyh.minio.IFile; /** *File接口 * *@Author:heyuhua *@Date:2021/1/1210:53 */ publicinterfaceFileServiceextendsIFile{ /** *上传 * *@paramfilename文件名称 *@paramobject保存对象文件名称 *@parambucket存储桶 */ voidupload(Stringfilename,Stringobject,Stringbucket); }
File接口实现类:
packagecom.hyh.minio.service.impl; importcom.hyh.minio.service.FileService; importcom.hyh.utils.common.StringUtils; importio.minio.BucketExistsArgs; importio.minio.MakeBucketArgs; importio.minio.MinioClient; importio.minio.UploadObjectArgs; importio.minio.errors.MinioException; importorg.slf4j.Logger; importorg.slf4j.LoggerFactory; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.beans.factory.annotation.Value; importorg.springframework.stereotype.Service; importjava.io.IOException; importjava.security.InvalidKeyException; importjava.security.NoSuchAlgorithmException; /** *文件接口服务实现 * *@Author:heyuhua *@Date:2021/1/1210:53 */ @Service publicclassFileServiceImplimplementsFileService{ /** *日志 */ privatestaticfinalLoggerLOG=LoggerFactory.getLogger(FileServiceImpl.class); /** *minio客户端 */ @Autowired privateMinioClientminioClient; /** *默认存储桶名称 */ @Value("${minio.bucketName:}") privateStringdefaultBucketName; @Override publicvoidupload(Stringfilename){ uploadObject(filename,null,defaultBucketName); } @Override publicvoidupload(Stringfilename,Stringobject){ uploadObject(filename,object,defaultBucketName); } @Override publicvoidupload(Stringfilename,Stringobject,Stringbucket){ uploadObject(filename,object,bucket); } /** *上传 * *@paramfilename *@paramobject *@parambucket */ privatevoiduploadObject(Stringfilename,Stringobject,Stringbucket){ if(StringUtils.isAnyBlank(filename,bucket)) return; try{ //存储桶构建 bucketBuild(bucket); //保存的文件名称 object=StringUtils.isBlank(object)?filename.substring(filename.lastIndexOf("/")>0?filename.lastIndexOf("/"):filename.lastIndexOf("\\")):object; minioClient.uploadObject( UploadObjectArgs.builder() .bucket(bucket) .object(object) .filename(filename) .build()); }catch(MinioException|InvalidKeyException|IOException|NoSuchAlgorithmExceptionexception){ LOG.error("uploadObjecterror",exception); } } /** *存储桶构建 * *@parambucketName */ privatevoidbucketBuild(StringbucketName){ try{ booleanfound= minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); if(!found){ minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); LOG.info("Bucket"+bucketName+"makesuccess."); }else{ LOG.info("Bucket"+bucketName+"alreadyexists."); } }catch(MinioException|InvalidKeyException|IOException|NoSuchAlgorithmExceptionexception){ LOG.error("bucketBuilderror",exception); } } publicStringgetDefaultBucketName(){ returndefaultBucketName; } publicvoidsetDefaultBucketName(StringdefaultBucketName){ this.defaultBucketName=defaultBucketName; } }
Minio配置类:
packagecom.hyh.minio.config; importio.minio.MinioClient; importorg.springframework.boot.context.properties.ConfigurationProperties; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; /** *@Author:heyuhua *@Date:2021/1/1210:42 */ @Configuration @ConfigurationProperties(prefix="minio") publicclassMinioConfig{ /** *endPoint是一个URL,域名,IPv4或者IPv6地址 */ privateStringendpoint; /** *端口 */ privateintport; /** *accessKey类似于用户ID,用于唯一标识你的账户 */ privateStringaccessKey; /** *secretKey是你账户的密码 */ privateStringsecretKey; /** *如果是true,则用的是https而不是http,默认值是true */ privateBooleansecure; /** *默认存储桶 */ privateStringbucketName; /** *配置目录 */ privateStringconfigDir; @Bean publicMinioClientgetMinClient(){ returnMinioClient.builder() .endpoint(endpoint,port,secure)//http .credentials(accessKey,secretKey) .build(); } publicStringgetEndpoint(){ returnendpoint; } publicvoidsetEndpoint(Stringendpoint){ this.endpoint=endpoint; } publicStringgetAccessKey(){ returnaccessKey; } publicvoidsetAccessKey(StringaccessKey){ this.accessKey=accessKey; } publicStringgetSecretKey(){ returnsecretKey; } publicvoidsetSecretKey(StringsecretKey){ this.secretKey=secretKey; } publicBooleangetSecure(){ returnsecure; } publicvoidsetSecure(Booleansecure){ this.secure=secure; } publicStringgetBucketName(){ returnbucketName; } publicvoidsetBucketName(StringbucketName){ this.bucketName=bucketName; } publicStringgetConfigDir(){ returnconfigDir; } publicvoidsetConfigDir(StringconfigDir){ this.configDir=configDir; } publicintgetPort(){ returnport; } publicvoidsetPort(intport){ this.port=port; } }
Minio助手类封装:
packagecom.hyh.minio.helper; importcom.hyh.minio.service.FileService; importorg.slf4j.Logger; importorg.slf4j.LoggerFactory; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.stereotype.Component; importorg.springframework.util.Assert; /** *Minio助手 * *@Author:heyuhua *@Date:2021/1/1210:54 */ @Component publicclassMinioHelper{ /** *日志 */ privatestaticfinalLoggerLOG=LoggerFactory.getLogger(MinioHelper.class); /** *文件接口服务 */ @Autowired privateFileServicefileService; /** *上传 * *@paramfilename */ publicvoidupload(Stringfilename){ Assert.notNull(filename,"filenameisnull."); fileService.upload(filename); } /** *上传 * *@paramfilename *@paramobject */ publicvoidupload(Stringfilename,Stringobject){ Assert.notNull(filename,"filenameisnull."); Assert.notNull(object,"objectisnull."); fileService.upload(filename,object); } /** *上传 * *@paramfilename *@paramobject *@parambucket */ publicvoidupload(Stringfilename,Stringobject,Stringbucket){ Assert.notNull(filename,"filenameisnull."); Assert.notNull(object,"objectisnull."); Assert.notNull(bucket,"bucketisnull."); fileService.upload(filename,object,bucket); } }
3.配置文件
代码如下(示例):
server: port:8088 #minio配置 minio: endpoint:39.108.49.252 port:9000 accessKey:admin secretKey:admin123 secure:false bucketName:"hyh-bucket" configDir:"/home/data/"
4.单元测试
测试代码如下(示例):
packagecom.hyh.core.test; importcom.hyh.core.test.base.HyhTest; importcom.hyh.minio.helper.MinioHelper; importorg.junit.Test; importorg.springframework.beans.factory.annotation.Autowired; /** *MinioTest * *@Author:heyuhua *@Date:2021/1/1211:54 */ publicclassMinioTestextendsHyhTest{ @Autowired privateMinioHelperminioHelper; @Test publicvoidtestUpload(){ //直接指向你本地的路径测试 Stringfilename="E:\\home\\static\\img\\fsPic\\0c34de99ac6b4c56812e83c4eab13a6f.jpg"; Stringobject="hyh-test-name.jpg"; Stringbucket="hyh-test-bucket"; minioHelper.upload(filename); minioHelper.upload(filename,object); minioHelper.upload(filename,object,bucket); //上传完后打开浏览器访问http://ip:9000登录控制台可查看上传的文件 } @Test @Override publicvoidtest(){ System.out.println("---minio测试---"); } }
总结
是不是感觉很简单?关注我带你揭秘更多Minio高级用法源码地址:点此查看源码.
以上就是手把手教你SpringBoot轻松整合Minio的详细内容,更多关于SpringBoot整合Minio的资料请关注毛票票其它相关文章!