若依提供的上传和下载组件有些开发者想将其更换成Minio, 我集成后在此文做个记录。
1、简单了解Minion
Minio是一款开源的对象存储服务器,它与云存储服务Amazon S3兼容,能够提供高性能、高可用性、分布式的文件存储服务。Minio适合用于存储、备份、存档、大数据的分布式存储等场景。Minio使用Go语言编写,具有轻量级、可扩展、易于部署的特点。并且可以通过API和命令行进行管理和访问。搭建Minio服务器的方法网上很多,这里就不作阐述,直接从整合开始。
2、在ruoyi-admin模块pom.xml引入Minio的依赖
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.1.0</version>
</dependency>
3、在yml里增加Minio的账号信息配置
#Minio
minio:
url: 服务器地址
access-key: 账号
secret-key: 密码
bucket-name: 桶名字
4、读取配置,初始化Minio客户端
/**
* 创建Minio客户端
*/
@Configuration
public class MinioConfig {
@Value("${minio.accessKey}")
private String accessKey;
@Value("${minio.secretKey}")
private String secretKey;
@Value("${minio.url}")
private String url;
/**
* 注入客户端
*/
@Bean
public MinioClient minioClient() {
return MinioClient.builder()
.credentials("accessKey", "secretKey")
.endpoint(url)
.build();
}
}
5、引入Minio工具类
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class MinioTemplate {
@Value("${minio.bucketName}")
public String bucketName;
@Autowired
private MinioClient minioClient;
/**
* 判断桶是否存在,如果存在返回true,如果不存在返回false
*
* @param bucketName
* @return
*/
@SneakyThrows
public Boolean existBucket(String bucketName) {
boolean exist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (!exist) {
return false;
}
return true;
}
/**
* 创建桶
*
* @param bucketName
* @return
*/
@SneakyThrows
public void makeBucket(String bucketName) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
}
/**
* 上传对象
*
* @param objectName
* @param file
* @return
* @throws Exception
*/
@SneakyThrows
public void putObject(String objectName, MultipartFile file) {
//判断 桶是否存在
boolean flag = existBucket(bucketName);
if (flag) {
PutObjectArgs args = PutObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.contentType(file.getContentType())
.stream(file.getInputStream(), file.getSize(), -1)
.build();
minioClient.putObject(args);
}
}
/**
* 删除对象
*
* @param objectName
* @return
* @throws Exception
*/
@SneakyThrows
public boolean removeObject(String objectName) {
boolean flag = existBucket(bucketName);
if (flag) {
RemoveObjectArgs args = RemoveObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build();
minioClient.removeObject(args);
return true;
}
return false;
}
/**
* 获取对象信息
*
* @param objectName
* @return
* @throws Exception
*/
@SneakyThrows
public ObjectStat getMessage(String objectName) {
boolean flag = existBucket(bucketName);
if (flag) {
ObjectStat statObject = minioClient.statObject(StatObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build());
return statObject;
}
return null;
}
/**
* 返回的文件路径,不会过期.
*
* @param objectName
* @return
*/
@SneakyThrows
public String getObjectUrl(String objectName) {
Boolean flag = existBucket(bucketName);
String url = "";
if (flag) {
url = minioClient.getObjectUrl(bucketName, objectName);
}
return url;
}
/**
* 下载
*
* @param filename
* @param response
*/
public void getObject(String filename, HttpServletResponse response) {
InputStream in = null;
OutputStream out = null;
try {
in = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(filename).build());
int length = 0;
byte[] buffer = new byte[1024];
out = response.getOutputStream();
response.reset();
response.addHeader("Content-Disposition",
" attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
response.setContentType("application/octet-stream");
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
6、进行测试
@PostMapping("/minio/upload")
public AjaxResult minioUploadFile(@RequestPart MultipartFile file) throws Exception {
try {
System.out.println("文件正在上传");
String fileName = file.getOriginalFilename();
String uuid = UUID.randomUUID().toString();
String imgType = fileName.substring(fileName.lastIndexOf("."));
String finalFileName = "images/"+uuid + imgType;
minioTemplate.putObject(finalFileName,file);
String path = "/test/" + finalFileName;
AjaxResult ajax = AjaxResult.success();
ajax.put("path", path);
return ajax;
} catch (Exception e) {
return AjaxResult.error(e.getMessage());
}
}
其它测试根据需求自己写