大家好,我是一安,今天介绍一个高性能对象存储MinIo
之前公司使用的是FastDFS,一个开源的轻量级分布式文件系统,为互联网应用量身定做,简单、灵活、高效,采用C语言开发,由阿里巴巴开发并开源
FastDFS对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载、文件删除)等,解决了大容量文件存储的问题,特别适合以文件为载体的在线服务,如相册网站、文档网站、图片网站、视频网站等等。
FastDFS充分考虑了冗余备份、线性扩容等机制,并注重高可用、高性能等指标,使用FastDFS很容易搭建一套高性能的文件服务器集群提供文件上传、下载等服务
但由于上电信云,电信云提供的ctg-slb(也就是nginx)不支持编译安装fastdfs-nginx-module,所以考虑使用MinIo代替FastDFS
MinIO是根据GNU Affero通用公共许可证v3.0发布的高性能对象存储。它与Amazon S3云存储服务兼容。使用MinIO构建用于机器学习,分析和应用程序数据工作负载的高性能基础架构
MinIo安装
本文采用docker方式安装测试
docker run -p 9000:9000 -p 9001:9001 --name minio -d -e "MINIO_ACCESS_KEY=admin12345" -e "MINIO_SECRET_KEY=admin12345" -v /mydata/minio/data:/data -v /mydata/minio/config:/root/.minio minio/minio server /data --console-address ":9000" --address ":9001"
命令解释如下:
-p:9000是图形界面的端口,9001是API的端口,在使用SDK连接需要用到
MINIO_ACCESS_KEY:指定图形界面的用户名
MINIO_SECRET_KEY:指定图形界面的密码
1.登录界面:2.登录成功:3.创建一个测试桶4.创建accessKey和secretKey以上搭建的都是单机版的,想要了解分布式 的方式请查看官网文档:https://www.minio.org.cn/
SpringBoot集成 MinIo
致谢:代码示例来自码猿技术专栏 ,作者不才陈某
1.引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springboot-minio</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.6.5</version>
</dependency>
</dependencies>
</project>
2.配置application.yml
spring:
profiles:
active: prod
application:
name: springboot-minio
servlet:
multipart:
max-file-size: -1
server:
port: 8011
minio:
# 访问的url
endpoint: http://192.168.5.128
# API的端口
port: 9001
# 秘钥
accessKey: Oe3vRjfPJ1a5USrL
secretKey: F8USKX2lRkjIB7C4gKkW37C3r9KgN2i5
secure: false
bucket-name: test # 桶名 我这是给出了一个默认桶名
image-size: 10485760 # 我在这里设定了 图片文件的最大大小
file-size: 1073741824 # 此处是设定了文件的最大大小
3.编写配置文件
@Data
@Configuration
@ConfigurationProperties(prefix = "minio")
public class MinioProperties {
/**
* 是一个URL,域名,IPv4或者IPv6地址
*/
private String endpoint;
/**
* TCP/IP端口号
*/
private Integer port;
/**
* accessKey类似于用户ID,用于唯一标识你的账户
*/
private String accessKey;
/**
* secretKey是你账户的密码
*/
private String secretKey;
/**
* 如果是true,则用的是https而不是http,默认值是true
*/
private boolean secure;
/**
* 默认存储桶
*/
private String bucketName;
/**
* 图片的最大大小
*/
private long imageSize;
/**
* 其他文件的最大大小
*/
private long fileSize;
/**
* 官网给出的 构造方法
* 此类是 客户端进行操作的类
*/
@Bean
public MinioClient minioClient() {
MinioClient minioClient =
MinioClient.builder()
.credentials(accessKey, secretKey)
.endpoint(endpoint,port,secure)
.build();
return minioClient;
}
}
4.minioClient工具类
@Component
public class MinioUtil {
private final MinioClient minioClient;
private final MinioProperties minioProperties;
public MinioUtil(MinioClient minioClient, MinioProperties minioProperties) {
this.minioClient = minioClient;
this.minioProperties = minioProperties;
}
/**
* 检查存储桶是否存在
*
* @param bucketName 存储桶名称
* @return
*/
@SneakyThrows
public boolean bucketExists(String bucketName) {
boolean found =
minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (found) {
System.out.println(bucketName + " exists");
} else {
System.out.println(bucketName + " does not exist");
}
return found;
}
/**
* 创建存储桶
*
* @param bucketName 存储桶名称
*/
@SneakyThrows
public boolean makeBucket(String bucketName) {
boolean flag = bucketExists(bucketName);
if (!flag) {
minioClient.makeBucket(
MakeBucketArgs.builder()
.bucket(bucketName)
.build());
return true;
} else {
return false;
}
}
/**
* 列出所有存储桶名称
*
* @return
*/
@SneakyThrows
public List<String> listBucketNames() {
List<Bucket> bucketList = listBuckets();
List<String> bucketListName = new ArrayList<>();
for (Bucket bucket : bucketList) {
bucketListName.add(bucket.name());
}
return bucketListName;
}
/**
* 列出所有存储桶
*
* @return
*/
@SneakyThrows
public List<Bucket> listBuckets() {
return minioClient.listBuckets();
}
/**
* 删除存储桶
*
* @param bucketName 存储桶名称
* @return
*/
@SneakyThrows
public boolean removeBucket(String bucketName) {
boolean flag = bucketExists(bucketName);
if (flag) {
Iterable<Result<Item>> myObjects = listObjects(bucketName);
for (Result<Item> result : myObjects) {
Item item = result.get();
// 有对象文件,则删除失败
if (item.size() > 0) {
return false;
}
}
// 删除存储桶,注意,只有存储桶为空时才能删除成功。
minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
flag = bucketExists(bucketName);
if (!flag) {
return true;
}
}
return false;
}
/**
* 列出存储桶中的所有对象名称
*
* @param bucketName 存储桶名称
* @return
*/
@SneakyThrows
public List<String> listObjectNames(String bucketName) {
List<String> listObjectNames = new ArrayList<>();
boolean flag = bucketExists(bucketName);
if (flag) {
Iterable<Result<Item>> myObjects = listObjects(bucketName);
for (Result<Item> result : myObjects) {
Item item = result.get();
listObjectNames.add(item.objectName());
}
}else{
listObjectNames.add("存储桶不存在");
}
return listObjectNames;
}
/**
* 列出存储桶中的所有对象
*
* @param bucketName 存储桶名称
* @return
*/
@SneakyThrows
public Iterable<Result<Item>> listObjects(String bucketName) {
boolean flag = bucketExists(bucketName);
if (flag) {
return minioClient.listObjects(
ListObjectsArgs.builder().bucket(bucketName).build());
}
return null;
}
/**
* 文件上传
*
* @param bucketName
* @param multipartFile
*/
@SneakyThrows
public void putObject(String bucketName, MultipartFile multipartFile, String filename, String fileType) {
InputStream inputStream = new ByteArrayInputStream(multipartFile.getBytes());
minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object(filename).stream(
inputStream, -1, minioProperties.getFileSize())
.contentType(fileType)
.build());
}
/**
* 文件访问路径
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return
*/
@SneakyThrows
public String getObjectUrl(String bucketName, String objectName) {
boolean flag = bucketExists(bucketName);
String url = "";
if (flag) {
url = minioClient.getPresignedObjectUrl(
GetPresignedObjectUrlArgs.builder()
.method(Method.GET)
.bucket(bucketName)
.object(objectName)
.expiry(2, TimeUnit.MINUTES)
.build());
System.out.println(url);
}
return url;
}
/**
* 删除一个对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
*/
@SneakyThrows
public boolean removeObject(String bucketName, String objectName) {
boolean flag = bucketExists(bucketName);
if (flag) {
minioClient.removeObject(
RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
return true;
}
return false;
}
/**
* 以流的形式获取一个文件对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return
*/
@SneakyThrows
public InputStream getObject(String bucketName, String objectName) {
boolean flag = bucketExists(bucketName);
if (flag) {
StatObjectResponse statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.size() > 0) {
InputStream stream =
minioClient.getObject(
GetObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build());
return stream;
}
}
return null;
}
/**
* 获取对象的元数据
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return
*/
@SneakyThrows
public StatObjectResponse statObject(String bucketName, String objectName) {
boolean flag = bucketExists(bucketName);
if (flag) {
StatObjectResponse stat =
minioClient.statObject(
StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
return stat;
}
return null;
}
/**
* 删除指定桶的多个文件对象,返回删除错误的对象列表,全部删除成功,返回空列表
*
* @param bucketName 存储桶名称
* @param objectNames 含有要删除的多个object名称的迭代器对象
* @return
*/
@SneakyThrows
public boolean removeObject(String bucketName, List<String> objectNames) {
boolean flag = bucketExists(bucketName);
if (flag) {
List<DeleteObject> objects = new LinkedList<>();
for (int i = 0; i < objectNames.size(); i++) {
objects.add(new DeleteObject(objectNames.get(i)));
}
Iterable<Result<DeleteError>> results =
minioClient.removeObjects(
RemoveObjectsArgs.builder().bucket(bucketName).objects(objects).build());
for (Result<DeleteError> result : results) {
DeleteError error = result.get();
System.out.println(
"Error in deleting object " + error.objectName() + "; " + error.message());
return false;
}
}
return true;
}
/**
* 以流的形式获取一个文件对象(断点下载)
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param offset 起始字节的位置
* @param length 要读取的长度 (可选,如果无值则代表读到文件结尾)
* @return
*/
@SneakyThrows
public InputStream getObject(String bucketName, String objectName, long offset, Long length) {
boolean flag = bucketExists(bucketName);
if (flag) {
StatObjectResponse statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.size() > 0) {
InputStream stream =
minioClient.getObject(
GetObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.offset(offset)
.length(length)
.build());
return stream;
}
}
return null;
}
/**
* 通过InputStream上传对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param inputStream 要上传的流
* @param contentType 要上传的文件类型 MimeTypeUtils.IMAGE_JPEG_VALUE
* @return
*/
@SneakyThrows
public boolean putObject(String bucketName, String objectName, InputStream inputStream,String contentType) {
boolean flag = bucketExists(bucketName);
if (flag) {
minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(
inputStream, -1, minioProperties.getFileSize())
.contentType(contentType)
.build());
StatObjectResponse statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.size() > 0) {
return true;
}
}
return false;
}
}
5.文件工具类
public class FileTypeUtils {
private final static String IMAGE_TYPE = "image/";
private final static String AUDIO_TYPE = "audio/";
private final static String VIDEO_TYPE = "video/";
private final static String APPLICATION_TYPE = "application/";
private final static String TXT_TYPE = "text/";
public static String getFileType(MultipartFile multipartFile) {
InputStream inputStream = null;
String type = null;
try {
inputStream = multipartFile.getInputStream();
type = FileTypeUtil.getType(inputStream,multipartFile.getOriginalFilename());
if (type.equalsIgnoreCase("JPG") || type.equalsIgnoreCase("JPEG")
|| type.equalsIgnoreCase("GIF") || type.equalsIgnoreCase("PNG")
|| type.equalsIgnoreCase("BMP") || type.equalsIgnoreCase("PCX")
|| type.equalsIgnoreCase("TGA") || type.equalsIgnoreCase("PSD")
|| type.equalsIgnoreCase("TIFF")) {
return IMAGE_TYPE+type;
}
if (type.equalsIgnoreCase("mp3") || type.equalsIgnoreCase("OGG")
|| type.equalsIgnoreCase("WAV") || type.equalsIgnoreCase("REAL")
|| type.equalsIgnoreCase("APE") || type.equalsIgnoreCase("MODULE")
|| type.equalsIgnoreCase("MIDI") || type.equalsIgnoreCase("VQF")
|| type.equalsIgnoreCase("CD")) {
return AUDIO_TYPE+type;
}
if (type.equalsIgnoreCase("mp4") || type.equalsIgnoreCase("avi")
|| type.equalsIgnoreCase("MPEG-1") || type.equalsIgnoreCase("RM")
|| type.equalsIgnoreCase("ASF") || type.equalsIgnoreCase("WMV")
|| type.equalsIgnoreCase("qlv") || type.equalsIgnoreCase("MPEG-2")
|| type.equalsIgnoreCase("MPEG4") || type.equalsIgnoreCase("mov")
|| type.equalsIgnoreCase("3gp")) {
return VIDEO_TYPE+type;
}
if (type.equalsIgnoreCase("doc") || type.equalsIgnoreCase("docx")
|| type.equalsIgnoreCase("ppt") || type.equalsIgnoreCase("pptx")
|| type.equalsIgnoreCase("xls") || type.equalsIgnoreCase("xlsx")
|| type.equalsIgnoreCase("zip")||type.equalsIgnoreCase("jar")
||type.equalsIgnoreCase("java")) {
return APPLICATION_TYPE+type;
}
if (type.equalsIgnoreCase("txt")||type.equalsIgnoreCase("csv")) {
return TXT_TYPE+type;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
6.业务层
@Service
public class MinioServiceImpl implements MinioService {
@Autowired
private MinioUtil minioUtil;
@Autowired
private MinioProperties minioProperties;
/**
* 判断 bucket是否存在
*
* @param bucketName
* @return
*/
@Override
public boolean bucketExists(String bucketName) {
return minioUtil.bucketExists(bucketName);
}
/**
* 创建 bucket
*
* @param bucketName
*/
@Override
public void makeBucket(String bucketName) {
minioUtil.makeBucket(bucketName);
}
/**
* 列出所有存储桶名称
* @return
*/
@Override
public List<String> listBucketName() {
return minioUtil.listBucketNames();
}
/**
* 列出所有存储桶 信息
*
* @return
*/
@Override
public List<Bucket> listBuckets() {
return minioUtil.listBuckets();
}
/**
* 根据桶名删除桶
* @param bucketName
*/
@Override
public boolean removeBucket(String bucketName) {
return minioUtil.removeBucket(bucketName);
}
/**
* 列出存储桶中的所有对象名称
* @param bucketName
* @return
*/
@Override
public List<String> listObjectNames(String bucketName) {
return minioUtil.listObjectNames(bucketName);
}
/**
* 文件上传
*
* @param multipartFile
* @param bucketName
*/
@Override
public String putObject(MultipartFile file, String bucketName,String fileType) {
try {
bucketName = StringUtils.isNotBlank(bucketName) ? bucketName : minioProperties.getBucketName();
if (!this.bucketExists(bucketName)) {
this.makeBucket(bucketName);
}
String fileName = file.getOriginalFilename();
String objectName = UUID.randomUUID().toString().replaceAll("-", "")
+ fileName.substring(fileName.lastIndexOf("."));
minioUtil.putObject(bucketName, file, objectName,fileType);
return minioProperties.getEndpoint()+":"+minioProperties.getPort()+"/"+bucketName+"/"+objectName;
} catch (Exception e) {
e.printStackTrace();
return "上传失败";
}
}
/**
* 文件流下载
* @param bucketName
* @param objectName
* @return
*/
@Override
public InputStream downloadObject(String bucketName, String objectName) {
return minioUtil.getObject(bucketName,objectName);
}
/**
* 删除文件
* @param bucketName
* @param objectName
*/
@Override
public boolean removeObject(String bucketName, String objectName) {
return minioUtil.removeObject(bucketName, objectName);
}
/**
* 批量删除文件
* @param bucketName
* @param objectNameList
* @return
*/
@Override
public boolean removeListObject(String bucketName, List<String> objectNameList) {
return minioUtil.removeObject(bucketName,objectNameList);
}
/**
* 获取文件路径
* @param bucketName
* @param objectName
* @return
*/
@Override
public String getObjectUrl(String bucketName,String objectName) {
return minioUtil.getObjectUrl(bucketName, objectName);
}
}
7.控制层
@RequestMapping("/minio")
@RestController
public class MinioController {
@Autowired
private MinioService minioService;
@PostMapping("/upload")
public String uploadFile(MultipartFile file, String bucketName) {
String fileType = FileTypeUtils.getFileType(file);
if (fileType != null) {
return minioService.putObject(file, bucketName, fileType);
}
return "不支持的文件格式。请确认格式,重新上传!!!";
}
@PostMapping("/addBucket/{bucketName}")
public String addBucket(@PathVariable String bucketName) {
minioService.makeBucket(bucketName);
return "创建成功!!!";
}
@GetMapping("/show/{bucketName}")
public List<String> show(@PathVariable String bucketName) {
return minioService.listObjectNames(bucketName);
}
@GetMapping("/showBucketName")
public List<String> showBucketName() {
return minioService.listBucketName();
}
@GetMapping("/showListObjectNameAndDownloadUrl/{bucketName}")
public Map<String, String> showListObjectNameAndDownloadUrl(@PathVariable String bucketName) {
Map<String, String> map = new HashMap<>();
List<String> listObjectNames = minioService.listObjectNames(bucketName);
String url = "localhost:8011/minio/download/" + bucketName + "/";
listObjectNames.forEach(System.out::println);
for (int i = 0; i <listObjectNames.size() ; i++) {
map.put(listObjectNames.get(i),url+listObjectNames.get(i));
}
return map;
}
@DeleteMapping("/removeBucket/{bucketName}")
public String delBucketName(@PathVariable String bucketName) {
return minioService.removeBucket(bucketName) == true ? "删除成功" : "删除失败";
}
@DeleteMapping("/removeObject/{bucketName}/{objectName}")
public String delObject(@PathVariable("bucketName") String bucketName, @PathVariable("objectName") String objectName) {
return minioService.removeObject(bucketName, objectName) == true ? "删除成功" : "删除失败";
}
@DeleteMapping("/removeListObject/{bucketName}")
public String delListObject(@PathVariable("bucketName") String bucketName, @RequestBody List<String> objectNameList) {
return minioService.removeListObject(bucketName, objectNameList) == true ? "删除成功" : "删除失败";
}
@RequestMapping("/download/{bucketName}/{objectName}")
public void download(HttpServletResponse response, @PathVariable("bucketName") String bucketName, @PathVariable("objectName") String objectName) {
InputStream in = null;
try {
in = minioService.downloadObject(bucketName, objectName);
response.setHeader("Content-Disposition", "attachment;filename="
+ URLEncoder.encode(objectName, "UTF-8"));
response.setCharacterEncoding("UTF-8");
//将字节从InputStream复制到OutputStream 。
IOUtils.copy(in, response.getOutputStream());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
测试验证
采用apipost
请求验证
总结
MInIO虽然是个开源项目,但是功能非常强大,小型项目中完全可以用它实现对象存储,也可以使用MinIO搭建一个免费的图床
号外!号外!
如果这篇文章对你有所帮助,或者有所启发的话,帮忙点赞、在看、转发、收藏,你的支持就是我坚持下去的最大动力!
一文让你了解cookie、session、token、OAuth2
原文始发于微信公众号(一安未来):Spring Boot + minio 实现高性能存储服务,So Easy~!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/44602.html