一、简介
MinIO是一个基于Apache License v2.0开源协议的对象存储服务。它兼容aws s3云存储服务接口,非常适合于存储非结构化的数据,例如图片、视频、日志文件、备份数据和容量/虚拟机镜像等,而一个对象文件可以是任意大小的,几十kb到5TB不等。
MinIO是一个非常轻量的服务,可以很简单的和其他应用的结合,如MySQL、redis...
1.基本概念
2.优点
二、安装与使用
1.安装
官网MinIO | The MinIO Quickstart Guide,下载MinIO.exe,创建data目录,命令行minio server ./data
2.JAVA的集成使用
(1)依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.3.7</version>
</dependency>
(2)MinIo配置类
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @className: MinioConfig
* @author Hope
* @date 2020/7/28 13:43
* @description: MinioConfig
*/
@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioConfig {
private final static String HTTP = "http://";
//endPoint是一个URL,域名,IPv4或者IPv6地址
private String endpoint;
//TCP/IP端口号
private int port;
//accessKey类似于用户ID,用于唯一标识你的账户
private String accessKey;
//secretKey是你账户的密码
private String secretKey;
//如果是true,则用的是https而不是http,默认值是true
private Boolean secure;
//默认存储桶
private String bucketName;
}
(3)application相关配置
# Miniio配置
minio:
endpoint: 127.0.0.1
port: 9000
accessKey: minioadmin
secretKey: minioadmin
secure: false
bucketName: "huike-crm"
configDir: "/data/excel"
(4)快速入门(Java Client API)
- 创建 MinIO 客户端
//minioConfig配置类
MinioClient minioClient =
MinioClient.builder()
.endpoint("http://"+minioConfig.getEndpoint()+":"+ minioConfig.getPort())
.credentials(minioConfig.getAccessKey(),minioConfig.getSecretKey())
.build();
- 判断桶
// Check whether 'my-bucketname' exists or not.
boolean found =
minioClient.bucketExists(BucketExistsArgs.builder().bucket("my-bucketname").build());
if (found) {
System.out.println("my-bucketname exists");
} else {
System.out.println("my-bucketname does not exist");
}
- 创建桶
// Create bucket with default region.
minioClient.makeBucket(
MakeBucketArgs.builder()
.bucket("my-bucketname")
.build());
// Create bucket with specific region.
minioClient.makeBucket(
MakeBucketArgs.builder()
.bucket("my-bucketname")
.region("us-west-1")
.build());
// Create object-lock enabled bucket with specific region.
minioClient.makeBucket(
MakeBucketArgs.builder()
.bucket("my-bucketname")
.region("us-west-1")
.objectLock(true)
.build());
- 对象下载
// Download object given the bucket, object name and output file name
minioClient.downloadObject(
DownloadObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.filename("my-object-file")
.build());
- 对象上传(给定的流作为存储桶中的对象上传)
// Upload known sized input stream.
minioClient.putObject(
PutObjectArgs.builder().bucket("my-bucketname").object("my-objectname").stream(
inputStream, size, -1)
.contentType("video/mp4")
.build());
// Upload unknown sized input stream.
minioClient.putObject(
PutObjectArgs.builder().bucket("my-bucketname").object("my-objectname").stream(
inputStream, -1, 10485760)
.contentType("video/mp4")
.build());
- 对象上传(文件中的内容作为存储桶中的对象)
// Upload an JSON file.
minioClient.uploadObject(
UploadObjectArgs.builder()
.bucket("my-bucketname").object("my-objectname").filename("person.json").build());
// Upload a video file.
minioClient.uploadObject(
UploadObjectArgs.builder()
.bucket("my-bucketname")
.object("my-objectname")
.filename("my-video.avi")
.contentType("video/mp4")
.build());