MinIO分布式对象存储服务

MinIO

MinIO 是一个基于Apache License v2.0开源协议的对象存储服务。它兼容亚马逊S3云存储服务接口,非常适合于存储大容量非结构化的数据,例如图片、视频、日志文件、备份数据和容器/虚拟机镜像等,而一个对象文件可以是任意大小,从几kb到最大5T不等。

MinIO是一种高性能的分布式对象存储系统。它是软件定义的,可在行业标准硬件上运行,并且在Apache V2许可下是100%开放源代码。

MinIO的不同之处在于,它从一开始就被设计为私有云对象存储的标准。由于MinIO是专门为仅服务对象而构建的,因此单层体系结构可实现所有必需的功能而不会受到影响。结果是一台同时具有性能,可伸缩性和轻量级的云原生对象服务器。

尽管MinIO在传统对象存储用例(例如辅助存储灾难恢复和归档)方面表现出色,但在克服与机器学习,分析和云原生应用程序工作负载相关的私有云挑战方面却独树一帜。

下载安装

我们可以参考官方文档来进行快速的安装,网址如下:

MinIO quick start

SpringBoot+MinIO+OSS

最近在公司实习有接触到SpringBoot 集成MinIO分布式文件服务,并进行相应的开发。这里贴一下我最近写的集成MinIO和OSS两种分布式文件服务的代码。

OSS

阿里云对象存储服务(Object Storage Service,简称 OSS)为您提供基于网络的数据存取服务。使用 OSS,您可以通过网络随时存储和调用包括文本、图片、音频和视频等在内的各种非结构化数据文件。

MinIO

MinIO是一种高性能的分布式对象存储系统。它是软件定义的,可在行业标准硬件上运行,并且在Apache V2许可下是100%开放源代码。

MinIO的不同之处在于,它从一开始就被设计为私有云对象存储的标准。由于MinIO是专门为仅服务对象而构建的,因此单层体系结构可实现所有必需的功能而不会受到影响。结果是一台同时具有性能,可伸缩性和轻量级的云原生对象服务器。

尽管MinIO在传统对象存储用例(例如辅助存储灾难恢复和归档)方面表现出色,但在克服与机器学习,分析和云原生应用程序工作负载相关的私有云挑战方面却独树一帜。

项目简介

通过SpringBoot 将两种存储环境进行集成,通过application.yml文件定义的属性进行两种环境的切换。我也在其中集成了Swagger,这样我们就可以轻松的访问http://localhost:8080/swagger-ui.html 进行测试。两种不同的存储仅仅实现了简单的上传和下载,如果想使用存储的其他API我们可以访问如下官网:

将相关API添加至我们的MinIOClientSlaveOssClientSlave中去,相信你肯定能很快上手。

在我们的application.yml文件中我们只需要配合MinIO或者是Oss相关属性即可切换我们底层的存储。

application.yml

1
2
3
4
5
6
7
cloudclient:
status: 1 # 0:启动OSS 1: 启动MinIO
url: http://127.0.0.1:9000 #OSS URL 或者是MinIOURL
accesskey: minioadmin #登录OSS账户 或者是MinIO账户
secretkey: minioadmin #登录OSS账户 或者是MinIO密码
bucketName: xxxx #代表你存储的bucket名字
storeDir: / # storeDir代表路径
  • 依赖
1
2
3
4
5
6
7
8
9
10
11
12
<!--minio文件系统-->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>6.0.2</version>
</dependency>
<!--Lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependenc
  • CloudClientController
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@RestController
public class CloudClientController {

@Value("${cloudclient.status}")
private Integer status;
@Value("${cloudclient.url}")
private String url;
@Value("${cloudclient.accesskey}")
private String accesskey;
@Value("${cloudclient.secretkey}")
private String secretkey;
@Value("${cloudclient.bucketName}")
private String bucketName;
@Value("${cloudclient.storeDir}")
private String storeDir;

/**
* 上传当前文件
* @param file
* @param objectName
*/
@PostMapping("/upload")
public String MinIOUpload(@RequestParam MultipartFile file, @RequestParam String objectName) throws InvalidPortException, InvalidEndpointException, IOException, InvalidKeyException, NoSuchAlgorithmException, XmlPullParserException, InvalidResponseException, ErrorResponseException, NoResponseException, InvalidBucketNameException, InsufficientDataException, InternalException, RegionConflictException {
//System.out.println(1+url+accesskey+secretkey);
CloudClientMaster cloudClientMaster = new CloudClientMaster(status,url,accesskey,secretkey);
return cloudClientMaster.upload(file,bucketName,objectName,storeDir);
}
/**
* 下载当前文件
* @param
* @param objectName
*/
@PostMapping("/downloadFile")
public void MinIOdownload(@RequestParam String objectName,@RequestParam String filePath) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, InternalException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, InvalidPortException, InvalidEndpointException, NoResponseException, XmlPullParserException, InvalidArgumentException {
CloudClientMaster cloudClientMaster = new CloudClientMaster(status,url,accesskey,secretkey);
cloudClientMaster.download(bucketName,objectName,filePath,storeDir);
}
}
  • MinioClientSlave
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class MinioClientSlave implements Strategy {
private String url;
private String accesskey;
private String screatkey;

private MinioClient minioClient;
public MinioClientSlave (String url,String accesskey,String screatkey) throws InvalidPortException, InvalidEndpointException {
this.minioClient = new MinioClient(url,accesskey,screatkey);
}
@Override
public String upload(MultipartFile file, String bucketName, String objectName,String storeDir) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InvalidResponseException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException, RegionConflictException {
if (file.isEmpty() || (file.getSize() == 0)) {
return "文件为空";
}
if (!minioClient.bucketExists(bucketName)) {
minioClient.makeBucket(bucketName);
}
String uploadFileTips = "";
try {
String fileName = file.getOriginalFilename();
objectName = storeDir + "/" + objectName;
InputStream inputStream = file.getInputStream();
minioClient.putObject(bucketName, objectName, inputStream,"image/jpeg");
inputStream.close();
uploadFileTips = objectName + "文件上传成功!";
return uploadFileTips;
} catch (Exception e) {
e.printStackTrace();
return "文件上传失败";
}
}
@Override
public boolean download(String bucketName, String objectName, String filePath,String storeDir) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InvalidResponseException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException, InvalidArgumentException {
objectName = storeDir + "/" + objectName;
boolean flag = minioClient.bucketExists(bucketName);
if (flag) {
ObjectStat statObject = minioClient.statObject(bucketName, objectName);
if (statObject != null && statObject.length() > 0) {
minioClient.getObject(bucketName, objectName, filePath);
return true;
}
}
return false;
}
}
  • OssClientSlave
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class OssClientSlave  implements Strategy {
private String url;
private String accesskey;
private String screatkey;
//private String storeDir;
private OSSClient ossClient;
public OssClientSlave (String url,String accesskey,String screatkey){
this.ossClient = new OSSClient(url,accesskey,screatkey);
}
/**
*
* @param file:上传的文件
* @param bucketName:上传的指定bucketName
* @param objectName:objectName
* @return
*/
@Override
public String upload(MultipartFile file, String bucketName, String objectName,String storeDir) {
String uploadFileTips = "";
if(!ossClient.isBucketExist(bucketName)){
ossClient.createBucket(bucketName);
}
try {
String fileName = file.getOriginalFilename();
objectName = storeDir + "/" + objectName;
InputStream inputStream = file.getInputStream();
/**
* bucketName:
* 如果是指定的bucketName,那就在application.yml中指定出,修改方法中传入参数
* 如果是根据传入参数,就不需要改动
*/
ossClient.putObject(new PutObjectRequest(bucketName,objectName,inputStream));
inputStream.close();
uploadFileTips = objectName + "文件上传成功!";
return uploadFileTips;
} catch (Exception e) {
e.printStackTrace();
return "文件上传失败";
}
}
/**
*
* @param bucketName:这里是我们指定的bucketName
* @param objectName:存储的objectName
* @param filePath: 存储到我们本地路径
* @return
*/
@Override
public boolean download(String bucketName, String objectName, String filePath,String storeDir) {
objectName = storeDir + "/" + objectName;
ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File(filePath));
return true;
}
}
  • Strategy
1
2
3
4
public interface Strategy {
public String upload(MultipartFile file, String bucketName, String objectName,String storeDir) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InvalidResponseException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException, RegionConflictException;
public boolean download(String bucketName,String objectName,String filePath,String storeDir) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InvalidResponseException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException, InvalidArgumentException;
}
文章目录
  1. 1. MinIO
    1. 1.1. 下载安装
  2. 2. SpringBoot+MinIO+OSS
    1. 2.1. OSS
    2. 2.2. MinIO
    3. 2.3. 项目简介
,