可以选择集成minio
This commit is contained in:
parent
4ebb288d46
commit
28451fc538
16
pom.xml
16
pom.xml
@ -38,6 +38,7 @@
|
||||
<jaxb.version>2.3.1</jaxb.version>
|
||||
<springdoc.version>2.1.0</springdoc.version>
|
||||
<jakarta.version>6.0.0</jakarta.version>
|
||||
<minio.version>8.2.1</minio.version>
|
||||
</properties>
|
||||
|
||||
|
||||
@ -198,6 +199,13 @@
|
||||
<version>${jakarta.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Minio 文件存储 -->
|
||||
<dependency>
|
||||
<groupId>io.minio</groupId>
|
||||
<artifactId>minio</artifactId>
|
||||
<version>${minio.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 定时任务-->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
@ -260,6 +268,13 @@
|
||||
<artifactId>ruoyi-mybatis-jpa</artifactId>
|
||||
<version>${ruoyi.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- mybatis-minio-->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-minio</artifactId>
|
||||
<version>${ruoyi.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
@ -274,6 +289,7 @@
|
||||
<module>ruoyi-pay</module>
|
||||
<module>ruoyi-online</module>
|
||||
<module>ruoyi-mybatis-jpa</module>
|
||||
<module>ruoyi-minio</module>
|
||||
</modules>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
|
@ -75,6 +75,12 @@
|
||||
<artifactId>ruoyi-mybatis-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- minio -->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-minio</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- knife4j -->
|
||||
<dependency>
|
||||
<groupId>com.github.xiaoymin</groupId>
|
||||
|
6
ruoyi-admin/src/main/resources/application-minio.yml
Normal file
6
ruoyi-admin/src/main/resources/application-minio.yml
Normal file
@ -0,0 +1,6 @@
|
||||
# Minio配置
|
||||
minio:
|
||||
url: http://localhost:9000
|
||||
accessKey: minioadmin
|
||||
secretKey: minioadmin
|
||||
bucketName: ruoyi
|
@ -55,7 +55,7 @@ spring:
|
||||
# 国际化资源文件路径
|
||||
basename: i18n/messages
|
||||
profiles:
|
||||
active: druid,mybatis,oauth,pay
|
||||
active: druid,mybatis,oauth,pay,minio
|
||||
# 文件上传
|
||||
servlet:
|
||||
multipart:
|
||||
|
33
ruoyi-minio/pom.xml
Normal file
33
ruoyi-minio/pom.xml
Normal file
@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>ruoyi</artifactId>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<version>3.8.7.3.2</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>ruoyi-minio</artifactId>
|
||||
|
||||
<description>
|
||||
minio文件上传模块
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- 通用工具-->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Minio 文件存储 -->
|
||||
<dependency>
|
||||
<groupId>io.minio</groupId>
|
||||
<artifactId>minio</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,80 @@
|
||||
package com.ruoyi.minio.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import io.minio.MinioClient;
|
||||
|
||||
/**
|
||||
* Minio 配置信息
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "minio")
|
||||
public class MinioConfig {
|
||||
|
||||
/**
|
||||
* 服务地址
|
||||
*/
|
||||
private static String url;
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private static String accessKey;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private static String secretKey;
|
||||
|
||||
/**
|
||||
* 存储桶名称
|
||||
*/
|
||||
private static String bucketName;
|
||||
|
||||
public static String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
MinioConfig.url = url;
|
||||
}
|
||||
|
||||
public static String getAccessKey() {
|
||||
return accessKey;
|
||||
}
|
||||
|
||||
public void setAccessKey(String accessKey) {
|
||||
MinioConfig.accessKey = accessKey;
|
||||
}
|
||||
|
||||
public static String getSecretKey() {
|
||||
return secretKey;
|
||||
}
|
||||
|
||||
public void setSecretKey(String secretKey) {
|
||||
MinioConfig.secretKey = secretKey;
|
||||
}
|
||||
|
||||
public static String getBucketName() {
|
||||
return bucketName;
|
||||
}
|
||||
|
||||
public void setBucketName(String bucketName) {
|
||||
MinioConfig.bucketName = bucketName;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MinioClient getMinioClient() {
|
||||
try {
|
||||
return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build();
|
||||
} catch (Exception e) {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.ruoyi.minio.controller;
|
||||
|
||||
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 com.ruoyi.common.annotation.Anonymous;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.utils.file.FileUtils;
|
||||
import com.ruoyi.minio.utils.FileUploadMinioUtils;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/minio")
|
||||
public class MinioController {
|
||||
@PostMapping("/upload")
|
||||
@Anonymous
|
||||
public AjaxResult uploadFileMinio(MultipartFile file) throws Exception {
|
||||
try {
|
||||
// 上传并返回新文件名称
|
||||
String fileName = FileUploadMinioUtils.uploadMinio(file);
|
||||
AjaxResult ajax = AjaxResult.success();
|
||||
ajax.put("url", fileName);
|
||||
ajax.put("fileName", fileName);
|
||||
ajax.put("newFileName", FileUtils.getName(fileName));
|
||||
ajax.put("originalFilename", file.getOriginalFilename());
|
||||
return ajax;
|
||||
} catch (Exception e) {
|
||||
return AjaxResult.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
package com.ruoyi.minio.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
|
||||
import com.ruoyi.common.exception.file.FileSizeLimitExceededException;
|
||||
import com.ruoyi.common.exception.file.InvalidExtensionException;
|
||||
import com.ruoyi.common.utils.file.FileUploadUtils;
|
||||
import com.ruoyi.common.utils.file.MimeTypeUtils;
|
||||
import com.ruoyi.minio.config.MinioConfig;
|
||||
|
||||
public class FileUploadMinioUtils extends FileUploadUtils {
|
||||
/**
|
||||
* Minio默认上传的地址
|
||||
*/
|
||||
private static String bucketName = MinioConfig.getBucketName();
|
||||
|
||||
public static String getBucketName()
|
||||
{
|
||||
return bucketName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 以默认BucketName配置上传到Minio服务器
|
||||
*
|
||||
* @param file 上传的文件
|
||||
* @return 文件名称
|
||||
* @throws Exception
|
||||
*/
|
||||
public static final String uploadMinio(MultipartFile file) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
return uploadMinino(getBucketName(), file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new IOException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义bucketName配置上传到Minio服务器
|
||||
*
|
||||
* @param file 上传的文件
|
||||
* @return 文件名称
|
||||
* @throws Exception
|
||||
*/
|
||||
public static final String uploadMinio(MultipartFile file, String bucketName) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
return uploadMinino(bucketName, file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new IOException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private static final String uploadMinino(String bucketName, MultipartFile file, String[] allowedExtension)
|
||||
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
|
||||
InvalidExtensionException
|
||||
{
|
||||
int fileNamelength = file.getOriginalFilename().length();
|
||||
if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
|
||||
{
|
||||
throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
|
||||
}
|
||||
assertAllowed(file, allowedExtension);
|
||||
try
|
||||
{
|
||||
String fileName = extractFilename(file);
|
||||
String pathFileName = MinioUtil.uploadFile(bucketName, fileName, file);
|
||||
return pathFileName;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new IOException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package com.ruoyi.minio.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.ruoyi.common.utils.ServletUtils;
|
||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||
|
||||
import io.minio.GetPresignedObjectUrlArgs;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.PutObjectArgs;
|
||||
import io.minio.http.Method;
|
||||
|
||||
/**
|
||||
* Minio 文件存储工具类
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class MinioUtil
|
||||
{
|
||||
/**
|
||||
* 上传文件
|
||||
*
|
||||
* @param bucketName 桶名称
|
||||
* @param fileName
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String uploadFile(String bucketName, String fileName, MultipartFile multipartFile) throws IOException
|
||||
{
|
||||
String url = "";
|
||||
MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
|
||||
try (InputStream inputStream = multipartFile.getInputStream())
|
||||
{
|
||||
minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(fileName).stream(inputStream, multipartFile.getSize(), -1).contentType(multipartFile.getContentType()).build());
|
||||
url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(fileName).method(Method.GET).build());
|
||||
url = url.substring(0, url.indexOf('?'));
|
||||
return ServletUtils.urlDecode(url);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new IOException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user