- MINIO 官网下载地址:
注:需要下载 MINIO SERVER(服务端) 和 MINIO CLIENT(客户端)两个文件
-
WINDOWS版本下载地址:https://min.io/download#/windows
-
LINUX版本下载地址:https://min.io/download#/linux
-
Windows环境安装示例
-
创建minio相关目录:bin、data、log
-
将下载的 MINIO 服务端与客户端文件放到bin目录下
-
启动minio服务
方式一:管理员权限打开cmd窗口,进入minio.exe所在目录,依次执行以下命令:
#设置用户名
setx MINIO_ROOT_USER blublu#设置密码
setx MINIO_ROOT_PASSWORD 123456789#启动
E:\minio\bin\minio.exe server E:\minio\data --console-address ":9001" --address ":9000" > E:\minio\logs\minio.log
方式二:bin目录下新建一个minio.bat文件,执行该文件启动,文件内容示例:
@echo off
REM 声明采用UTF-8编码
chcp 65001
echo.
echo [信息] 运行MinIO文服务器。
echo.
# 设置窗口标题
title Minio文件服务# 设置用户名
setx MINIO_ROOT_USER blublu
# 设置密码为mypassword
setx MINIO_ROOT_PASSWORD 123456789cd %~dp0
# 切换到minio.exe文件所在目录
cd E:\minio\bin
# 启动minio服务
minio.exe server E:\minio\data --console-address ":9001" --address ":9000" > E:\minio\logs\minio.log
pause
- 查看MINIO版本
minio --version
-
访问MINIO控制台:http://127.0.0.1:9001/
-
MINIO 整合 SpringBoot
<dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>3.0.10</version>
</dependency>
# MINIO配置
minio:endpoint: http://127.0.0.1:9000accesskey: blublusecretKey: 123456789
package com.blu.config;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioProp {/*** 连接url*/private String endpoint;/*** 用户名*/private String accesskey;/*** 密码*/private String secretKey;
}
package com.blu.config;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;@Configuration
@EnableConfigurationProperties(MinioProp.class)
public class MinioConfig {@Autowiredprivate MinioProp minioProp;/*** 获取 MinioClient** @return* @throws InvalidPortException* @throws InvalidEndpointException*/@Beanpublic MinioClient minioClient() throws InvalidPortException, InvalidEndpointException {return new MinioClient(minioProp.getEndpoint(), minioProp.getAccesskey(), minioProp.getSecretKey());}
}
package com.blu.utils.minio;import com.alibaba.fastjson.JSONObject;
import com.blu.config.MinioProp;
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 {@Autowiredprivate MinioClient client;@Autowiredprivate MinioProp minioProp;/*** 创建bucket** @param bucketName bucket名称*/@SneakyThrowspublic 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;}}