(1)编写sh脚本install_minio.sh
docker pull minio/minio
# --console-address 是 UI 界面的端口
docker run --name minio -p 9000:9000 -p 9001:9001 -d --restart=always -e "MINIO_ACCESS_KEY=admin" -e "MINIO_SECRET_KEY=password" -v "/data/docker/minio/data":"/data" -v "/data/docker/minio/config":"/root/.minio" minio/minio server /data/ --console-address ":9001"
(2)建两个文件夹:/data/docker/minio/data,/data/docker/minio/config(绝对路径)
mkdir /data/docker/minio/data
mkdir /data/docker/minio/config
(3)执行sh install_minio.sh ,执行完成好了之后,输入docker ps ,是这个样子的,9001是控制台登录端口,9000是api端口。
(4)访问 http://ip:9001/ ,即可,账号密码是上面配的admin,password
(5)登录进去之后,左侧菜单有个Service Accounts,点进去,创建一个账号,创建好了之后会弹出 accessKey,secretKey下载或者copy下来,等会要用。
(6)在springboot项目中添加配置
spring:
servlet:
multipart:
max-file-size: 20MB #最大上传单个文件大小:默认1M
max-request-size: 20MB #最大总上传的数据大小:默认10M(对同时上传多个文件大小的限制)
#minio文件服务器配置
minio:
address: http://ip:9000
accessKey: 你刚刚下载的accessKey
secretKey: 你刚刚下载的secretKey
bucketName: 篮子名称,可以随意取
multipart:
maxFileSize: 20Mb
maxRequestSize: 25Mb
(7)pom.xml中添加minio依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.0.2</version>
</dependency>
(8)创建两个类MinioPropertiesConfig,MinioConfig
package com.example.sbdemo.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* minio配置类
*
*/
@Component
@ConfigurationProperties(prefix = "minio")
@Data
public class MinioPropertiesConfig {
/**
* 地址
*/
private String address;
/**
* 端点
*/
private String endpoint;
/**
* 端口
*/
private int port;
/**
* 用户名
*/
private String accessKey;
/**
* 密码
*/
private String secretKey;
/**
* 桶名称
*/
private String bucketName;
/**
* 开启https
*/
private Boolean secure;
}
package com.example.sbdemo.config;
import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
@Configuration
@EnableConfigurationProperties(MinioPropertiesConfig.class)
public class MinioConfig {
@Resource
private MinioPropertiesConfig minioPropertiesConfig;
@Bean
public MinioClient getMinioClient() throws InvalidEndpointException, InvalidPortException {
MinioClient minioClient = new MinioClient(minioPropertiesConfig.getAddress(),
minioPropertiesConfig.getAccessKey(),
minioPropertiesConfig.getSecretKey());
return minioClient;
}
}
(9)核心类MinIOService
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.netty.util.internal.StringUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
@Data
@Component
public class MinIOService {
@Value("${minio.address}")
private String address;
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
@Value("${minio.bucketName}")
private String bucketName;
public MinioClient getMinioClient() {
try {
return new MinioClient(address, accessKey, secretKey);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 检查存储桶是否存在
*
* @param bucketName 存储桶名称
* @return
*/
public boolean bucketExists(MinioClient minioClient, String bucketName) {
boolean flag = false;
try {
flag = minioClient.bucketExists(bucketName);
if (flag) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return false;
}
/**
* 创建存储桶
*
* @param bucketName 存储桶名称
*/
public boolean makeBucket(MinioClient minioClient, String bucketName) {
try {
boolean flag = bucketExists(minioClient, bucketName);
//存储桶不存在则创建存储桶
if (!flag) {
minioClient.makeBucket(bucketName);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 上传文件
*
* @param file 上传文件
* @return 成功则返回文件名,失败返回空
*/
public String uploadFile(MinioClient minioClient, MultipartFile file,String dictName,String fileName) {
//创建存储桶
boolean createFlag = makeBucket(minioClient, bucketName);
//创建存储桶失败
if (createFlag == false) {
return "";
}
try {
PutObjectOptions putObjectOptions = new PutObjectOptions(file.getSize(), PutObjectOptions.MIN_MULTIPART_SIZE);
putObjectOptions.setContentType(file.getContentType());
//得到文件流
InputStream inputStream = file.getInputStream();
minioClient.putObject(bucketName, dictName+"/"+fileName, inputStream, putObjectOptions);
return fileName;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* 下载文件
*
* @param originalName 文件路径
*/
public InputStream downloadFile(MinioClient minioClient, String originalName, HttpServletResponse response) {
try {
InputStream file = minioClient.getObject(bucketName, originalName);
String filename = new String(originalName.getBytes("ISO8859-1"), StandardCharsets.UTF_8);
if (!StringUtil.isNullOrEmpty(originalName)) {
filename = originalName;
}
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
ServletOutputStream servletOutputStream = response.getOutputStream();
int len;
byte[] buffer = new byte[1024];
while ((len = file.read(buffer)) > 0) {
servletOutputStream.write(buffer, 0, len);
}
servletOutputStream.flush();
file.close();
servletOutputStream.close();
return file;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 删除文件
*
* @param fileName 文件路径
* @return
*/
public boolean deleteFile(MinioClient minioClient, String fileName) {
try {
minioClient.removeObject(bucketName, fileName);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 得到指定文件的InputStream
*
* @param originalName 文件路径
* @return
*/
public InputStream getObject(MinioClient minioClient, String originalName) {
try {
return minioClient.getObject(bucketName, originalName);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 根据文件路径得到预览文件绝对地址
*
* @param minioClient
* @param fileName 文件路径
* @return
*/
public String getPreviewFileUrl(MinioClient minioClient, String fileName) {
try {
return minioClient.presignedGetObject(bucketName,fileName);
}catch (Exception e){
e.printStackTrace();
return "";
}
}
}
(10)调用文件上传和预览
@Controller
@RequestMapping("/minio")
public class UploadController {
@Resource
MinIOService minIOService;
@Resource
MinioClient minioClient;
/*
* @Description :图片或MP4上传
* @URL :
* @param file
* @param dict
* @return : com.example.sbdemo.util.ResultUtil
* @Author :
* @Date : 2022/7/25 17:34
**/
@RequestMapping(value = "/uoloadMp4OrImages",method = RequestMethod.POST)
@ResponseBody
public ResultUtil uoload(MultipartFile file,String dict){
return ResultUtil.Ok("url", minIOService.uploadFile(minioClient,file,dict,file.getOriginalFilename()));
}
/*
* @Description : 预览图片视频
* @URL :
* @param url
* @param dict
* @param response
* @return : void
* @Author :
* @Date : 2022/7/25 17:34
**/
@GetMapping(value = "/showMp4OrImages")
public void show(String url, String dict,HttpServletResponse response) throws IOException {
InputStream stream = null;
ServletOutputStream out = null;
try {
stream = minIOService.getObject(minioClient, dict + "/" + url);
out = response.getOutputStream();
byte buff[] = new byte[1024];
int length = 0;
while ((length = stream.read(buff)) > 0) {
out.write(buff, 0, length);
}
} catch (Exception e) {
}finally {
stream.close();
out.close();
out.flush();
}
}
}
(11)查看效果
温馨提示:9001是控制台登录端口,9000是api端口哟~