Windows环境下部署文件服务器 MinIO
MinIO官网地址:https://min.io/
MinIO中文网:http://minio.org.cn/
MinIO项目github地址:https://github.com/minio/minio
1、windows服务端下载地址:https://dl.minio.io/server/minio/release/windows-amd64/minio.exe
2、安装启动:进入minio.exe所在文件夹,地址栏键入cmd
,然后回车进入当前文件夹下的cmd控制台。命令:
.\minio.exe server D:\minio #minio所在目录
运行成功之后,会出现后台的登录地址和用户名/密码:
D:\minio>.\minio.exe server D:\minio
API: http://192.168.3.95:9000 http://127.0.0.1:9000
RootUser: minioadmin
RootPass: minioadmin
Console: http://192.168.3.95:52717 http://127.0.0.1:52717
RootUser: minioadmin
RootPass: minioadmin
Command-line: https://docs.min.io/docs/minio-client-quickstart-guide
$ mc.exe alias set myminio http://192.168.3.95:9000 minioadmin minioadmin
Documentation: https://docs.min.io
WARNING: Console endpoint is listening on a dynamic port (52717), please use --console-address ":PORT" to choose a static port.
3、创建桶和上传图片:
在后台管理界面你可以上创建你的Bucket(桶),可以理解为一个文件夹;桶创建成功之后就可以上传图片了。
SpringBoot项目上传文件例程
新建一个SpringBoot项目:
1、引入pom.xml配置:
<!-- minio 相关依赖 -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.0.2</version>
</dependency>
<!-- commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.17</version>
</dependency>
<!-- JPA -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<!-- alibaba的fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
2、在 application.yml添加配置信息:
server:
port: 8081
minio:
endpoint: http://127.0.0.1:9000
accessKey: minioadmin
secretKey: minioadmin
secure: false
bucketName: "test"
spring:
servlet:
multipart:
max-request-size: 500MB
max-file-size: 500MB
3、MinioUtil工具类:
package com.minio.demo.utils;
import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.PutObjectOptions;
import io.minio.Result;
import io.minio.errors.*;
import io.minio.messages.Bucket;
import io.minio.messages.DeleteError;
import io.minio.messages.Item;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
@Component
public class MinioUtil {
@Autowired
private MinioClient minioClient;
private static final int DEFAULT_EXPIRY_TIME = 7 * 24 * 3600;
/**
* 检查存储桶是否存在
*
* @param bucketName 存储桶名称
* @return
*/
@SneakyThrows
public boolean bucketExists(String bucketName) {
boolean flag = false;
flag = minioClient.bucketExists(bucketName);
if (flag) {
return true;
}
return false;
}
/**
* 创建存储桶
*
* @param bucketName 存储桶名称
*/
@SneakyThrows
public boolean makeBucket(String bucketName) {
boolean flag = bucketExists(bucketName);
if (!flag) {
minioClient.makeBucket(bucketName);
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(bucketName);
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());
}
}
return listObjectNames;
}
/**
* 列出存储桶中的所有对象
*
* @param bucketName 存储桶名称
* @return
*/
@SneakyThrows
public Iterable<Result<Item>> listObjects(String bucketName) {
boolean flag = bucketExists(bucketName);
if (flag) {
return minioClient.listObjects(bucketName);
}
return null;
}
/**
* 通过文件上传到对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param fileName File name
* @return
*/
@SneakyThrows
public boolean putObject(String bucketName, String objectName, String fileName) {
boolean flag = bucketExists(bucketName);
if (flag) {
minioClient.putObject(bucketName, objectName, fileName, null);
ObjectStat statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.length() > 0) {
return true;
}
}
return false;
}
/**
* 文件上传
*
* @param bucketName
* @param multipartFile
*/
@SneakyThrows
public void putObject(String bucketName, MultipartFile multipartFile, String filename) {
PutObjectOptions putObjectOptions = new PutObjectOptions(multipartFile.getSize(), PutObjectOptions.MIN_MULTIPART_SIZE);
putObjectOptions.setContentType(multipartFile.getContentType());
minioClient.putObject(bucketName, filename, multipartFile.getInputStream(), putObjectOptions);
}
/**
* 通过InputStream上传对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param stream 要上传的流
* @param stream 要上传的文件类型 MimeTypeUtils.IMAGE_JPEG_VALUE
* @return
*/
@SneakyThrows
public boolean putObject(String bucketName, String objectName, InputStream stream,String contentType) {
boolean flag = bucketExists(bucketName);
if (flag) {
PutObjectOptions putObjectOptions = new PutObjectOptions(stream.available(), -1);
/**
* 开启公共类功能设置setContentType
*/
if (StringUtils.isNotBlank(contentType)) {
putObjectOptions.setContentType(contentType);
}
minioClient.putObject(bucketName, objectName, stream, putObjectOptions);
ObjectStat statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.length() > 0) {
return true;
}
}
return false;
}
/**
* 以流的形式获取一个文件对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return
*/
@SneakyThrows
public InputStream getObject(String bucketName, String objectName) {
boolean flag = bucketExists(bucketName);
if (flag) {
ObjectStat statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.length() > 0) {
InputStream stream = minioClient.getObject(bucketName, objectName);
return stream;
}
}
return null;
}
/**
* 以流的形式获取一个文件对象(断点下载)
*
* @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) {
ObjectStat statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.length() > 0) {
InputStream stream = minioClient.getObject(bucketName, objectName, offset, length);
return stream;
}
}
return null;
}
/**
* 下载并将文件保存到本地
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param fileName File name
* @return
*/
@SneakyThrows
public boolean getObject(String bucketName, String objectName, String fileName) {
boolean flag = bucketExists(bucketName);
if (flag) {
ObjectStat statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.length() > 0) {
minioClient.getObject(bucketName, objectName, fileName);
return true;
}
}
return false;
}
/**
* 删除一个对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
*/
@SneakyThrows
public boolean removeObject(String bucketName, String objectName) {
boolean flag = bucketExists(bucketName);
if (flag) {
minioClient.removeObject(bucketName, objectName);
return true;
}
return false;
}
/**
* 删除指定桶的多个文件对象,返回删除错误的对象列表,全部删除成功,返回空列表
*
* @param bucketName 存储桶名称
* @param objectNames 含有要删除的多个object名称的迭代器对象
* @return
*/
@SneakyThrows
public List<String> removeObject(String bucketName, List<String> objectNames) {
List<String> deleteErrorNames = new ArrayList<>();
boolean flag = bucketExists(bucketName);
if (flag) {
Iterable<Result<DeleteError>> results = minioClient.removeObjects(bucketName, objectNames);
for (Result<DeleteError> result : results) {
DeleteError error = result.get();
deleteErrorNames.add(error.objectName());
}
}
return deleteErrorNames;
}
/**
* 生成一个给HTTP GET请求用的presigned URL。
* 浏览器/移动端的客户端可以用这个URL进行下载,即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间,默认值是7天。
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param expires 失效时间(以秒为单位),默认是7天,不得大于七天
* @return
*/
@SneakyThrows
public String presignedGetObject(String bucketName, String objectName, Integer expires) {
boolean flag = bucketExists(bucketName);
String url = "";
if (flag) {
if (expires < 1 || expires > DEFAULT_EXPIRY_TIME) {
throw new InvalidExpiresRangeException(expires,
"expires must be in range of 1 to " + DEFAULT_EXPIRY_TIME);
}
url = minioClient.presignedGetObject(bucketName, objectName, expires);
}
return url;
}
/**
* 生成一个给HTTP PUT请求用的presigned URL。
* 浏览器/移动端的客户端可以用这个URL进行上传,即使其所在的存储桶是私有的。这个presigned URL可以设置一个失效时间,默认值是7天。
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param expires 失效时间(以秒为单位),默认是7天,不得大于七天
* @return
*/
@SneakyThrows
public String presignedPutObject(String bucketName, String objectName, Integer expires) {
boolean flag = bucketExists(bucketName);
String url = "";
if (flag) {
if (expires < 1 || expires > DEFAULT_EXPIRY_TIME) {
throw new InvalidExpiresRangeException(expires,
"expires must be in range of 1 to " + DEFAULT_EXPIRY_TIME);
}
url = minioClient.presignedPutObject(bucketName, objectName, expires);
}
return url;
}
/**
* 获取对象的元数据
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return
*/
@SneakyThrows
public ObjectStat statObject(String bucketName, String objectName) {
boolean flag = bucketExists(bucketName);
if (flag) {
ObjectStat statObject = minioClient.statObject(bucketName, objectName);
return statObject;
}
return null;
}
/**
* 文件访问路径
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return
*/
@SneakyThrows
public String getObjectUrl(String bucketName, String objectName) {
boolean flag = bucketExists(bucketName);
String url = "";
if (flag) {
url = minioClient.getObjectUrl(bucketName, objectName);
}
return url;
}
/**
* 设置存储桶策略
*
* @param bucketName 存储桶名称
* @param policy 存储桶里的对象名称
*/
public void setBucketPolicy(String bucketName, String policy) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
minioClient.setBucketPolicy(bucketName, policy);
}
/**
* 获取存储桶策略
*
* @param bucketName 存储桶名称
* @return
*/
public String getBucketPolicy(String bucketName) throws IOException, InvalidKeyException, InvalidResponseException, BucketPolicyTooLargeException, NoSuchAlgorithmException, InternalException, XmlParserException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException {
return minioClient.getBucketPolicy(bucketName);
}
public void downloadFile(String bucketName, String fileName, String originalName, HttpServletResponse response) {
try {
InputStream file = minioClient.getObject(bucketName, fileName);
String filename = new String(fileName.getBytes("ISO8859-1"), StandardCharsets.UTF_8);
if (StringUtils.isNotEmpty(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();
} catch (ErrorResponseException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
4、创建核心配置类:
package com.minio.demo.config;
import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioConfig {
// endPoint是一个URL,域名,IPv4或者IPv6地址
private String endpoint;
// TCP/IP端口号
private int port;
private String accessKey;
private String secretKey;
// 如果是true,则用的是https而不是http,默认值是true
private Boolean secure;
// 默认存储桶
private String bucketName;
@Bean
public MinioClient getMinioClient() throws InvalidEndpointException, InvalidPortException {
MinioClient minioClient = new MinioClient(endpoint, port, accessKey, secretKey, secure);
return minioClient;
}
}
5、创建MinioService:
package com.minio.demo.service;
import io.minio.ObjectStat;
import io.minio.Result;
import io.minio.errors.*;
import io.minio.messages.Item;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
public interface MinioService {
/**
* 判断 bucket是否存在
*
* @param bucketName
* @return
*/
boolean bucketExists(String bucketName);
/**
* 创建 bucket
*
* @param bucketName
*/
void makeBucket(String bucketName);
/**
* 文件上传
*
* @param bucketName
* @param objectName
* @param filename
*/
void putObject(String bucketName, String objectName, String filename);
/**
* 文件上传
*
* @param bucketName
* @param objectName
* @param stream
*/
void putObject(String bucketName, String objectName, InputStream stream, String contentType);
/**
* 文件上传
*
* @param bucketName
* @param multipartFile
*/
void putObject(String bucketName, MultipartFile multipartFile, String filename);
/**
* 删除文件
* @param bucketName
* @param objectName
*/
boolean removeObject(String bucketName,String objectName);
/**
* 下载文件
*
* @param fileName
* @param originalName
* @param response
*/
void downloadFile(String bucketName, String fileName, String originalName, HttpServletResponse response);
/**
* 获取文件路径
* @param bucketName
* @param objectName
* @return
*/
String getObjectUrl(String bucketName,String objectName);
/**
* @Description //TODO 文件下载
* @param bucketName
* @param objectName
* @return io.minio.ObjectStat
*/
ObjectStat statObject(String bucketName, String objectName);
/**
* 以流的形式获取一个文件对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return
*/
InputStream getObject(String bucketName, String objectName);
/**
* 列出存储桶中所有对象
*
* @param bucketName 存储桶名称
* @return
*/
Iterable<Result<Item>> listObjects(String bucketName);
/**
* 生成一个给HTTP GET请求用的presigned URL
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param expires 失效时间(以秒为单位),默认是7天,不得大于七天
* @return
*/
String presignedGetObject(String bucketName, String objectName, Integer expires);
/**
* 设置存储桶策略
*
* @param bucketName 存储桶名称
* @return
*/
void setBucketPolicy(String bucketName, String policy) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException;
/**
* 获取存储桶策略
*
* @param bucketName 存储桶名称
* @return
*/
String getBucketPolicy(String bucketName) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, BucketPolicyTooLargeException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException;
}
6、创建MinioServiceImpl:
package com.minio.demo.service.impl;
import com.minio.demo.service.MinioService;
import com.minio.demo.utils.MinioUtil;
import io.minio.ObjectStat;
import io.minio.Result;
import io.minio.errors.*;
import io.minio.messages.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@Service
public class MinioServiceImpl implements MinioService {
@Autowired
private MinioUtil minioUtil;
/**
* 判断 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);
}
/**
* 文件上传
*
* @param bucketName
* @param objectName
* @param filename
*/
@Override
public void putObject(String bucketName, String objectName, String filename) {
minioUtil.putObject(bucketName, objectName, filename);
}
@Override
public void putObject(String bucketName, String objectName, InputStream stream, String contentType) {
minioUtil.putObject(bucketName, objectName, stream, contentType);
}
/**
* 文件上传
*
* @param bucketName
* @param multipartFile
*/
@Override
public void putObject(String bucketName, MultipartFile multipartFile, String filename) {
minioUtil.putObject(bucketName, multipartFile, filename);
}
/**
* 删除文件
* @param bucketName
* @param objectName
*/
@Override
public boolean removeObject(String bucketName,String objectName) {
return minioUtil.removeObject(bucketName,objectName);
}
/**
* 下载文件
*
* @param fileName
* @param originalName
* @param response
*/
@Override
public void downloadFile(String bucketName, String fileName, String originalName, HttpServletResponse response) {
minioUtil.downloadFile(bucketName,fileName, originalName, response);
}
/**
* 获取文件路径
* @param bucketName
* @param objectName
* @return
*/
@Override
public String getObjectUrl(String bucketName,String objectName) {
return minioUtil.getObjectUrl(bucketName,objectName);
}
/**
* @Description //TODO 文件下载
* @param bucketName
* @param objectName
* @return io.minio.ObjectStat
*/
@Override
public ObjectStat statObject(String bucketName, String objectName) {
return minioUtil.statObject(bucketName,objectName);
}
/**
* 以流的形式获取一个文件对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return
*/
@Override
public InputStream getObject(String bucketName, String objectName) {
return minioUtil.getObject(bucketName,objectName);
}
/**
* 列出存储桶中所有对象
*
* @param bucketName 存储桶名称
* @return
*/
@Override
public Iterable<Result<Item>> listObjects(String bucketName) {
return minioUtil.listObjects(bucketName);
}
/**
* 生成一个给HTTP GET请求用的presigned URL
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param expires 失效时间(以秒为单位),默认是7天,不得大于七天
* @return
*/
@Override
public String presignedGetObject(String bucketName, String objectName, Integer expires) {
return minioUtil.presignedGetObject(bucketName, objectName, expires);
}
/**
* 设置存储桶策略
*
* @param bucketName 存储桶名称
* @param policy
* @return
*/
@Override
public void setBucketPolicy(String bucketName, String policy) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
minioUtil.setBucketPolicy(bucketName, policy);
}
/**
* 获取存储桶策略
*
* @param bucketName 存储桶名称
* @return
*/
@Override
public String getBucketPolicy(String bucketName) throws IOException, InvalidResponseException, InvalidKeyException, NoSuchAlgorithmException, BucketPolicyTooLargeException, ErrorResponseException, XmlParserException, InvalidBucketNameException, InsufficientDataException, InternalException {
return minioUtil.getBucketPolicy(bucketName);
}
}
7、创建UploadFile:
package com.minio.demo.config;
import cn.hutool.core.io.file.FileNameUtil;
import com.minio.demo.service.MinioService;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
/**
* @Description //TODO 上传策略类
*/
@Component
public class UploadFile {
@Autowired
private MinioService minioService;
@Autowired
private MinioConfig minioConfig;
@SneakyThrows
public void uploadFile(MultipartFile file, String bucketName) {
// 获取存储桶名称
bucketName = StringUtils.isNotBlank(bucketName) ? bucketName : minioConfig.getBucketName();
// 判断是否存在该存储桶
if (!minioService.bucketExists(bucketName)) {
// 不存在则创建
minioService.makeBucket(bucketName);
// 设置存储桶只读策略
String bucketPolicy ="{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"" +
"Action\":[\"s3:GetBucketLocation\",\"s3:ListBucket\"],\"Resource\":[\"arn:aws:s3:::fast\"]},{\"Effect\":\"Allow\",\"" +
"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetObject\"],\"Resource\":[\"arn:aws:s3:::" +
bucketName+"/*\"]}]}";
// 设置存储桶策略
minioService.setBucketPolicy(bucketName,bucketPolicy);
}
// 原始文件名
String fileName = file.getOriginalFilename();
// 获取文件后缀名
String extName = FileNameUtil.extName(fileName);
// 定义文件路径
String format = new SimpleDateFormat("yyyy/MM/dd/").format(new Date());
// 定义文件修改之后的名字,去除uuid中的' - '
String fileUUID = UUID.randomUUID().toString().replaceAll("-", "");
// 定义新的文件名
String objectName = format + fileUUID + "." + extName;
//上传文件
minioService.putObject(bucketName, file, objectName);
// 在控制台打印图片绝对路径
System.out.println("http://127.0.0.1:9000/"+bucketName+"/"+objectName);
}
}
8、创建Controller:
package com.minio.demo.controller;
import com.alibaba.fastjson.JSON;
import com.minio.demo.config.MinioConfig;
import com.minio.demo.result.Result;
import com.minio.demo.service.MinioService;
import com.minio.demo.config.UploadFile;
import io.minio.ObjectStat;
import io.minio.errors.*;
import io.minio.messages.Item;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.util.*;
@RequestMapping("/minio")
@RestController
@Slf4j
public class MinioController {
@Autowired
private MinioService minioService;
@Autowired
private MinioConfig minioConfig;
@Autowired
private UploadFile uploadFile;
@SneakyThrows
@PostMapping("/file")
public Result uploadFile(MultipartFile file, @RequestParam("bucketName") String bucketName) {
uploadFile.uploadFile(file, bucketName);
return Result.success();
}
@PostMapping("/files")
public Result uploadFiles(@RequestParam("multipartFiles") List<MultipartFile> files, @RequestParam("bucketName") String bucketName) {
for (MultipartFile file : files) {
uploadFile.uploadFile(file, bucketName);
}
return Result.success();
}
@GetMapping("/file")
public Result download(HttpServletResponse response, @RequestParam("fileName") String fileName, @RequestParam("bucketName") String bucketName) {
try {
//获取存储捅名
bucketName = StringUtils.isNotBlank(bucketName) ? bucketName : minioConfig.getBucketName();
//获取对象信息和对象的元数据。
ObjectStat objectStat = minioService.statObject(bucketName, fileName);
//setContentType 设置发送到客户机的响应的内容类型
response.setContentType(objectStat.contentType());
//设置响应头
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(objectStat.name(), "UTF-8"));
//文件流
InputStream object = minioService.getObject(bucketName, fileName);
//设置文件大小
response.setHeader("Content-Length", String.valueOf(objectStat.length()));
IOUtils.copy(object, response.getOutputStream());
//关闭流
object.close();
return Result.success();
} catch (Exception e) {
log.error("下载文件失败,错误信息: " + e.getMessage());
return Result.fail("下载文件失败,错误信息: " + e.getMessage());
}
}
@DeleteMapping(value = "/file")
public Result deleteFile(@RequestParam("bucketName") String bucketName, @RequestParam("objectName") String objectName) {
minioService.removeObject(bucketName, objectName);
return Result.success();
}
@GetMapping(value = "/file/list")
public Result<List<Object>> getFileList(@RequestParam("bucketName") String bucketName) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
try {
//获取存储捅名
bucketName = StringUtils.isNotBlank(bucketName) ? bucketName : minioConfig.getBucketName();
//列出存储桶中所有对象
Iterable<io.minio.Result<Item>> results = minioService.listObjects(bucketName);
//迭代器
Iterator<io.minio.Result<Item>> iterator = results.iterator();
List<Object> items = new ArrayList<>();
String format = "{'fileName':'%s','fileSize':'%s'}";
while (iterator.hasNext()) {
//返回迭代中的下一个元素。
Item item = iterator.next().get();
//封装信息
items.add(JSON.parse(String.format(format, "http://localhost:9000/"+bucketName+"/"+item.objectName(), formatFileSize(item.size()))));
}
return Result.success(items);
} catch (Exception e) {
log.error("获取文件列表失败,错误信息: " + e.getMessage());
return Result.fail("获取文件列表失败");
}
}
@GetMapping("/preview/file")
public Result<List<Object>> getPreviewFile(@RequestParam("bucketName") String bucketName,
@RequestParam("expires") Integer expires,
@RequestParam("objectName") String objectName) {
//获取存储捅名
bucketName = StringUtils.isNotBlank(bucketName) ? bucketName : minioConfig.getBucketName();
//生成一个给HTTP GET请求用的presigned URL
String filePath = minioService.presignedGetObject(bucketName, objectName, expires);
//封装信息
return Result.success(filePath);
}
@GetMapping("/previewList")
public Result<List<Object>> getPreviewList(@RequestParam("bucketName") String bucketName,
@RequestParam("expires") Integer expires
) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, InternalException, XmlParserException, InvalidBucketNameException, ErrorResponseException {
try {
//获取存储捅名
bucketName = StringUtils.isNotBlank(bucketName) ? bucketName : minioConfig.getBucketName();
//列出存储桶中所有对象
Iterable<io.minio.Result<Item>> myObjects = minioService.listObjects(bucketName);
//迭代器
Iterator<io.minio.Result<Item>> iterator = myObjects.iterator();
List<Object> items = new ArrayList<>();
String format = "{'fileName':'%s','fileSize':'%s'}";
while (iterator.hasNext()) {
//返回迭代中的下一个元素。
Item item = iterator.next().get();
//生成一个给HTTP GET请求用的presigned URL
String filePath = minioService.presignedGetObject(bucketName, item.objectName(), expires);
//封装信息
items.add(JSON.parse(String.format(format, filePath, formatFileSize(item.size()))));
}
return Result.success(items);
} catch (Exception e) {
return Result.fail("生成可以预览的文件链接失败,错误信息:" + e.getMessage());
}
}
/**
* 显示文件大小信息单位
*
* @param fileS
* @return
*/
private static String formatFileSize(long fileS) {
DecimalFormat df = new DecimalFormat("#.00");
String fileSizeString = "";
String wrongSize = "0B";
if (fileS == 0) {
return wrongSize;
}
if (fileS < 1024) {
fileSizeString = df.format((double) fileS) + " B";
} else if (fileS < 1048576) {
fileSizeString = df.format((double) fileS / 1024) + " KB";
} else if (fileS < 1073741824) {
fileSizeString = df.format((double) fileS / 1048576) + " MB";
} else {
fileSizeString = df.format((double) fileS / 1073741824) + " GB";
}
return fileSizeString;
}
}
9、创建Result:
package com.minio.demo.result;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.minio.demo.exception.BaseException;
import com.minio.demo.exception.ErrorType;
import com.minio.demo.exception.SystemErrorType;
import lombok.Getter;
import java.time.Instant;
import java.time.ZonedDateTime;
@Getter
public class Result<T> {
public static final Integer SUCCESSFUL_CODE = 200000;
public static final String SUCCESSFUL_MESG = "处理成功";
private Integer code;
private String message;
private Instant time;
@JsonInclude(JsonInclude.Include.NON_NULL)
private T data;
public Result() {
this.time = ZonedDateTime.now().toInstant();
}
/**
* @param errorType
*/
public Result(ErrorType errorType) {
this.code = errorType.getCode();
this.message = errorType.getMsg();
this.time = ZonedDateTime.now().toInstant();
}
/**
* @param errorType
* @param data
*/
public Result(ErrorType errorType, T data) {
this(errorType);
this.data = data;
}
/**
* 内部使用,用于构造成功的结果
*
* @param code
* @param message
* @param data
*/
private Result(Integer code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
this.time = ZonedDateTime.now().toInstant();
}
/**
* 快速创建成功结果并返回结果数据
*
* @param data
* @return Result
*/
public static Result success(Object data) {
return new Result<>(SUCCESSFUL_CODE, SUCCESSFUL_MESG, data);
}
/**
* 快速创建成功结果
*
* @return Result
*/
public static Result success() {
return success(null);
}
/**
* 系统异常类没有返回数据
*
* @return Result
*/
public static Result fail() {
return new Result(SystemErrorType.SYSTEM_ERROR);
}
/**
* 系统异常类没有返回数据
*
* @param baseException
* @return Result
*/
public static Result fail(BaseException baseException) {
return fail(baseException, null);
}
/**
* 系统异常类并返回结果数据
*
* @param data
* @return Result
*/
public static Result fail(BaseException baseException, Object data) {
return new Result<>(baseException.getErrorType(), data);
}
/**
* 系统异常类并返回结果数据
*
* @param errorType
* @param data
* @return Result
*/
public static Result fail(ErrorType errorType, Object data) {
return new Result<>(errorType, data);
}
/**
* 系统异常类并返回结果数据
*
* @param errorType
* @return Result
*/
public static Result fail(ErrorType errorType) {
return Result.fail(errorType, null);
}
/**
* 系统异常类并返回结果数据
*
* @param data
* @return Result
*/
public static Result fail(Object data) {
return new Result<>(SystemErrorType.SYSTEM_ERROR, data);
}
/**
* 成功code=000000
*
* @return true/false
*/
@JsonIgnore
public boolean isSuccess() {
return SUCCESSFUL_CODE.equals(this.code);
}
/**
* 失败
*
* @return true/false
*/
@JsonIgnore
public boolean isFail() {
return !isSuccess();
}
}
10、异常类:
10.1、BaseException:
package com.minio.demo.exception;
import lombok.Getter;
@Getter
public class BaseException extends RuntimeException {
/**
* 异常对应的错误类型
*/
private final ErrorType errorType;
/**
* 默认是系统异常
*/
public BaseException() {
this.errorType = SystemErrorType.SYSTEM_ERROR;
}
public BaseException(ErrorType errorType) {
this.errorType = errorType;
}
public BaseException(ErrorType errorType, String message) {
super(message);
this.errorType = errorType;
}
public BaseException(ErrorType errorType, String message, Throwable cause) {
super(message, cause);
this.errorType = errorType;
}
}
10.2、ErrorType:
package com.minio.demo.exception;
public interface ErrorType {
/**
* 返回code
*
* @return
*/
Integer getCode();
/**
* 返回msg
*
* @return
*/
String getMsg();
}
10.3、ExcelException:
package com.minio.demo.exception;
/**
* @ClassName
* @Description excel表格操作异常
* @Date $ $
**/
public class ExcelException extends BaseException {
public ExcelException() {
super(SystemErrorType.EXCEL_ERROR);
}
public ExcelException(String message) {
super(SystemErrorType.EXCEL_ERROR, message);
}
}
10.4、ServiceException:
package com.minio.demo.exception;
/**
* 业务异常
*/
public class ServiceException extends BaseException {
//TODO 对业务异常的返回码进行校验,规范到一定范围内
}
10.5、SystemErrorType:
package com.minio.demo.exception;
import lombok.Getter;
@Getter
public enum SystemErrorType implements ErrorType {
SYSTEM_ERROR(-1, "系统异常"),
SYSTEM_BUSY(100001, "系统繁忙,请稍候再试"),
GATEWAY_NOT_FOUND_SERVICE(100404, "服务未找到"),
GATEWAY_ERROR(100500, "网关异常"),
GATEWAY_CONNECT_TIME_OUT(100002, "网关超时"),
ARGUMENT_NOT_VALID(101000, "请求参数校验不通过"),
INVALID_TOKEN(101001, "无效的token"),
GET_TOKEN_ERROR(101002,"获取token失败"),
TOKEN_NOT_FOUND(101003, "token不存在"),
UPLOAD_FILE_SIZE_LIMIT(101010, "上传文件大小超过限制"),
UPLOAD_FILE_ERROR(101011, "上传文件异常"),
EXCEL_ERROR(101021, "excel表格操作异常"),
USER_ACCOUNT_OR_PWD_ERROR(101100,"用户账号或密码错误"),
USER_OLDPWD_ERROR(101101,"用户原密码输入错误"),
DUPLICATE_PRIMARY_KEY(103000,"唯一键冲突");
/**
* 错误类型码
*/
private Integer code;
/**
* 错误类型描述信息
*/
private String msg;
SystemErrorType(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
}
11、测试运行:
这里用postman进行测试:
上传文件方法:
POST localhost:8081/minio/file
Body form-data
key:file value:选择文件并上传
key:bucketName value:test
下载文件方法:
GET localhost:8081/minio/file
Body form-data
key:fileName value:2022/03/03/XXX.jpg
key:bucketName value:test
删除文件方法:
DELETE localhost:8081/minio/file
key:objectName value:2022/03/03/XXX.jpg
key:bucketName value:test
生成文件预览链接方法:
GET localhost:8081/minio/preview/file
Body form-data
key:objectName value:2022/03/03/XXX.jpg
key:bucketName value:test
key:expires value:1
查询文件列表方法:
GET localhost:8081/minio/file/list
Body form-data
key:bucketName value:test
1.上传的图片体积过大报错:
The field file exceeds its maximum permitted size of 1048576 bytes
MinIO好像并没有限制单个文件或者一次上传文件大小的限制,出现这个问题的原因是springboot限制了上传文件的大小。
可以查看源码MultipartProperties类,从源码可以得知
maxFileSize
即最大文件大小,默认被限制为1MB;maxRequestSize
即最大请求大小,默认被限制为10MB。这个时候就需要在yml文件中设置:spring:
servlet:
multipart:
max-request-size: 500MB
max-file-size: 500MB