MinIO介绍
MinIO 是一个基于Apache License v2.0开源协议的对象存储服务。它兼容亚马逊S3云存储服务接口,非常适合于存储大容量非结构化的数据,例如图片、视频、日志文件、备份数据和容器/虚拟机镜像等,而
一个对象文件可以是任意大小,从几kb到最大5T不等
使用docker来安装MinIO
docker run -d -p 9000:9000 -p 50000:50000 --name minio
-e “MINIO_ROOT_USER=admin”
-e “MINIO_ROOT_PASSWORD=admin”
-v /mnt/data:/data
-v /mnt/config:/root/.minio
minio/minio server --console-address “:50000” /data
使用 http://ip:50000/login 地址来访问控制面板,账号admin,密码admin
环境已经搭建好了,开始我们的第一步,创建一个桶为test
并且设置这个桶的隐私规则:public
获取accessKey和secretkey
开始Springboot整合MinIO
导入依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.2.1</version>
</dependency>
构建MinioClient对象,并交给spring管理
@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioProperties {
private String endpoint;
private String accessKey;
private String secretKey;
}
@Configuration
public class MinioConfig {
@Autowired
private MinioProperties minioProperties;
@Bean
public MinioClient minioClient() {
MinioClient minioClient =
MinioClient.builder()
.endpoint(minioProperties.getEndpoint())
.credentials(minioProperties.getAccessKey(),
minioProperties.getSecretKey())
.build();
return minioClient;
}
}
application.yml
#minio配置信息
minio:
endpoint: http://IP:9000
accessKey: 你的access
secretKey: 你的secret
bucketName: test
文件上传和下载的demo
package com.wuwei.reggie.controller;
import io.minio.*;
import io.minio.errors.*;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
@RestController
@RequestMapping("/minio")
public class CommonController {
@Autowired
private MinioClient minioClient;
@Value("${minio.bucketName}")
private String bucketName;
/**
* 文件上传(minio)
* @param file
* @return
*/
@PostMapping("/upload")
public String upload(MultipartFile file){
if(file == null){
return "文件不能为空";
}
String originalFilename = file.getOriginalFilename();//获取原始文件名
String suffix =originalFilename.substring(originalFilename.lastIndexOf("."));//获取文件类型
String fileName = UUID.randomUUID().toString() + suffix;//重新生成的文件名
InputStream in;
try {
in = file.getInputStream();
minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object(fileName).stream(
in, file.getSize(), -1)
.contentType(file.getContentType())
.build());
in.close();
} catch (Exception e) {
e.printStackTrace();
return "上传失败";
}
return fileName;
}
/**
* 文件下载
* @param name
* @param response
*/
@GetMapping("/download")
public void download(String name , HttpServletResponse response){
InputStream fileInputStream = null;
try {
response.setContentType("image/jpeg");
//输出流,将文件写到浏览器
ServletOutputStream outputStream = response.getOutputStream();
//文件下载
fileInputStream = minioClient.getObject(
GetObjectArgs.builder()
.bucket(bucketName)
.object(name)
.build());
IOUtils.copy(fileInputStream, outputStream);//将图片负责到输出流
//关闭资源
outputStream.close();
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
}