minio-分布式文件存储系统
minio的简介
MinIO基于Apache License v2.0开源协议的对象存储服务,可以做为云存储的解决方案用来保存海量的图片,视频,文档。由于采用Golang实现,服务端可以工作在Windows,Linux, OS X和FreeBSD上。配置简单,基本是复制可执行程序,单行命令可以运行起来。
MinIO兼容亚马逊S3云存储服务接口,非常适合于存储大容量非结构化的数据,例如图片、视频、日志文件、备份数据和容器/虚拟机镜像等,而一个对象文件可以是任意大小,从几kb到最大5T不等。 官网文档:
minio的特点
- 数据保护
Minio使用Minio Erasure Code(纠删码)来防止硬件故障。即便损坏一半以上的driver,但是仍然可以从中恢复。
- 高性能
作为高性能对象存储,在标准硬件条件下它能达到55GB/s的读、35GB/s的写速率
- 可扩容
不同MinIO集群可以组成联邦,并形成一个全局的命名空间,并跨越多个数据中心
- SDK支持
基于Minio轻量的特点,它得到类似Java、Python或Go等语言的sdk支持
- 有操作页面
面向用户友好的简单操作界面,非常方便的管理Bucket及里面的文件资源
- 功能简单
这一设计原则让MinIO不容易出错、更快启动
- 丰富的API
支持文件资源的分享连接及分享链接的过期策略、存储桶操作、文件列表访问及文件上传下载的基本功能等。
- 文件变化主动通知
存储桶(Bucket)如果发生改变,比如上传对象和删除对象,可以使用存储桶事件通知机制进行监控,并通过以下方式发布出去:AMQP、MQTT、 Elasticsearch、Redis、NATS、MySQL、Kafka、Webhooks等。
minio部署(docker)
创建文件目录
mkdir -p /export/server/docker/minio/data
mkdir -p /export/server/docker/minio/config
docker run -p 9000:9000 -p 9090:9090 \--net=host \--name minio \-d --restart=always \-e "MINIO_ACCESS_KEY=minioadmin" \-e "MINIO_SECRET_KEY=minioadmin" \-v /export/server/docker/minio/data:/data \-v /export/server/docker/minio/config:/root/.minio \minio/minio server \/data --console-address ":9090" -address ":9000"
访问地址 :http://ip:9090 用户名:minioadmin 密码:minioadmin
springboot整合minio
maven依赖
<dependency><groupId>io.minio</groupId><artifactId>minio</artifactId><version>7.1.0</version>
</dependency>
配置文件
minio:url: 129.0.0.1:9000 #换成自己的minio服务端地址access-key: minioadminsecret-key: minioadminbucket-name: ding_server
配置类
@Data
@Configuration
@Component
@PropertySource(value = {"classpath:application.yml"},ignoreResourceNotFound = false, encoding = "UTF-8", name = "authorSetting.properties")
@ConfigurationProperties(value = "minio")
public class MinioProperties {@Value("${minio.url}")private String url;@Value("${minio.access-key}")private String accessKey;@Value("${minio.secret-key}")private String secretKey;@Value("${minio.bucket-name}")private String bucketName;}
工具类
import io.minio.MinioClient;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.xmlpull.v1.XmlPullParserException;import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;@Component
@Configuration
@EnableConfigurationProperties(MinioProperties.class)
public class MinioTemplate {@Autowiredprivate MinioProperties minioProperties;private MinioClient minioClient;public MinioTemplate() {}public MinioClient getMinioClient() {if (minioClient == null) {try {return new MinioClient(minioProperties.getUrl(), minioProperties.getAccessKey(), minioProperties.getSecretKey());} catch (InvalidEndpointException e) {e.printStackTrace();} catch (InvalidPortException e) {e.printStackTrace();}}return minioClient;}public void createBucket(String bucketName) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InvalidResponseException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException, RegionConflictException {MinioClient minioClient = getMinioClient();if (!minioClient.bucketExists(bucketName)) {minioClient.makeBucket(bucketName);}}/*** 获取文件外链* @param bucketName bucket 名称* @param objectName 文件名称* @param expires 过期时间 <=7* @return*/public String getObjectURL(String bucketName,String objectName,int expires) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InvalidExpiresRangeException, InvalidResponseException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException {return getMinioClient().presignedGetObject(bucketName, objectName, expires);}/*** 获取文件* @param bucketName* @param objectName* @return*/public InputStream getObject(String bucketName,String objectName) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InvalidArgumentException, InvalidResponseException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException {return getMinioClient().getObject(bucketName, objectName);}/*** 上传文件* @param bucketName* @param objectName* @param stream*/public void putObject(String bucketName, String objectName, InputStream stream) throws IOException, XmlPullParserException, NoSuchAlgorithmException, RegionConflictException, InvalidKeyException, InvalidResponseException, ErrorResponseException, NoResponseException, InvalidBucketNameException, InsufficientDataException, InternalException, InvalidArgumentException {createBucket(bucketName);getMinioClient().putObject(bucketName,objectName,stream,stream.available(),"application/octet-stream");}public void putObject(String bucketName, String objectName, InputStream stream, int size, String contextType) throws IOException, XmlPullParserException, NoSuchAlgorithmException, RegionConflictException, InvalidKeyException, InvalidResponseException, ErrorResponseException, NoResponseException, InvalidBucketNameException, InsufficientDataException, InternalException, InvalidArgumentException {createBucket(bucketName);getMinioClient().putObject(bucketName,objectName,stream,size,contextType);}/*** 删除文件* @param bucketName* @param objectName*/public void removeObject(String bucketName, String objectName) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InvalidArgumentException, InvalidResponseException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException {getMinioClient().removeObject(bucketName,objectName);}}