minio文件服务器工具类
1、minio介绍
Minio是一个高性能的分布式对象存储服务,它与Amazon S3 API兼容,因此可以轻松地被现有的应用程序集成。Minio设计用于提供高可靠性和弹性,支持大规模数据存储和访问。它适用于存储不常访问的数据,如备份、日志、用户生成的内容等。
2、pom依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>3.0.10</version>
</dependency>
3、docker安装minio
4、MinioConfig
/**
* @author ychao
* minio文件服务器配置
* @date 2021/6/19 19:51
*/
public class MinioConfig {
// minio端口
public static final String PORT = "9000";
// 地址
public static final String URL = "http://127.0.0.1";
//节点地址(IP+minio端口号)
public static final String ENDPOINT = PORT + PORT;
//储存桶名称
public static final String BUCKET_NAME = "filestore";
//用户名
public static final String ACCESS_KEY = "accessKey";
//用户密码
public static final String SECRET_KEY = "secretKey";
}
5、MinioUtils
/**
* @author: yc
* @des: minio工具类
* @date: 2024/8/4 13:44
*/
public class MinioUtils {
/**
* @author: yc
* @des: 文件上传
* @date: 2024/8/4 13:45
*/
public static Map<String,String> upload(MultipartFile file, String dirPath){
Map<String, String> minioMap = new HashMap<>();
String filename = file.getOriginalFilename();
try {
//创建一个MinIO的Java客户端
MinioClient minioClient = new MinioClient(MinioConfig.ENDPOINT, MinioConfig.ACCESS_KEY, MinioConfig.SECRET_KEY);
boolean isExist = minioClient.bucketExists(MinioConfig.BUCKET_NAME);
//没有桶则创建
if (!isExist) {
//创建存储桶并设置只读权限
minioClient.makeBucket(MinioConfig.BUCKET_NAME);
minioClient.setBucketPolicy(MinioConfig.BUCKET_NAME, "*.*", PolicyType.READ_ONLY);
}
// 设置存储对象名称
String uuid = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
String objectName = dirPath + "/" + filename;
// 使用putObject上传一个文件到存储桶中
minioClient.putObject(MinioConfig.BUCKET_NAME, objectName, file.getInputStream(),0, file.getContentType());
minioMap.put("fileName", filename);
minioMap.put("fileUrl", MinioConfig.ENDPOINT + "/" + MinioConfig.BUCKET_NAME + "/" + objectName);
minioMap.put("fileSize",String.valueOf(file.getSize()));
} catch (Exception e) {
e.printStackTrace();
}
return minioMap;
}
/**
* @author: yc
* @des: 文件下载
* @date: 2024/8/4 13:47
*/
public void download(String fileUrl, String fileName, HttpServletResponse response){
OutputStream outputStream = null;
InputStream inputStream = null;
if (StringUtils.isBlank(fileUrl)) {
response.setHeader("Content-type", "text/html;charset=UTF-8");
System.out.println("fileUrl为null文件下载失败");
}
try {
// 拿到文件路径
String url = fileUrl.split(MinioConfig.PORT + "/")[1];
// 获取文件对象
inputStream = getMinioObjectInputStream(MinioConfig.BUCKET_NAME, url.substring(url.indexOf("/") + 1));
response.reset();
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
outputStream = response.getOutputStream();
IOUtils.copy(inputStream, outputStream);
} catch (Exception ex) {
response.setHeader("Content-type", "text/html;charset=UTF-8");
System.out.println("文件下载失败");
} finally {
try {
if (null != outputStream) {
outputStream.close();
}
if (null != inputStream) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @author: yc
* @des: 获取minio文件中的输入流
* @date: 2024/8/4 13:49
*/
public static InputStream getMinioObjectInputStream(String bucketName, String objectName) {
try {
MinioClient minioClient = new MinioClient(MinioConfig.ENDPOINT, MinioConfig.ACCESS_KEY, MinioConfig.SECRET_KEY);
return minioClient.getObject(bucketName, objectName);
} catch (Exception e) {
System.out.println("minio文件输入流获取失败");
}
return null;
}
}