iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >minio安装配置教程及整合springboot(史上最强保姆级教程---minio入门)
  • 442
分享到

minio安装配置教程及整合springboot(史上最强保姆级教程---minio入门)

springbootspringjava 2023-08-22 21:08:44 442人浏览 独家记忆
摘要

miNIO安装配置教程及整合SpringBoot 1、进入minio官网2、选择放置minio文件路径3、根据命令行提示访问minio面板4、minio配置(yaml文件版)5、编写minio的配置文件MinIoClientConf

1、进入minio官网

https://www.minio.org.cn/
点击下载(Download),选择自己需要的版本即可

2、选择放置minio文件路径

在此路径下,
新建文件夹minioData(与minio.exe处于同一路径)
在minio.exe文件夹的路径处输入cmd进入命令行界面(该exe文件不能双击运行)
输入命令:minio.exe server minioData全路径
例如:minio.exe server E:\software\minioData
后面路径为创建的minioData文件夹的路径
(如果上面命令执行的时候,命令框在疯狂跳动,就关闭该命令框,以管理员方式开启cmd窗口,进入自己的minio文件夹,执行上面的命令即可)

3、根据命令行提示访问minio面板

有多个url可以访问,但是其中固定的是127.0.0.1:9000
第一次访问,会进入登录页面,登录账户和密码均为minioadmin,
进入点击Create Bucket,bucket Name 建议设定为项目的名字,再点击Create Bucket即可,注意如果需要后期通过访问url预览,需要在新建的bucket Name点击Manage,点击AccessPolicy后面的icon(图标),将访问权限改为Public即可,在项目运行中必须开启minio服务,否则无法使用minion。

4、minio配置(yaml文件版)

spring:  # 配置文件上传大小限制(minio文件上传)  servlet:    multipart:      max-file-size: 200MB      max-request-size: 200MBminio:  endpoint: Http://127.0.0.1:9000  accessKey:  minioadmin  secreTKEy:  minioadmin  bucketName: commUnity-WEB

如果前面有spring配置,只需要将spring的配置minio文件限制复制贴贴到spring下即可。

5、编写minio的配置文件MinioclientConfig

package com.example.config;import io.minio.MinioClient;import lombok.Data;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.stereotype.Component;@Data@Componentpublic class MinIoClientConfig {    @Value("${minio.endpoint}")    private String endpoint;    @Value("${minio.accessKey}")    private String accessKey;    @Value("${minio.secretKey}")    private String secretKey;        @Bean    public MinioClient minioClient() {        return MinioClient.builder()                .endpoint(endpoint)                .credentials(accessKey, secretKey)                .build();    }}

6、新建minio工具类需要的实体类ObjectItem

package com.example.sys.entity;import lombok.Data;@Datapublic class ObjectItem {    private String objectName;    private Long size;}

7、编写minio的工具类MinioUtils

package com.example.utils;import com.example.sys.entity.ObjectItem;import io.minio.*;import io.minio.messages.DeleteError;import io.minio.messages.DeleteObject;import io.minio.messages.Item;import org.apache.Tomcat.util.http.fileupload.IOUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.http.HttpHeaders;import org.springframework.http.HttpStatus;import org.springframework.http.MediaType;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Component;import org.springframework.web.multipart.MultipartFile;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;@Componentpublic class MinioUtils {    @Autowired    private MinioClient minioClient;    @Value("${minio.bucketName}")    private String bucketName;        public void existBucket(String name) {        try {            boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(name).build());            if (!exists) {                minioClient.makeBucket(MakeBucketArgs.builder().bucket(name).build());            }        } catch (Exception e) {            e.printStackTrace();        }    }        public Boolean makeBucket(String bucketName) {        try {            minioClient.makeBucket(MakeBucketArgs.builder()                    .bucket(bucketName)                    .build());        } catch (Exception e) {            e.printStackTrace();            return false;        }        return true;    }        public Boolean removeBucket(String bucketName) {        try {            minioClient.removeBucket(RemoveBucketArgs.builder()                    .bucket(bucketName)                    .build());        } catch (Exception e) {            e.printStackTrace();            return false;        }        return true;    }        public List upload(MultipartFile[] multipartFile) {        List names = new ArrayList<>(multipartFile.length);        for (MultipartFile file : multipartFile) {            String fileName = file.getOriginalFilename();            String[] split = fileName.split("\\.");            if (split.length > 1) {                fileName = split[0] + "_" + System.currentTimeMillis() + "." + split[1];            } else {                fileName = fileName + System.currentTimeMillis();            }            InputStream in = null;            try {                in = file.getInputStream();                minioClient.putObject(PutObjectArgs.builder()                        .bucket(bucketName)                        .object(fileName)                        .stream(in, in.available(), -1)                        .contentType(file.getContentType())                        .build()                );            } catch (Exception e) {                e.printStackTrace();            } finally {                if (in != null) {                    try {                        in.close();                    } catch (IOException e) {                        e.printStackTrace();                    }                }            }            names.add(fileName);        }        return names;    }        public ResponseEntity download(String fileName) {        ResponseEntity responseEntity = null;        InputStream in = null;        ByteArrayOutputStream out = null;        try {            in = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());            out = new ByteArrayOutputStream();            IOUtils.copy(in, out);            //封装返回值            byte[] bytes = out.toByteArray();            HttpHeaders headers = new HttpHeaders();            try {                headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));            } catch (UnsupportedEncodingException e) {                e.printStackTrace();            }            headers.setContentLength(bytes.length);            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);            headers.setAccessControlExposeHeaders(Arrays.asList("*"));            responseEntity = new ResponseEntity(bytes, headers, HttpStatus.OK);        } catch (Exception e) {            e.printStackTrace();        } finally {            try {                if (in != null) {                    try {                        in.close();                    } catch (IOException e) {                        e.printStackTrace();                    }                }                if (out != null) {                    out.close();                }            } catch (IOException e) {                e.printStackTrace();            }        }        return responseEntity;    }        public List listObjects(String bucketName) {        Iterable> results = minioClient.listObjects(                ListObjectsArgs.builder().bucket(bucketName).build());        List objectItems = new ArrayList<>();        try {            for (Result result : results) {                Item item = result.get();                ObjectItem objectItem = new ObjectItem();                objectItem.setObjectName(item.objectName());                objectItem.setSize(item.size());                objectItems.add(objectItem);            }        } catch (Exception e) {            e.printStackTrace();            return null;        }        return objectItems;    }        public Iterable> removeObjects(String bucketName, List objects) {        List dos = objects.stream().map(e -> new DeleteObject(e)).collect(Collectors.toList());        Iterable> results = minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(dos).build());        return results;    }}

8、编写minio的Controller层MinioController

package com.example.sys.controller;import com.example.utils.MinioUtils;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.multipart.MultipartFile;import java.util.List;@RestController@Slf4jpublic class MinioController {    @Autowired    private MinioUtils minioUtils;    @Value("${minio.endpoint}")    private String address;    @Value("${minio.bucketName}")    private String bucketName;    @PostMapping("/upload")    public Object upload(MultipartFile file) {        List upload = minioUtils.upload(new MultipartFile[]{file});        return address + "/" + bucketName + "/" + upload.get(0);    }}

9.通过接口测试工具测试

1)新建一个请求(Request)
2)请求方式修改为post
3)请求的url,项目访问路径/upload
例如:http://127.0.0.1:8080/upload
4)选择Body
5)选择FORM-Data
6)填写key和value
第一个键值
key—>file
参数类型为file
value—>点击Select Files按钮,选择文件(建议图片)
第二个键值
key—>bucketName
参数类型:选择String
value—>个人设置的桶(bucketName)的名称
最后点击send,发送即可
通过响应的url,复制到浏览器,打开即可,一般打开即可预览到图片,有些电脑会直接下载图片。

来源地址:https://blog.csdn.net/qq_51073233/article/details/127673489

--结束END--

本文标题: minio安装配置教程及整合springboot(史上最强保姆级教程---minio入门)

本文链接: https://www.lsjlt.com/news/378077.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作