• minio安装部署及使用


    一、服务器安装minio

    1.进行下载

    下载地址:

    GNU/Linux

    https://dl.min.io/server/minio/release/linux-amd64/minio
    
    • 1

    2.新建minio安装目录,执行如下命令

    mkdir -p /home/minio/data

    把二进制文件上传到安装目录后,执行:

    chmod +x minio    //给予权限
    export MINIO_ACCESS_KEY=minioadmin   //创建账号
    export MINIO_SECRET_KEY=minioadmin   //创建密码
    ./minio server /home/minio/data        //启动
    
    • 1
    • 2
    • 3
    • 4

    后台启动,并打印日志

    nohup ./minio server /home/minio/data > /home/minio/data/minio.log &
    
    • 1

    默认的端口为:9000

    自定义端口方式:自定义启动端口号以及控制台端口号,不设置则控制台会自动配置其他端口号,非常不方便

    nohup ./minio server  --address :9000 --console-address :9001 /home/minio/data > /home/minio/data/minio.log &
    
    • 1

    查看状态

    ps -ef|grep minio
    
    • 1

    二、进行访问,并设置桶

    1.访问

    地址:http://127.0.0.1:9000

    输入账号密码后:

    进行创建桶,名字自取,创建完成后服务器home/minio/data下也会创建这个文件目录

    进行设置:

    必须将规则设置成readwrite ,才可进行读取文件,否则只存或者只能读。

    三、springboot进行实现

    1.引入依赖

    
    
        io.minio
        minio
        3.0.10
    
    
    
        com.alibaba
        fastjson
        1.2.51
    
    
    
        org.springframework.boot
        spring-boot-starter-thymeleaf
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    2.在 application.yml 文件中加入 MinIO 服务器的相关信息

    # minio 文件存储配置信息
    minio:
      endpoint: http://127.0.0.1:9000
      accesskey: minioadmin
      secretKey: minioadmin
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3.创建实体类

    这一步,我们将配置文件中 minio 的配置信息通过注解的方式注入到 MinioProp 这个实体中,方便后面我们使用

    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    /**
     * minio 属性值
     */
    @Data
    @Component
    @ConfigurationProperties(prefix = "minio")
    public class MinioProp {
        /**
         * 连接url
         */
        private String endpoint;
        /**
         * 用户名
         */
        private String accesskey;
        /**
         * 密码
         */
        private String secretKey;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    4、创建核心配置类

    通过注入 MinIO 服务器的相关配置信息,得到 MinioClient 对象,我们上传文件依赖此对象

    import io.minio.MinioClient;
    import io.minio.errors.InvalidEndpointException;
    import io.minio.errors.InvalidPortException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * minio 核心配置类
     */
    @Configuration
    @EnableConfigurationProperties(MinioProp.class)
    public class MinioConfig {
    
        @Autowired
        private MinioProp minioProp;
    
        /**
         * 获取 MinioClient
         *
         * @return
         * @throws InvalidPortException
         * @throws InvalidEndpointException
         */
        @Bean
        public MinioClient minioClient() throws InvalidPortException, InvalidEndpointException {
            return new MinioClient(minioProp.getEndpoint(), minioProp.getAccesskey(), minioProp.getSecretKey());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    5、上传工具类

    import com.alibaba.fastjson.JSONObject;
    import com.zyxx.email.common.redis.RedisUtil;
    import com.zyxx.email.utils.DateUtils;
    import io.minio.MinioClient;
    import lombok.SneakyThrows;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.web.multipart.MultipartFile;
    
    @Slf4j
    @Component
    public class MinioUtils {
    
        @Autowired
        private MinioClient client;
        @Autowired
        private MinioProp minioProp;
    
        /**
         * 创建bucket
         *
         * @param bucketName bucket名称
         */
        @SneakyThrows
        public void createBucket(String bucketName) {
            if (!client.bucketExists(bucketName)) {
                client.makeBucket(bucketName);
            }
        }
    
        /**
         * 上传文件
         *
         * @param file       文件
         * @param bucketName 存储桶
         * @return
         */
        public JSONObject uploadFile(MultipartFile file, String bucketName) throws Exception {
            JSONObject res = new JSONObject();
            res.put("code", 0);
            // 判断上传文件是否为空
            if (null == file || 0 == file.getSize()) {
                res.put("msg", "上传文件不能为空");
                return res;
            }
            try {
    	       	// 判断存储桶是否存在
    	        createBucket(bucketName);
    	        // 文件名
    	        String originalFilename = file.getOriginalFilename();
    	        // 新的文件名 = 存储桶名称_时间戳.后缀名
    	        String fileName = bucketName + "_" + System.currentTimeMillis() + originalFilename.substring(originalFilename.lastIndexOf("."));
    	        // 开始上传
    	        client.putObject(bucketName, fileName, file.getInputStream(), file.getContentType());
    	        res.put("code", 1);
    	        res.put("msg", minioProp.getEndpoint() + "/" + bucketName + "/" + fileName);
    	        return res;
    		}  catch (Exception e) {
                log.error("上传文件失败:{}", e.getMessage());
            }
            res.put("msg", "上传失败");
            return res;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    6.controller接口

    import com.alibaba.fastjson.JSONObject;
    import com.zyxx.email.common.minio.MinioUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.multipart.MultipartFile;
    
    import javax.servlet.http.HttpServletRequest;
    
    @Controller
    public class MinioController {
    
        @Autowired
        private MinioUtils minioUtils;
    
    
        /**
         * 上传
         *
         * @param file
         * @param request
         * @return
         */
        @PostMapping("/upload")
        @ResponseBody
        public String upload(@RequestParam(name = "file", required = false) MultipartFile file, HttpServletRequest request) {
            JSONObject res = null;
            try {
                res = minioUtils.uploadFile(file, "product");
            } catch (Exception e) {
                e.printStackTrace();
                res.put("code", 0);
                res.put("msg", "上传失败");
            }
            return res.toJSONString();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    测试

    通过网址进行访问:

    PostMan进行测试上传;

    删除文件:

        //文件删除
        @DeleteMapping
        public String delete(String name) {
            try {
                MinioClient minioClient = new MinioClient(ENDPOINT, ACCESSKEY, SECRETKEY);
                minioClient.removeObject(BUCKETNAME, name);
            } catch (Exception e) {
                return "删除失败"+e.getMessage();
            }
            return "删除成功";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    【USRP】软件无线电基础篇:无线电频谱和波段划分表
    【Spring Boot 源码学习】BootstrapRegistry 初始化器实现
    .balckhoues-V-XXXXXXX勒索病毒数据怎么处理|数据解密恢复
    183. 从不订购的客户
    WebSocket 入门案例
    计算机视觉—车道线检测
    电脑电源灯一闪一闪开不了机怎么办
    HTML网页设计制作——初音动漫(6页) dreamweaver作业静态HTML网页设计模板
    阿里云短信接入 PHP
    电影售票系统
  • 原文地址:https://blog.csdn.net/m0_67401761/article/details/126034954