MinIO介紹
什么是MinIO
Minio 是個(gè)基于 Golang 編寫(xiě)的開(kāi)源對(duì)象存儲(chǔ)套件涎拉,基于Apache License v2.0開(kāi)源協(xié)議削葱,雖然輕量,卻擁有著不錯(cuò)的性能辑甜。它兼容亞馬遜S3云存儲(chǔ)服務(wù)接口率翅。可以很簡(jiǎn)單的和其他應(yīng)用結(jié)合使用沸久,例如 NodeJS季眷、Redis、MySQL等卷胯。
應(yīng)用場(chǎng)景
可以作為私有云的對(duì)象存儲(chǔ)服務(wù)來(lái)使用瘟裸,也可以作為云對(duì)象存儲(chǔ)的網(wǎng)關(guān)層,無(wú)縫對(duì)接 Amazon S3
或者 MicroSoft Azure
诵竭。
特點(diǎn)
高性能:作為一款高性能存儲(chǔ)话告,在標(biāo)準(zhǔn)硬件條件下,其讀寫(xiě)速率分別可以達(dá)到 55Gb/s 和 35Gb/s卵慰。并而 MinIO 支持一個(gè)對(duì)象文件可以是任意大小沙郭,從幾kb到最大5T不等。
可擴(kuò)展:不同MinIO集群可以組成聯(lián)邦裳朋,并形成一個(gè)全局的命名空間病线,并跨越多個(gè)數(shù)據(jù)中心
云原生:容器化、基于K8S的編排、多租戶支持
Amazon S3兼容:Minio使用Amazon S3 v2 / v4 API送挑“筝海可以使用Minio SDK,Minio Client惕耕,AWS SDK和AWS CLI訪問(wèn)Minio服務(wù)器纺裁。
可對(duì)接后端存儲(chǔ): 除了Minio自己的文件系統(tǒng),還支持DAS司澎、 JBODs欺缘、NAS、Google云存儲(chǔ)和Azure Blob存儲(chǔ)挤安。
-
SDK支持: 基于Minio輕量的特點(diǎn)谚殊,它得到類似Java、Python或Go等語(yǔ)言的sdk支持
JavaSDK: https://github.com/minio/minio-java
PythonSDK: https://github.com/minio/minio-py
Lambda計(jì)算: Minio服務(wù)器通過(guò)其兼容AWS SNS / SQS的事件通知服務(wù)觸發(fā)Lambda功能蛤铜。支持的目標(biāo)是消息隊(duì)列嫩絮,如Kafka,NATS围肥,AMQP絮记,MQTT,Webhooks以及Elasticsearch虐先,Redis怨愤,Postgres和MySQL等數(shù)據(jù)庫(kù)。
有操作頁(yè)面
功能簡(jiǎn)單: 這一設(shè)計(jì)原則讓MinIO不容易出錯(cuò)蛹批、更快啟動(dòng)
支持糾刪碼:MinIO使用糾刪碼撰洗、Checksum來(lái)防止硬件錯(cuò)誤和靜默數(shù)據(jù)污染。在最高冗余度配置下腐芍,即使丟失1/2的磁盤(pán)也能恢復(fù)數(shù)據(jù)
安裝
docker 安裝
下載鏡像
docker pull minio/minio
啟動(dòng)容器
docker run -d \
-p 9000:9000 \
-p 9001:9001 \
--name minio \
-v /mnt/data:/data \
-e "MINIO_ROOT_USER=minioadmin" \
-e "MINIO_ROOT_PASSWORD=minioadmin" \
minio/minio \
server /data --console-address ":9001"
參數(shù)解釋
# 命令詳解
# -e MINIO_ROOT_USER 指定用戶名
# -e MINIO_ROOT_PASSWORD 指定密碼
# -v 掛載目錄,持久化minio目錄
配置
使用 docker命令中的賬號(hào)密碼登錄http://127.0.0.1:9001/login差导,本例中用戶名:minioadmin, 密碼:minioadmin。
創(chuàng)建bucket
輸入bucket name
配置權(quán)限
創(chuàng)建access key
記錄下access key, secret key 留作后面使用
JAVA 使用
基于spring boot對(duì)接minio的文件上傳功能
引入MinIO
pom引入依賴
這里有個(gè)坑猪勇,記住版本選擇8.2.1 高版本的會(huì)有問(wèn)題设褐,運(yùn)行時(shí)報(bào)錯(cuò)。
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.2.1</version>
</dependency>
增加minio配置
application.yml
spring:
servlet:
multipart:
max-file-size: 10MB # 文件上傳大小限制
minio:
url: http://127.0.0.1:9000 # 內(nèi)網(wǎng)上傳的鏈接
bucket: mytest # bucket name
readUrl: http://127.0.0.1:9000 # 外網(wǎng)訪問(wèn)的鏈接
accessKey: xxxxxxxxxxxx # 上一步的access key
secretKey: xxxxxxxxxxxxxxxxxxx # 上一步的secret key
創(chuàng)建MinioClient Bean
@Configuration
public class MinioConfig {
@Value("${minio.url}")
private String url;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
@Bean
public MinioClient getMinioClient() {
return MinioClient.builder().endpoint(url)
.credentials(accessKey, secretKey).build();
}
}
上傳接口實(shí)現(xiàn)
實(shí)現(xiàn)上傳服務(wù)方法
先定義一個(gè)文件服務(wù)接口泣刹,以后如果有更好的實(shí)現(xiàn)方式助析,增加實(shí)現(xiàn)即可
public interface FileService {
String upload(MultipartFile file) throws Exception;
}
minio的實(shí)現(xiàn)
@Service
public class MinioFileServiceImpl implements FileService {
@Value("${minio.bucket}")
private String bucket;
@Value("${minio.readUrl}")
private String readUrl;
@Resource
private MinioClient minioClient;
@Override
public String upload(MultipartFile file) throws Exception{
String filename = getFileName(file.getOriginalFilename());
ObjectWriteResponse res = minioClient.putObject(PutObjectArgs.builder()
.bucket(bucket)
.object(filename)
.contentType(file.getContentType())
.stream(file.getInputStream(), file.getSize(), ObjectWriteArgs.MIN_MULTIPART_SIZE).build());
// 返回可訪問(wèn)的圖片鏈接
return readUrl + "/" + bucket + "/" + filename;
}
private String getFileName(String filename){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
// 設(shè)置存儲(chǔ)對(duì)象名稱
String dir = sdf.format(new Date());
int idx = filename.lastIndexOf(".");
if (idx >= 0) {
String ext = filename.substring(idx+1);
String name = System.currentTimeMillis() + filename.substring(0, idx);
filename = XString.md5(name) + "." + ext;
}
return dir + "/" +filename;
}
}
controller
@Controller
@RequestMapping("/minio")
public class MinioController {
private static final Logger LOGGER = LoggerFactory.getLogger(MinioController.class);
@Resource(type = MinioFileServiceImpl.class)
private FileService fileService;
@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public CommonResult upload(@RequestPart("file") MultipartFile file) {
try {
return CommonResult.success(fileService.upload(file));
} catch (Exception e) {
LOGGER.error("upload file exception {}", e.getMessage(), e);
return CommonResult.failed(ResultCode.FAILED);
}
}
}
測(cè)試
啟動(dòng)服務(wù),使用postman測(cè)試
postman配置
請(qǐng)求頭設(shè)置
Content-Type:multipart/form-data
查看minio上傳的文件
至此椅您,完成文件上傳功能外冀。
最后附上源碼:https://github.com/chain-zhang/study
歡迎大家指正。