MinIO对象存储环境安装与使用
一、MinIO简介
MinIO 是一款高性能、分布式的对象存储系统. 它是一款软件产品, 可以100%的运行在标准硬件。即X86等低成本机器也能够很好的运行MinIO。
官网:http://docs.minio.org.cn/docs/
二、快速入门单装(单机版)
单机版安装记录三种安装方式如下:
1.容器安装方式
官网镜像地址:https://hub.docker.com/r/minio/minio
下载镜像: https://hub.docker.com/r/minio/minio
##最新版本
docker pull minio/minio:latest
##指定版本
docker pull minio/minio:RELEASE.2022-10-21T22-37-48Z.fips
创建目录
mkdir -p /home/minio/config
mkdir -p /home/minio/data
启动容器
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 /home/minio/data:/data \
-v /home/minio/config:/root/.minio \
192.168.56.1:6000/minio/minio:latest server \
/data --console-address ":9090" -address ":9000"
访问:如果开了访问墙,将对应的端口先开放,防火墙未开不用理下面的步骤
firewall-cmd --zone=public --add-port=9090/tcp --permanent
firewall-cmd --zone=public --add-port=9000/tcp --permanent
firewall-cmd --reload
访问地址:
http://192.168.56.101:9090/login
minioadmin/minioadmin
2.二进制安装方式
(1)下载文件
https://dl.min.io/server/minio/release/
wget https://dl.min.io/server/minio/release/linux-amd64/minio
(2)新建存储目录
mkdir -p /home/minio/data
(3)启动minio
chmod +x minio
export MINIO_ACCESS_KEY=minioadmin
export MINIO_SECRET_KEY=minioadmin
有可能会提示
WARNING: MINIO_ACCESS_KEY and MINIO_SECRET_KEY are deprecated.
Please use MINIO_ROOT_USER and MINIO_ROOT_PASSWORD
WARNING: Detected Linux kernel version older than 4.0.0 release
##提示旧的MINIO_ACCESS_KEY和MINIO_SECRET_KEY不推荐使用了,使用如下的
export MINIO_ROOT_USER=minioadmin
export MINIO_ROOT_PASSWORD=minioadmin
##后台启动minio
./minio server --address :9000 --console-address :9090 /home/minio/data > /home/minio/data/minio.log &
三、集成Springboot使用
1.pom.xml
<!-- minio end -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.2.1</version>
</dependency>
<!-- minio end -->
2.增加Springboot配置(application.yml)
minio:
endpoint: http://192.168.56.101:9000
accessKey: minioadmin
secretKey: minioadmin
bucketName: liutest
3.Configuration配置类
package cn.gzsendi.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.minio.MinioClient;
@Configuration
public class MinioConfig {
@Value("${minio.endpoint}")
private String endpoint;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
@Bean
public MinioClient minioClient(){
return MinioClient.builder()
.endpoint(endpoint)
.credentials(accessKey, secretKey)
.build();
}
}
4.封装操作桶及文件等操作
package cn.gzsendi.modules.minio.service;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.multipart.MultipartFile;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import cn.gzsendi.modules.framework.exception.GzsendiException;
import cn.gzsendi.modules.minio.model.MinioObjectInfo;
import io.minio.BucketExistsArgs;
import io.minio.GetObjectArgs;
import io.minio.GetObjectResponse;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.ListObjectsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveBucketArgs;
import io.minio.Result;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import lombok.extern.slf4j.Slf4j;
@Service
@Slf4j
public class MinioService {
@Autowired
private MinioClient minioClient;
/**
* 列出所有的桶名称
* @return
*/
public List<String> listBuckets(){
List<String> bucketNames = new ArrayList<String>();
try {
List<Bucket> list = minioClient.listBuckets();
for(Bucket bucket : list) {
bucketNames.add(bucket.name());
}
} catch (Exception e) {
log.error("查看桶存不存在失败:",e);
}
return bucketNames;
}
/**
* 查看桶存不存在
* @param bucketName
* @return
*/
public Boolean bucketExists(String bucketName) {
Boolean found;
try {
found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
} catch (Exception e) {
log.error("查看桶存不存在失败:",e);
return false;
}
return found;
}
/**
* 创建桶
* @param bucketName
* @return
*/
public Boolean makeBucket(String bucketName) {
try {
boolean isExist = bucketExists(bucketName);
if(isExist){
throw new GzsendiException("桶已经存在.");
}
minioClient.makeBucket(MakeBucketArgs.builder()
.bucket(bucketName)
.build());
} catch (Exception e) {
log.error("创建桶失败:",e);
return false;
}
return true;
}
/**
* 删除桶
* @param bucketName
* @return
*/
public Boolean removeBucket(String bucketName) {
try {
minioClient.removeBucket(RemoveBucketArgs.builder()
.bucket(bucketName)
.build());
} catch (Exception e) {
log.error("删除桶失败:",e);
return false;
}
return true;
}
/**
* 文件上传
* @param file
* @param fileName
* @param bucketName
* @return
*/
public Boolean upload(MultipartFile file, String fileName, String bucketName) {
try {
PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(bucketName).object(fileName)
.stream(file.getInputStream(), file.getSize(), -1).contentType(file.getContentType()).build();
//文件名称相同会覆盖
minioClient.putObject(objectArgs);
} catch (Exception e) {
log.error("文件上传失败:",e);
return false;
}
return true;
}
/**
* 文件下载
* @param bucketName
* @param fileName
* @param res
*/
public void download(String bucketName, String fileName, HttpServletResponse res) {
GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(bucketName)
.object(fileName).build();
try (GetObjectResponse response = minioClient.getObject(objectArgs)) {
byte[] buf = new byte[1024];
int len;
try (FastByteArrayOutputStream os = new FastByteArrayOutputStream()) {
while ((len = response.read(buf)) != -1) {
os.write(buf, 0, len);
}
os.flush();
byte[] bytes = os.toByteArray();
res.setCharacterEncoding("utf-8");
//设置强制下载不打开
res.setContentType("application/force-download");
res.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
try (ServletOutputStream stream = res.getOutputStream()) {
stream.write(bytes);
stream.flush();
}
}
} catch (Exception e) {
log.error("文件下载失败:",e);
}
}
/**
* 查看文件对象
* @param bucketName
* @return
* @throws IOException
* @throws XmlParserException
* @throws ServerException
* @throws NoSuchAlgorithmException
* @throws InvalidResponseException
* @throws InternalException
* @throws InsufficientDataException
* @throws IllegalArgumentException
* @throws ErrorResponseException
* @throws InvalidKeyException
* @throws JsonParseException
* @throws JsonMappingException
*/
public List<MinioObjectInfo> listObjects(String bucketName) throws JsonMappingException, JsonParseException, InvalidKeyException, ErrorResponseException, IllegalArgumentException, InsufficientDataException, InternalException, InvalidResponseException, NoSuchAlgorithmException, ServerException, XmlParserException, IOException {
Iterable<Result<Item>> results = minioClient.listObjects(
ListObjectsArgs.builder().bucket(bucketName).build());
List<MinioObjectInfo> items = new ArrayList<MinioObjectInfo>();
for (Result<Item> result : results) {
Item item = result.get();
MinioObjectInfo objectInfo = new MinioObjectInfo();
objectInfo.setObjectName(item.objectName());
objectInfo.setSize(item.size());
items.add(objectInfo);
}
return items;
}
/**
*
* @return
* @throws InvalidKeyException
* @throws ErrorResponseException
* @throws InsufficientDataException
* @throws InternalException
* @throws InvalidResponseException
* @throws NoSuchAlgorithmException
* @throws XmlParserException
* @throws ServerException
* @throws IOException
*/
public String getPresignedObjectUrl(String bucketName,String fileName) throws InvalidKeyException, ErrorResponseException, InsufficientDataException, InternalException, InvalidResponseException, NoSuchAlgorithmException, XmlParserException, ServerException, IOException{
GetPresignedObjectUrlArgs args =
GetPresignedObjectUrlArgs.builder()
.method(Method.GET)
.bucket(bucketName)
.object(fileName)
.expiry(2, TimeUnit.MINUTES)
.build();
String url = minioClient.getPresignedObjectUrl(args);
return url;
}
}
package cn.gzsendi.modules.minio.model;
import lombok.Data;
@Data
public class MinioObjectInfo {
/**
* 对象名称
*/
private String objectName;
/**
* 大小
*/
private Long size;
}
5.Controller类
package cn.gzsendi.modules.minio.controller;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import cn.gzsendi.modules.framework.model.Result;
import cn.gzsendi.modules.minio.model.MinioObjectInfo;
import cn.gzsendi.modules.minio.service.MinioService;
/**
* Minio操作控制器
* @author liujh
*
*/
@Validated
@RestController
public class MinioController {
@Autowired
private MinioService minioService;
/**
* 列出所有的桶名称
* listBuckets
*/
@GetMapping("/listBuckets")
public Result<List<String>> listBuckets() {
return new Result<>(minioService.listBuckets());
}
/**
* 文件上传,文件名称相同会覆盖上传
* @param file
* @param fileName
*/
@PostMapping("/upload")
public void upload(@Validated @RequestParam("file") MultipartFile file, @NotNull(message = "fileName不可以为空!") String fileName) {
minioService.upload(file, fileName, "liutest");
}
/**
* 文件下载
* http://localhost:8080/miniotest/download?bucketName=liutest&fileName=application.yml
* @param bucketName
* @param fileName
* @param res
*/
@GetMapping("/download")
public void download(@NotNull(message = "bucketName不可以为空!") String bucketName, @NotNull(message = "fileName不可以为空!") String fileName, HttpServletResponse res) {
minioService.download(bucketName, fileName, res);
}
/**
* 查看文件对象列表
* http://localhost:8080/miniotest/listObjects?bucketName=liutest
* listObjects
*/
@GetMapping("/listObjects")
public Result<List<MinioObjectInfo>> listObjects(String bucketName) throws Exception{
List<MinioObjectInfo> list = minioService.listObjects(bucketName);
return new Result<List<MinioObjectInfo>>(list);
}
/**
* 获取预览的路径,并设置过期时间
* http://localhost:8080/miniotest/getPresignedObjectUrl?bucketName=liutest&fileName=application.yml
* @return
* @throws Exception
*/
@GetMapping("/getPresignedObjectUrl")
public Result<String> getPresignedObjectUrl(@NotNull(message = "bucketName不可以为空!") String bucketName, @NotNull(message = "fileName不可以为空!") String fileName) throws Exception {
return new Result<String>(minioService.getPresignedObjectUrl(bucketName,fileName));
}
}
四、s3fs客户端安装及挂载
参考文档:https://blog.csdn.net/WF_crystal/article/details/135749503
1.客户端安装
yum install epel-release
yum install -y s3fs-fuse
echo "minioadmin:minioadmin" > $HOME/.passwd-s3fs #minio的账号:密码
chmod 600 $HOME/.passwd-s3fs
mkdir /miniodata #创建挂载点
2.客户端挂载
s3fs -o passwd_file=$HOME/.passwd-s3fs -o url=http://localhost:9000 -o allow_other -o nonempty -o no_check_certificate -o use_path_request_style -o umask=000 liutest /miniodata
挂载命令中参数解说(参数根据实际调整):
# url=http://10.98.66.66:9000:是两种MinIO分布式集群部署方式中负载均衡的IP和端口,如果在nginx的配置文件中servername用的是域名,url就换成域名;
# allow_other: 允许其它用户操作;
# nonempty:如果挂载点下有文件/目录,s3fs(挂载命令)无法挂载到挂载点目录(会报错:s3fs: MOUNTPOINT directory /miniodata is not empty. if you are sure this is safe, can use the 'nonempty' mount option.),可以使用该参数,指定非空挂载,挂载后,挂载点下原来的文件会被覆盖;
# no_check_certificate:不检测证书;
# use_path_request_style:是对还不支持 virtual-host 请求风格的对象存储而提供的参数,指定该参数会使用传统的 API 调用风格。阿里云 OSS 和七牛对象存储可能不需指定该参数。;
# umask=000:给定挂载目录权限为777;
# liutest:上一部分在minio创建的bucket;
# /miniodata:客户端创建的挂载点;
3.取消挂载
umount /miniodata