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信息:
查看对象信息: