文件模块开发
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package com.dc.dc_project;
|
||||
|
||||
import com.dc.dc_project.config.MinioConfig;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
|
||||
@@ -4,24 +4,26 @@ package com.dc.dc_project.config;
|
||||
import io.minio.MinioClient;
|
||||
import lombok.Data;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@Data
|
||||
//@ConfigurationProperties(prefix = "minio")
|
||||
public class MinioConfig {
|
||||
|
||||
@Value("${minio.endpoint}")
|
||||
public static String MINIO_ENDPOINT;
|
||||
public String MINIO_ENDPOINT;
|
||||
|
||||
@Value("${minio.accessKey}")
|
||||
public static String MINIO_ACCESS_KEY;
|
||||
public String MINIO_ACCESS_KEY;
|
||||
|
||||
@Value("${minio.secretKey}")
|
||||
public static String MINIO_SECRET_KEY;
|
||||
public String MINIO_SECRET_KEY;
|
||||
|
||||
@Value("${minio.bucketName}")
|
||||
public static String MINIO_BUCKET_NAME;
|
||||
public String MINIO_BUCKET_NAME;
|
||||
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -36,6 +36,7 @@ public class FileServiceImpl extends ServiceImpl<FileMapper, File>
|
||||
implements FileService{
|
||||
|
||||
private final MinioService minioService;
|
||||
private final MinioConfig minioConfig;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
@@ -64,7 +65,7 @@ public class FileServiceImpl extends ServiceImpl<FileMapper, File>
|
||||
String url = minioService.uploadFile(
|
||||
inputStream,
|
||||
FileUtil.getRandomFileName(),
|
||||
MinioConfig.MINIO_BUCKET_NAME,
|
||||
minioConfig.MINIO_BUCKET_NAME,
|
||||
file.getContentType(),
|
||||
fileType.getPath()
|
||||
);
|
||||
@@ -88,7 +89,7 @@ public class FileServiceImpl extends ServiceImpl<FileMapper, File>
|
||||
// 3. 回滚:删除已上传的文件
|
||||
for (String url : uploadedUrls) {
|
||||
try {
|
||||
minioService.deleteFile(url, MinioConfig.MINIO_BUCKET_NAME);
|
||||
minioService.deleteFile(url, minioConfig.MINIO_BUCKET_NAME);
|
||||
log.info("已删除文件: {}", url);
|
||||
} catch (Exception deleteException) {
|
||||
log.error("删除文件失败: {}", url, deleteException);
|
||||
|
||||
@@ -27,6 +27,7 @@ import java.util.concurrent.TimeUnit;
|
||||
public class MinioService {
|
||||
|
||||
private final MinioClient minioClient;
|
||||
private final MinioConfig minioConfig;
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
@@ -44,7 +45,7 @@ public class MinioService {
|
||||
InsufficientDataException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return MinioConfig.MINIO_ENDPOINT + "/" + bucketName + "/" + fileName;
|
||||
return minioConfig.MINIO_ENDPOINT + "/" + bucketName + "/" + fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,7 +68,7 @@ public class MinioService {
|
||||
}
|
||||
|
||||
public void deleteFile(String url, String minioBucketName) {
|
||||
String objectName = url.replace(MinioConfig.MINIO_ENDPOINT + "/" + minioBucketName + "/", "");
|
||||
String objectName = url.replace(minioConfig.MINIO_ENDPOINT + "/" + minioBucketName + "/", "");
|
||||
try {
|
||||
minioClient.removeObject(
|
||||
RemoveObjectArgs.builder()
|
||||
|
||||
@@ -72,9 +72,9 @@ springdoc:
|
||||
path: /swagger-ui/index.html # 自定义路径,默认为"/swagger-ui/index.html"
|
||||
|
||||
minio:
|
||||
endpoint: http://192.168.1.100:9000
|
||||
accessKey: minioadmin
|
||||
secretKey: minioadmin
|
||||
endpoint: https://database.yuxindazhineng.com
|
||||
accessKey: pk0Vv6DOkSUfRqKT5EPn
|
||||
secretKey: YVhVB7seIG2q8R6yHL5CqXGYA448tgHIvtQFhq6X
|
||||
bucketName: dc-lab-system
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user