fix(file): 修复MinIO服务初始化和端口配置问题
- 修正MinIO端点端口号从10990改为10900 - 移除对MinioConfig的直接依赖注入,改用minioService.isInitialized()判断 - 重构MinioService实现延迟初始化和异常处理机制 - 添加初始化状态检查方法避免未初始化时的操作 - 更新依赖配置确保OkHttp和okio版本兼容性
This commit is contained in:
@@ -12,16 +12,10 @@ import io.minio.PutObjectArgs;
|
||||
import io.minio.RemoveObjectArgs;
|
||||
import io.minio.StatObjectArgs;
|
||||
import io.minio.errors.ErrorResponseException;
|
||||
import io.minio.errors.InsufficientDataException;
|
||||
import io.minio.errors.InternalException;
|
||||
import io.minio.errors.InvalidResponseException;
|
||||
import io.minio.errors.ServerException;
|
||||
import io.minio.errors.XmlParserException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@@ -35,45 +29,63 @@ import io.minio.http.Method;
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Service
|
||||
@ConditionalOnProperty(prefix = "minio", name = "enabled", havingValue = "true")
|
||||
public class MinioService
|
||||
{
|
||||
private static final Logger log = LoggerFactory.getLogger(MinioService.class);
|
||||
|
||||
private MinioClient minioClient;
|
||||
|
||||
private MinioConfig minioConfig;
|
||||
private volatile boolean initialized = false;
|
||||
|
||||
@Autowired
|
||||
public MinioService(MinioConfig minioConfig)
|
||||
{
|
||||
this.minioConfig = minioConfig;
|
||||
this.minioClient = MinioClient.builder()
|
||||
.endpoint(minioConfig.getEndpoint())
|
||||
.credentials(minioConfig.getAccessKey(), minioConfig.getSecretKey())
|
||||
.build();
|
||||
ensureBucketExists();
|
||||
if (!minioConfig.isEnabled())
|
||||
{
|
||||
log.info("MinIO 未启用(minio.enabled=false),跳过初始化");
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
this.minioClient = MinioClient.builder()
|
||||
.endpoint(minioConfig.getEndpoint())
|
||||
.credentials(minioConfig.getAccessKey(), minioConfig.getSecretKey())
|
||||
.build();
|
||||
ensureBucketExists();
|
||||
this.initialized = true;
|
||||
log.info("MinIO 客户端初始化成功: endpoint={}, bucket={}",
|
||||
minioConfig.getEndpoint(), minioConfig.getBucketName());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.warn("MinIO 初始化失败(应用将继续运行,上传降级为本地文件系统): {}", e.getMessage());
|
||||
this.minioClient = null;
|
||||
this.initialized = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查 MinIO 客户端是否已正确初始化
|
||||
*/
|
||||
private void checkInitialized()
|
||||
{
|
||||
if (!initialized || minioClient == null)
|
||||
{
|
||||
throw new IllegalStateException("MinIO 未启用或初始化失败,请检查配置");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 确保存储桶存在,不存在则自动创建
|
||||
*/
|
||||
private void ensureBucketExists()
|
||||
private void ensureBucketExists() throws Exception
|
||||
{
|
||||
try
|
||||
String bucketName = minioConfig.getBucketName();
|
||||
if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()))
|
||||
{
|
||||
String bucketName = minioConfig.getBucketName();
|
||||
boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
|
||||
if (!exists)
|
||||
{
|
||||
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
|
||||
log.info("MinIO 存储桶 [{}] 已自动创建", bucketName);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("MinIO 存储桶初始化失败", e);
|
||||
throw new RuntimeException("MinIO 存储桶初始化失败: " + e.getMessage(), e);
|
||||
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
|
||||
log.info("MinIO 存储桶 [{}] 已自动创建", bucketName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,6 +98,7 @@ public class MinioService
|
||||
*/
|
||||
public String upload(MultipartFile file, String objectName) throws IOException
|
||||
{
|
||||
checkInitialized();
|
||||
try
|
||||
{
|
||||
String contentType = file.getContentType() != null ? file.getContentType() : "application/octet-stream";
|
||||
@@ -117,6 +130,7 @@ public class MinioService
|
||||
*/
|
||||
public String upload(InputStream stream, String objectName, long size, String contentType) throws IOException
|
||||
{
|
||||
checkInitialized();
|
||||
try
|
||||
{
|
||||
String ct = (contentType != null) ? contentType : "application/octet-stream";
|
||||
@@ -144,6 +158,7 @@ public class MinioService
|
||||
*/
|
||||
public String getUrl(String objectName)
|
||||
{
|
||||
checkInitialized();
|
||||
try
|
||||
{
|
||||
return minioClient.getPresignedObjectUrl(
|
||||
@@ -151,13 +166,12 @@ public class MinioService
|
||||
.method(Method.GET)
|
||||
.bucket(minioConfig.getBucketName())
|
||||
.object(objectName)
|
||||
.expiry(7 * 24 * 60 * 60) // 7天有效期
|
||||
.expiry(7 * 24 * 60 * 60)
|
||||
.build());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.warn("MinIO 获取 URL 失败: {}", objectName, e);
|
||||
// 返回拼接直连地址
|
||||
return minioConfig.getEndpoint() + "/" + minioConfig.getBucketName() + "/" + objectName;
|
||||
}
|
||||
}
|
||||
@@ -170,6 +184,7 @@ public class MinioService
|
||||
*/
|
||||
public void download(String objectName, OutputStream os) throws IOException
|
||||
{
|
||||
checkInitialized();
|
||||
try
|
||||
{
|
||||
try (InputStream is = minioClient.getObject(
|
||||
@@ -200,6 +215,7 @@ public class MinioService
|
||||
*/
|
||||
public void delete(String objectName) throws IOException
|
||||
{
|
||||
checkInitialized();
|
||||
try
|
||||
{
|
||||
minioClient.removeObject(
|
||||
@@ -224,6 +240,10 @@ public class MinioService
|
||||
*/
|
||||
public boolean exists(String objectName)
|
||||
{
|
||||
if (!initialized || minioClient == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
try
|
||||
{
|
||||
minioClient.statObject(
|
||||
@@ -244,6 +264,14 @@ public class MinioService
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断 MinIO 是否已正确初始化并可用
|
||||
*/
|
||||
public boolean isInitialized()
|
||||
{
|
||||
return initialized && minioClient != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 MinIO 配置
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user