MinIO【部署 01】MinIO安装及SpringBoot集成简单测试

MinIO安装及SpringBoot集成测试

  • 1.下载安装
    • [1.1 Install the MinIO Server](#1.1 Install the MinIO Server)
    • [1.2 Launch the MinIO Server](#1.2 Launch the MinIO Server)
    • [1.3 Connect Your Browser to the MinIO Server](#1.3 Connect Your Browser to the MinIO Server)
  • 2.SpringBoot集成
    • [2.1 依赖及配置](#2.1 依赖及配置)
    • [2.2 代码](#2.2 代码)
    • [2.3 测试结果](#2.3 测试结果)

1.下载安装

下载 https://min.io/download#/linux;
安装文档 https://min.io/docs/minio/linux/index.html。

1.1 Install the MinIO Server

bash 复制代码
# 指导安装方式
wget https://dl.min.io/server/minio/release/linux-arm64/archive/minio-20230804174021.0.0.aarch64.rpm -O minio.rpm
sudo dnf install minio.rpm

# 实际安装
rpm -ivh minio-20230804174021.0.0.x86_64.rpm

1.2 Launch the MinIO Server

bash 复制代码
# 指导启动方式
mkdir ~/minio
minio server ~/minio --console-address :9090

# 实际启动
mkdir -p /home/minio/data
minio server /home/minio/data --console-address tcloud:9090
bash 复制代码
[root@tcloud data]# minio server /home/minio/data --console-address tcloud:9090
WARNING: Detected Linux kernel version older than 4.0.0 release, there are some known potential performance problems with this kernel version. MinIO recommends a minimum of 4.x.x linux kernel version for best performance
Formatting 1st pool, 1 set(s), 1 drives per set.
WARNING: Host local has more than 0 drives of set. A host failure will result in data becoming unavailable.
WARNING: Detected default credentials 'minioadmin:minioadmin', we recommend that you change these values with 'MINIO_ROOT_USER' and 'MINIO_ROOT_PASSWORD' environment variables
MinIO Object Storage Server
Copyright: 2015-2023 MinIO, Inc.
License: GNU AGPLv3 <https://www.gnu.org/licenses/agpl-3.0.html>
Version: RELEASE.2023-08-04T17-40-21Z (go1.19.12 linux/amd64)

Status:         1 Online, 0 Offline.
S3-API: http://172.17.0.8:9000  http://172.18.0.1:9000  http://172.81.0.1:9000  http://172.20.240.0:9000  http://127.0.0.1:9000
RootUser: minioadmin
RootPass: minioadmin

Console: http://tcloud:9090
RootUser: minioadmin
RootPass: minioadmin

Command-line: https://min.io/docs/minio/linux/reference/minio-mc.html#quickstart
   $ mc alias set myminio http://172.17.0.8:9000 minioadmin minioadmin

Documentation: https://min.io/docs/minio/linux/index.html
Warning: The standard parity is set to 0. This can lead to data loss.
bash 复制代码
# 后台启动命令
nohup /usr/local/bin/minio server /home/minio/data --console-address tcloud:9090 > /home/minio/data/minio.log 2>&1 &

# 自定义账号密码后的启动
export MINIO_ACCESS_KEY=minio
export MINIO_SECRET_KEY=miniostorage
nohup /usr/local/bin/minio server  /home/minio/data --console-address tcloud:9090 > /home/minio/data/minio.log 2>&1 &

1.3 Connect Your Browser to the MinIO Server

工作台详细使用文档 https://min.io/docs/minio/linux/administration/minio-console.html#minio-console

登录页面:

登录成功:

2.SpringBoot集成

Java Quickstart Guide https://min.io/docs/minio/linux/developers/java/minio-java.html#minio-java-quickstart

Java SDK https://github.com/minio/minio-java/tree/release

2.1 依赖及配置

xml 复制代码
<!--当前的最新版本-->
<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>8.5.5</version>
</dependency>

Minio Console上生成的两个 Key:

json 复制代码
{
    "accessKey": "SllF5SkIozb1n0b1D84M",
    "api": "s3v4",
    "path": "auto",
    "secretKey": "ZQSiCm3UDST1Q5lL1sImpOUNKIQSv6qZGucB4ku0",
    "url": "http://tcloud:9090/api/v1/service-account-credentials"
}

yml配置:

yaml 复制代码
minio:
  url: http://tcloud:9000
  accessKey: SllF5SkIozb1n0b1D84M
  secretKey: ZQSiCm3UDST1Q5lL1sImpOUNKIQSv6qZGucB4ku0
  bucketName: tcloud-minio

2.2 代码

Minio配置类:

java 复制代码
@Configuration
@ConfigurationProperties(prefix = "minio")
@Data
public class MinioConfig {
    private String url;
    private String accessKey;
    private String secretKey;
    private String bucketName;
}

Minio工具类:

java 复制代码
@Slf4j
@Component
public class MinioUtil {

    private MinioClient minioClient;

    @Resource
    private FileSystemConfig fileSystemConfig;

    @Resource
    private MinioConfig minioConfig;

    private String bucketName;

    @PostConstruct
    private void connectToServer() {
        if (fileSystemConfig.isMinioEnable()) {
            // 初始化 MinioClient
            minioClient = MinioClient.builder()
                    .endpoint(minioConfig.getUrl())
                    .credentials(minioConfig.getAccessKey(), minioConfig.getSecretKey())
                    .build();
            // 检测 bucket 是否存在
            bucketName = minioConfig.getBucketName();
            bucketExists(bucketName);
        }
    }

    /**
     * 根据 bucketName 检测是否存在 不存在则创建
     *
     * @param bucketName 名称
     */
    public void bucketExists(String bucketName) {
        try {
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
            if (!found) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            }
        } catch (Exception e) {
            log.error("bucketExists Failed!");
            e.printStackTrace();
        }
    }

    public void fileUploader(String objectName, String filePath) {
        try {
            UploadObjectArgs uploadObjectArgs = UploadObjectArgs.builder()
                    .bucket(bucketName)
                    .object(objectName)
                    .filename(filePath)
                    .build();
            minioClient.uploadObject(uploadObjectArgs);
        } catch (Exception e) {
            log.error("fileUploader [{}] Failed!", filePath);
            e.printStackTrace();
        }
    }

    public void fileDownload(String objectName, String targetFilePath) {
        Path path = new File(targetFilePath).toPath();
        try {
            GetObjectArgs getObjectArgs = GetObjectArgs.builder()
                    .bucket(bucketName)
                    .object(objectName)
                    .build();
            InputStream stream = minioClient.getObject(getObjectArgs);
            Files.copy(stream, path, StandardCopyOption.REPLACE_EXISTING);
        } catch (Exception e) {
            log.error("fileDownload [{}] Failed!", targetFilePath);
            e.printStackTrace();
        }
    }
}

2.3 测试结果

查看Buckets信息:

查看对象信息:

相关推荐
江深竹静,一苇以航5 分钟前
springboot3项目整合Mybatis-plus启动项目报错:Invalid bean definition with name ‘xxxMapper‘
java·spring boot
杜杜的man36 分钟前
【go从零单排】go中的结构体struct和method
开发语言·后端·golang
幼儿园老大*37 分钟前
走进 Go 语言基础语法
开发语言·后端·学习·golang·go
llllinuuu38 分钟前
Go语言结构体、方法与接口
开发语言·后端·golang
cookies_s_s39 分钟前
Golang--协程和管道
开发语言·后端·golang
为什么这亚子41 分钟前
九、Go语言快速入门之map
运维·开发语言·后端·算法·云原生·golang·云计算
豪宇刘1 小时前
SpringBoot+Shiro权限管理
java·spring boot·spring
想进大厂的小王1 小时前
项目架构介绍以及Spring cloud、redis、mq 等组件的基本认识
redis·分布式·后端·spring cloud·微服务·架构
customer081 小时前
【开源免费】基于SpringBoot+Vue.JS医院管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·开源·intellij-idea