解析SpringBoot|解析SpringBoot 搭建基于 MinIO 的高性能存储服务的问题

目录

  • 1.什么是MinIO
  • 2.使用docker搭建minio服务
    • 2.1.GNU/Linux和macOS
    • 2.2.windows
  • 3.下面开始搭建springboot环境

    1.什么是MinIO MinIO是根据GNU Affero通用公共许可证v3.0发布的高性能对象存储。它与Amazon S3云存储服务兼容。使用MinIO构建用于机器学习,分析和应用程序数据工作负载的高性能基础架构。
    官网地址:https://min.io/
    文档地址:https://docs.min.io/

    2.使用docker 搭建minio 服务 【解析SpringBoot|解析SpringBoot 搭建基于 MinIO 的高性能存储服务的问题】
    2.1.GNU / Linux和macOS
    docker run -p 9000:9000 \--name minio1 \-v /mnt/data:/data \-e "MINIO_ROOT_USER=AKIAIOSFODNN7EXAMPLE" \-e "MINIO_ROOT_PASSWORD=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" \minio/minio server /data


    2.2.windows
    docker run -p 9000:9000 \--name minio1 \-v D:\data:/data \-e "MINIO_ROOT_USER=AKIAIOSFODNN7EXAMPLE" \-e "MINIO_ROOT_PASSWORD=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" \minio/minio server /data

    • MINIO_ROOT_USER:为用户key
    • MINIO_ROOT_PASSWORD:为用户密钥
    以上搭建的都是单机版的。想要了解分布式 的方式请查看官网文档。
    解析SpringBoot|解析SpringBoot 搭建基于 MinIO 的高性能存储服务的问题
    文章图片

    这就是在win的docker上运行的。
    当启动后在浏览器访问 http://localhost:9000 就可以访问minio的图形化界面了,如图所示:
    解析SpringBoot|解析SpringBoot 搭建基于 MinIO 的高性能存储服务的问题
    文章图片

    解析SpringBoot|解析SpringBoot 搭建基于 MinIO 的高性能存储服务的问题
    文章图片


    3.下面开始搭建springboot 环境 初始化一个springboot项目大家都会,这里不多做介绍。
    主要是介绍需要引入的依赖:
    org.springframework.bootspring-boot-starter-thymeleafio.miniominio8.2.1org.projectlomboklomboktrue

    依赖可以官方文档里找:https://docs.min.io/docs/java-client-quickstart-guide.html
    下面介绍配置文件:
    spring:servlet:multipart:max-file-size: 10MBmax-request-size: 10MB#minio配置minio:access-key: AKIAIOSFODNN7EXAMPLE#key就是docker初始化是设置的,密钥相同secret-key: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEYurl: http://localhost:9000bucket-name: wdhcrthymeleaf:cache: false

    创建minio的配置类:
    @Configuration@ConfigurationProperties(prefix = "spring.minio")@Datapublic class MinioConfiguration {private String accessKey; private String secretKey; private String url; private String bucketName; @Beanpublic MinioClient minioClient() {return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build(); }}

    使用配置属性绑定进行参数绑定,并初始化一个minio client对象放入容器中。
    下面就是我封装的minio client 操作minio的简单方法的组件。
    @Componentpublic class MinioComp {@Autowiredprivate MinioClient minioClient; @Autowiredprivate MinioConfiguration configuration; /*** @description: 获取上传临时签名* @dateTime: 2021/5/13 14:12*/public Map getPolicy(String fileName, ZonedDateTime time) {PostPolicy postPolicy = new PostPolicy(configuration.getBucketName(), time); postPolicy.addEqualsCondition("key", fileName); try {Map map = minioClient.getPresignedPostFormData(postPolicy); HashMap map1 = new HashMap<>(); map.forEach((k,v)->{map1.put(k.replaceAll("-",""),v); }); map1.put("host",configuration.getUrl()+"/"+configuration.getBucketName()); return map1; } 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 (ServerException e) {e.printStackTrace(); } catch (XmlParserException e) {e.printStackTrace(); }return null; }/*** @description: 获取上传文件的url* @dateTime: 2021/5/13 14:15*/public String getPolicyUrl(String objectName, Method method, int time, TimeUnit timeUnit) {try {return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().method(method).bucket(configuration.getBucketName()).object(objectName).expiry(time, timeUnit).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; }/*** @description: 上传文件* @dateTime: 2021/5/13 14:17*/public void upload(MultipartFile file, String fileName) {// 使用putObject上传一个文件到存储桶中。try {InputStream inputStream = file.getInputStream(); minioClient.putObject(PutObjectArgs.builder().bucket(configuration.getBucketName()).object(fileName).stream(inputStream, file.getSize(), -1).contentType(file.getContentType()).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 (ServerException e) {e.printStackTrace(); } catch (XmlParserException e) {e.printStackTrace(); }}/*** @description: 根据filename获取文件访问地址* @dateTime: 2021/5/17 11:28*/public String getUrl(String objectName, int time, TimeUnit timeUnit) {String url = null; try {url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().method(Method.GET).bucket(configuration.getBucketName()).object(objectName).expiry(time, timeUnit).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 url; }}

    简单说明:
    • 使用MultipartFile接收前端文件流,再上传到minio。
    • 构建一个formData的签名数据,给前端,让前端之前上传到minio。
    • 构建一个可以上传的临时URL给前端,前端通过携带文件请求该URL进行上传。
    • 使用filename请求服务端获取临时访问文件的URL。(最长时间为7 天,想要永久性访问,需要其他设置,这里不做说明。)
    下面展示页面html,使用的是VUE+element-ui进行渲染。
    上传图片传统上传
    将文件拖到此处,或点击上传只能上传jpg/png文件,且不超过500kb解析SpringBoot|解析SpringBoot 搭建基于 MinIO 的高性能存储服务的问题
    文章图片
    前端formData直传
    将文件拖到此处,或点击上传只能上传jpg/png文件,且不超过500kb解析SpringBoot|解析SpringBoot 搭建基于 MinIO 的高性能存储服务的问题
    文章图片
    前端Url直传
    将文件拖到此处,或点击上传只能上传jpg/png文件,且不超过500kb解析SpringBoot|解析SpringBoot 搭建基于 MinIO 的高性能存储服务的问题
    文章图片
    .div-center-class {padding: 28% 0%; text-align: center; background: beige; }

    解析SpringBoot|解析SpringBoot 搭建基于 MinIO 的高性能存储服务的问题
    文章图片

    可以分别体验不同的实现效果。
    以上就是使用springboot搭建基于minio的高性能存储服务的全部步骤了。
    项目地址是:https://gitee.com/jack_whh/minio-upload
    到此这篇关于SpringBoot 搭建基于 MinIO 的高性能存储服务的文章就介绍到这了,更多相关SpringBoot 高性能存储服务内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

      推荐阅读