Docker安装MinIO

一、什么是MinIO?

为什么选择MinIO?

1、MinIO 是在 Apache License v2.0 下发布的对象存储服务器。 它与 Amazon S3 云存储服务兼容。 它最适合存储非结构化数据,如照片,视频,日志文件,备份和容器/ VM 映像。 对象的大小可以从几 KB 到最大 5TB。

2、MinIO 服务器足够轻,可以与应用程序堆栈捆绑在一起,类似于 NodeJS,Redis 和 MySQL

3 一种高性能的分布式对象存储服务器,用于大型数据基础设施。它是机器学习和其他大数据工作负载下Hadoop HDFS 的理想 s3 兼容替代品

MinIO特点

1、Minio 有良好的存储机制
2、Minio 有很好纠删码的算法与擦除编码算法
3、拥有RS code 编码数据恢复原理
4、公司做强做大时,数据的拥有重要性,对数据治理与大数据分析做准备。
5、搭建自己的一套文件系统服务,对文件数据进行安全保护。
6、拥有自己的平台,不限于其他方限制。

什么是纠删码?

Minio使用纠删码技术来保护数据,它是一种恢复丢失和损坏数据的数学算法,它将数据分块冗余的分散存储在各各节点的磁盘上,所有的可用磁盘组成一个集合,上图由8块硬盘组成一个集合,当上传一个文件时会通过纠删码算法计算对文件进行分块存储,除了将文件本身分成4个数据块,还会生成4个校验块,数据块和校验块会分散的存储在这8块硬盘上。使用纠删码的好处是即便丢失一半数量(N/2)的硬盘,仍然可以恢复数据。 比如上边集合中有4个以内的硬盘损害仍可保证数据恢复,不影响上传和下载,如果多于一半的硬盘坏了则无法恢复。

二、Docker安装MinIO

1. 拉取镜像

docker pull minio/minio

2. 查看镜像

 docker images

3. 创建MinIO的挂载目录
【/home/soft/minio/config】配置文件目录
【/home/soft/minio/data】存储文件的目录

mkdir -p /home/soft/minio/config
mkdir -p /home/soft/minio/data

4. 启动MinIO容器

docker run -d -p 9000:9000 -p 9090:9090 --name minio -v /home/soft/minio/data:/data -v /home/soft/minio/config:/root/.minio  -e "MINIO_ACCESS_KEY=minio" -e "MINIO_SECRET_KEY=minio123" minio/minio server /data --console-address ":9090" -address ":9000"
  1. -v /home/soft/minio/data:/data :表示将宿主机的/soft/minio/data目录挂载到容器的/data目录
  2. -e "MINIO_ACCESS_KEY=minio":密钥也是登录的用户名,自己设置
  3. -e "MINIO_SECRET_KEY=minio123" :秘密密钥也是登录的密码,自己设置
  4. --console-address ":9090" :客户端端口为9090
  5. -address ":9000" :服务端口为9000

5. 登陆测试
在这里插入图片描述

三、SpringBoot集成MinIO

1. MinIO前期准备

1. 创建MinIO用户

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2. 创建组

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3. 创建accessKey和secretKey

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4. 创建Bucket

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

2. springBoot整合MinIO

整合内容参考:https://blog.csdn.net/yueyue763184/article/details/131147025

1. 导入maven坐标

<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>7.0.2</version>
</dependency>

2. 配置 application.yml

server:
  port: 8080
spring:
  # 配置文件上传大小限制
  servlet:
    multipart:
      max-file-size: 200MB
      max-request-size: 200MB
minio:
  host: http://yourIp:9000
  url: ${
   minio.host}/${
   minio.bucket}/
  access-key: minioadmin
  secret-key: minioadmin
  bucket: public
 

3. MinioConfig配置类

import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.PutObjectOptions;
import io.minio.Result;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.UriUtils;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
 
 
@Component
public class MinioConfig implements InitializingBean {
   
 
    @Value(value = "${minio.bucket}")
    private String bucket;
 
    @Value(value = "${minio.host}")
    private String host;
 
    @Value(value = "${minio.url}")
    private String url;
 
    @Value(value = "${minio.access-key}")
    private String accessKey;
 
    @Value(value = "${minio.secret-key}")
    private String secretKey;
 
    private MinioClient minioClient;
 
    @Override
    public void afterPropertiesSet() throws Exception {
   
        Assert.hasText(url, "Minio url 为空");
        Assert.hasText(accessKey, "Minio accessKey为空");
        Assert.hasText(secretKey, "Minio secretKey为空");
        this.minioClient = new MinioClient(this.host, this.accessKey, this.secretKey);
    }
 
 
 
    /**
     * 上传
     */
    public String putObject(MultipartFile multipartFile) throws Exception {
   
        // bucket 不存在,创建
        if (!minioClient.bucketExists(this.bucket)) {
   
            minioClient.makeBucket(this.bucket);
        }
        try (InputStream inputStream = multipartFile.getInputStream()) {
   
            // 上传文件的名称
            String fileName = multipartFile.getOriginalFilename();
            // PutObjectOptions,上传配置(文件大小,内存中文件分片大小)
            PutObjectOptions putObjectOptions = new PutObjectOptions(multipartFile.getSize(), PutObjectOptions.MIN_MULTIPART_SIZE);
            // 文件的ContentType
            putObjectOptions.setContentType(multipartFile.getContentType());
            minioClient.putObject(this.bucket, fileName, inputStream, putObjectOptions);
            // 返回访问路径
            return this.url + UriUtils.encode(fileName, StandardCharsets.UTF_8);
        }
    }
 
    /**
     * 文件下载
     */
    public void download(String fileName, HttpServletResponse response){
   
        // 从链接中得到文件名
        InputStream inputStream;
        try {
   
            MinioClient minioClient = new MinioClient(host, accessKey, secretKey);
            ObjectStat stat = minioClient.statObject(bucket, fileName);
            inputStream = minioClient.getObject(bucket, fileName);
            response.setContentType(stat.contentType());
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            IOUtils.copy(inputStream, response.getOutputStream());
            inputStream.close();
        } catch (Exception e){
   
            e.printStackTrace();
            System.out.println("有异常:" + e);
        }
    }
 
    /**
     * 列出所有存储桶名称
     *
     * @return
     * @throws Exception
     */
    public List<String> listBucketNames()
            throws Exception {
   
        List<Bucket> bucketList = listBuckets();
        List<String> bucketListName = new ArrayList<>();
        for (Bucket bucket : bucketList) {
   
            bucketListName.add(bucket.name());
        }
        return bucketListName;
    }
 
    /**
     * 查看所有桶
     *
     * @return
     * @throws Exception
     */
    public List<Bucket> listBuckets()
            throws Exception {
   
        return minioClient.listBuckets();
    }
 
    /**
     * 检查存储桶是否存在
     *
     * @param bucketName
     * @return
     * @throws Exception
     */
    public boolean bucketExists(String bucketName) throws Exception {
   
        boolean flag = minioClient.bucketExists(bucketName);
        if (flag) {
   
            return true;
        }
        return false;
    }
 
    /**
     * 创建存储桶
     *
     * @param bucketName
     * @return
     * @throws Exception
     */
    public boolean makeBucket(String bucketName)
            throws Exception {
   
        boolean flag = bucketExists(bucketName);
        if (!flag) {
   
            minioClient.makeBucket(bucketName);
            return true;
        } else {
   
            return false;
        }
    }
 
    /**
     * 删除桶
     *
     * @param bucketName
     * @return
     * @throws Exception
     */
    public boolean removeBucket(String bucketName)
            throws Exception {
   
        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) {
   
                    return false;
                }
            }
            // 删除存储桶,注意,只有存储桶为空时才能删除成功。
            minioClient.removeBucket(bucketName);
            flag = bucketExists(bucketName);
            if (!flag) {
   
                return true;
            }
 
        }
        return false;
    }
 
    /**
     * 列出存储桶中的所有对象
     *
     * @param bucketName 存储桶名称
     * @return
     * @throws Exception
     */
    public Iterable<Result<Item>> listObjects(String bucketName) throws Exception {
   
        boolean flag = bucketExists(bucketName);
        if (flag) {
   
            return minioClient.listObjects(bucketName);
        }
        return null;
    }
 
    /**
     * 列出存储桶中的所有对象名称
     *
     * @param bucketName 存储桶名称
     * @return
     * @throws Exception
     */
    public List<String> listObjectNames(String bucketName) throws Exception {
   
        List<String> listObjectNames = new ArrayList<>();
        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 存储桶名称
     * @param objectName 存储桶里的对象名称
     * @throws Exception
     */
    public boolean removeObject(String bucketName, String objectName) throws Exception {
   
        boolean flag = bucketExists(bucketName);
        if (flag) {
   
            List<String> objectList = listObjectNames(bucketName);
            for (String s : objectList) {
   
                if(s.equals(objectName)){
   
                    minioClient.removeObject(bucketName, objectName);
                    return true;
                }
            }
        }
        return false;
    }
 
    /**
     * 文件访问路径
     *
     * @param bucketName 存储桶名称
     * @param objectName 存储桶里的对象名称
     * @return
     * @throws Exception
     */
    public String getObjectUrl(String bucketName, String objectName) throws Exception {
   
        boolean flag = bucketExists(bucketName);
        String url = "";
        if (flag) {
   
            url = minioClient.getObjectUrl(bucketName, objectName);
        }
        return url;
    }
 
}
 

4. MinioController控制类

import com.minio.demo.config.MinioConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.util.List;
 
@RestController
@CrossOrigin
@RequestMapping("/minio")
public class MinioController {
   
 
    @Autowired
    MinioConfig minioConfig;
 
    // 上传
    @PostMapping("/upload")
    public Object upload(@RequestParam("file") MultipartFile multipartFile) throws Exception {
   
        return this.minioConfig.putObject(multipartFile);
    }
 
    // 下载文件
    @GetMapping("/download")
    public void download(@RequestParam("fileName")String fileName, HttpServletResponse response) {
   
        this.minioConfig.download(fileName,response);
    }
 
    // 列出所有存储桶名称
    @PostMapping("/list")
    public List<String> list() throws Exception {
   
        return this.minioConfig.listBucketNames();
    }
 
    // 创建存储桶
    @PostMapping("/createBucket")
    public boolean createBucket(String bucketName) throws Exception {
   
        return this.minioConfig.makeBucket(bucketName);
    }
 
    // 删除存储桶
    @PostMapping("/deleteBucket")
    public boolean deleteBucket(String bucketName) throws Exception {
   
        return this.minioConfig.removeBucket(bucketName);
    }
 
    // 列出存储桶中的所有对象名称
    @PostMapping("/listObjectNames")
    public List<String> listObjectNames(String bucketName) throws Exception {
   
        return this.minioConfig.listObjectNames(bucketName);
    }
 
    // 删除一个对象
    @PostMapping("/removeObject")
    public boolean removeObject(String bucketName, String objectName) throws Exception {
   
        return this.minioConfig.removeObject(bucketName, objectName);
    }
 
    // 文件访问路径
    @PostMapping("/getObjectUrl")
    public String getObjectUrl(String bucketName, String objectName) throws Exception {
   
        return this.minioConfig.getObjectUrl(bucketName, objectName);
    }
}
 

5. 测试

在这里插入图片描述

6. 验证

在这里插入图片描述

相关推荐

  1. 07、Docker 安装 MinIO

    2023-12-28 22:20:02       48 阅读
  2. docker安装minio

    2023-12-28 22:20:02       36 阅读
  3. docker compose安装minio

    2023-12-28 22:20:02       27 阅读
  4. docker安装minio

    2023-12-28 22:20:02       12 阅读
  5. docker安装miniominio的使用

    2023-12-28 22:20:02       10 阅读
  6. CentOS7下使用Dockers安装MinIO

    2023-12-28 22:20:02       20 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-28 22:20:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-28 22:20:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-28 22:20:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-28 22:20:02       20 阅读

热门阅读

  1. 脚本批量导入导出es表结构

    2023-12-28 22:20:02       38 阅读
  2. List的四种遍历方法

    2023-12-28 22:20:02       36 阅读
  3. 面向-对象的三大原则

    2023-12-28 22:20:02       38 阅读
  4. vue中使用lodash的debounce防抖函数

    2023-12-28 22:20:02       38 阅读
  5. Qt开发Charts折线图绑定事件

    2023-12-28 22:20:02       42 阅读
  6. Vue前后端跨域链接

    2023-12-28 22:20:02       40 阅读
  7. vue前端学习笔记

    2023-12-28 22:20:02       43 阅读
  8. 数据库(部分函数)

    2023-12-28 22:20:02       35 阅读
  9. LeetCode 2660. 保龄球游戏的获胜者:模拟

    2023-12-28 22:20:02       36 阅读
  10. docker学习笔记

    2023-12-28 22:20:02       39 阅读
  11. centos 编译安装 make

    2023-12-28 22:20:02       38 阅读
  12. storyBook play学习

    2023-12-28 22:20:02       30 阅读