MinIO是根据GNU Affero通用公共许可证v3.0发布的高性能对象存储。它与Amazon S3云存储服务兼容。使用MinIO构建用于机器学习,分析和应用程序数据工作负载的高性能基础架构。
官网地址:https://min.io/
文档地址:https://docs.min.io/
中文文档地址:http://docs.minio.org.cn/docs/
1.安装minio
wget https://dl.min.io/server/minio/release/linux-amd64/minio
chmod +x minio
sudo mv minio /usr/local/bin/
2.运行minio server
1.sh命令启动
# 设置minio用户
export MINIO_ACCESS_KEY=admin
# 设置minio密码
export MINIO_SECRET_KEY=12345678
# 后台启动minio服务
nohup ./minio server --address :9800 --console-address :9889 /home/minio/data >nohup.out 2>&1 & tail -200f nohup.out
2.sh脚本启动
minio文件目录下创建deploy.sh,vim进入编辑模式,添加如下内容,保存退出
#!/bin/bash
source /etc/profile
PIDS=`ps -ef | grep minio | grep server |awk '{print $2}'`
if [ -n "$PIDS" ]; then
echo "kill $PIDS"
kill $PIDS
fi
sleep 5
PIDS=`ps -ef | grep minio | grep server |awk '{print $2}'`
if [ -n "$PIDS" ]; then
echo "kill -9 $PIDS"
kill -9 $PIDS
fi
nohup ./minio server --address :9800 --console-address :9889 /home/minio/data >nohup.out 2>&1 &
./deploy.sh启动minio服务
3.访问minio后管
管理端地址:http://ip:9889,用户名admin,密码12345678
1.依赖引入
<dependency>
<groupId>io.miniogroupId>
<artifactId>minioartifactId>
<version>8.0.1version>
dependency>
2.修改配置文件
spring:
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
#minio配置
minio:
#key就是docker初始化是设置的,密钥相同
access-key: admin
secret-key: 12345678
url: http://localhost:9889
bucket-name: mybucket
3.创建minio配置类
package io.yjff.minio.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.minio.MinioClient;
import lombok.Data;
/**
* description: minio的配置类
*
* @author yjliu25
* @version 1.0
* @date 2022/9/23 10:22
*/
@Configuration
@ConfigurationProperties(prefix = "minio")
@Data
public class MinioConfig {
private String accessKey;
private String secretKey;
private String url;
private String bucketName;
@Bean
public MinioClient minioClient() {
return MinioClient.builder()
.endpoint(url)
.credentials(accessKey, secretKey)
.build();
}
}
4.上传文件工具类
package io.yjff.minio.util;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import io.yjff.minio.config.MinioConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import io.minio.*;
import io.minio.errors.*;
import io.minio.http.Method;
/**
* description: MinioUtil
*
* @author yjliu25
* @version 1.0
* @date 2022/9/23 10:25
*/
@Slf4j
@Component
public class MinioUtil {
@Autowired
private MinioClient minioClient;
@Autowired
private MinioConfig minioConfig;
// 设置bucket的Access Policy为custom,可永久读
private static final String BUCKET_POLICY =
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetBucketLocation\",\"s3:ListBucket\"],\"Resource\":[\"arn:aws:s3:::%s\"]},{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetObject\"],\"Resource\":[\"arn:aws:s3:::%s/*\"]}]}";
public static void main(String[] args) {
MinioClient minioClient =
MinioClient.builder().endpoint("http://127.0.0.1:9800").credentials("admin", "12345678").build();
try {
// String hearingaid = minioClient.getBucketPolicy(GetBucketPolicyArgs.builder().bucket("test").build());
// log.info("{}", hearingaid);
// SetBucketPolicyArgs test = SetBucketPolicyArgs.builder().bucket("test")
// .config(String.format(BUCKET_POLICY, "test", "test")).build();
// minioClient.setBucketPolicy(test);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* description: 创建可分享永久链接bucket date: 2022/9/23 16:31 author: yjliu25
*
* @param bucketName bucketName
*/
public void createBucket(String bucketName) {
try {
// 如存储桶不存在,创建之。
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (!found) {
// 创建存储桶。
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
// 设置链接可永久访问
minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName)
.config(String.format(BUCKET_POLICY, bucketName, bucketName)).build());
} else {
throw new RuntimeException("bucketName已存在");
}
} catch (Exception e) {
throw new RuntimeException("创建bucket异常", e);
}
}
/**
* description: 上传文件
* date: 2022/9/23 17:32
* author: yjliu25
*
* @param file
* @param fileName
*/
public ObjectWriteResponse upload(MultipartFile file, String fileName) {
// 使用putObject上传一个文件到存储桶中。
try {
InputStream inputStream = file.getInputStream();
return minioClient.putObject(PutObjectArgs.builder().bucket(minioConfig.getBucketName()).object(fileName)
.stream(inputStream, file.getSize(), -1).contentType(file.getContentType()).build());
} catch (Exception e) {
throw new RuntimeException("上传文件异常", e);
}
}
}