fix(文件中心):修复冷数据访问路径bug

This commit is contained in:
2025-08-03 14:58:06 +08:00
parent ecc482ce13
commit 537b13e60e
5 changed files with 107 additions and 19 deletions
@@ -219,6 +219,12 @@ public class HotDataSyncController {
// 检查热同步配置
result.put("hotSyncConfig", hotDataSyncService.getHotSyncConfig());
// 测试新的S3路径生成
Map<String, Object> pathTest = new HashMap<>();
pathTest.put("oldPath", "hot-sync/test-file.txt");
pathTest.put("newPath", "s3://test-file.txt");
result.put("pathTest", pathTest);
return Result.succeed(result);
} catch (Exception e) {
log.error("诊断S3配置失败", e);
@@ -279,4 +285,60 @@ public class HotDataSyncController {
return Result.failed("URL访问测试失败:" + e.getMessage());
}
}
/**
* 测试文件同步到S3
*/
@Operation(summary = "测试文件同步到S3")
@PostMapping("/test-sync")
public Result<Map<String, Object>> testFileSync(@RequestParam String fileId) {
Map<String, Object> result = new HashMap<>();
try {
log.info("测试文件同步到S3: fileId={}", fileId);
boolean success = hotDataSyncService.syncSpecificFile(fileId);
result.put("fileId", fileId);
result.put("success", success);
result.put("message", success ? "文件同步成功" : "文件同步失败");
result.put("timestamp", System.currentTimeMillis());
return Result.succeed(result);
} catch (Exception e) {
log.error("测试文件同步失败: fileId={}", fileId, e);
result.put("fileId", fileId);
result.put("error", e.getMessage());
result.put("success", false);
return Result.failed("文件同步测试失败:" + e.getMessage());
}
}
/**
* 测试URL生成
*/
@Operation(summary = "测试URL生成")
@PostMapping("/test-url-generation")
public Result<Map<String, Object>> testUrlGeneration(@RequestParam String path) {
Map<String, Object> result = new HashMap<>();
try {
log.info("测试URL生成: path={}", path);
// 通过HotDataSyncService获取URL生成信息
Map<String, Object> urlTest = hotDataSyncService.testUrlGeneration();
result.put("path", path);
result.put("urlTest", urlTest);
result.put("timestamp", System.currentTimeMillis());
return Result.succeed(result);
} catch (Exception e) {
log.error("测试URL生成失败: path={}", path, e);
result.put("path", path);
result.put("error", e.getMessage());
return Result.failed("URL生成测试失败:" + e.getMessage());
}
}
}
@@ -225,11 +225,13 @@ public class HotDataSyncService {
// 使用增强的S3服务下载并上传文件
boolean uploadSuccess = false;
if (enhancedS3Service != null) {
log.info("使用增强S3服务同步文件: {} -> {}", ossUrl, s3Path);
uploadSuccess = enhancedS3Service.downloadAndUpload(ossUrl, s3Path);
} else if (s3Service != null) {
// 使用基础S3服务(简化实现)
// 使用基础S3服务
log.warn("使用基础S3服务,功能可能受限");
uploadSuccess = true; // 临时设置为true,实际需要实现具体的上传逻辑
// 这里需要实现具体的上传逻辑
uploadSuccess = false; // 暂时设置为false,需要实现具体逻辑
} else {
log.error("S3服务未配置,无法同步文件");
return false;
@@ -384,7 +386,7 @@ public class HotDataSyncService {
private String generateS3Path(FileInfo fileInfo) {
String originalPath = fileInfo.getPath();
if (originalPath == null || originalPath.isEmpty()) {
return "hot-sync/" + fileInfo.getId() + "/" + fileInfo.getName();
return "s3://" + fileInfo.getId() + "/" + fileInfo.getName();
}
// 提取文件名,避免路径过长
@@ -398,8 +400,8 @@ public class HotDataSyncService {
}
}
// 生成简洁的S3路径
return "hot-sync/" + fileName;
// 生成真正的S3路径,直接使用文件名(不带前缀)
return fileName;
}
/**
@@ -236,10 +236,13 @@ public class TimeWindowStorageService {
*/
private boolean isS3Path(String path) {
return path != null && (
path.contains("s3://") ||
path.startsWith("s3://") ||
path.contains("minio") ||
path.startsWith("cold-storage") ||
path.contains("S3")
path.contains("S3") ||
path.startsWith("hot-sync/") || // 兼容旧的hot-sync前缀
// 对于没有前缀的路径,如果数据库中的storageType是S3,也认为是S3路径
(!path.contains("/") && !path.startsWith("http"))
);
}
@@ -68,8 +68,12 @@ public class EnhancedS3Service extends AbstractIFileService {
public String getViewUrlByPath(String name) {
try {
S3Object s3Object = parsePath(name);
log.debug("解析S3路径: {} -> bucket: {}, object: {}", name, s3Object.bucketName, s3Object.objectName);
// 使用getViewUrl方法获取预签名URL
return s3Template.getViewUrl(s3Object.bucketName, s3Object.objectName, 60); // 60分钟有效期
String url = s3Template.getViewUrl(s3Object.bucketName, s3Object.objectName, 60); // 60分钟有效期
log.debug("生成S3 URL: {}", url);
return url;
} catch (Exception e) {
log.error("获取S3文件URL失败: {}", name, e);
return "";
@@ -323,14 +327,31 @@ public class EnhancedS3Service extends AbstractIFileService {
private S3Object parsePath(String path) {
S3Object s3Object = new S3Object();
if (StrUtil.isNotEmpty(path)) {
int splitIndex = path.lastIndexOf(CommonConstant.PATH_SPLIT);
if (splitIndex != -1) {
s3Object.bucketName = path.substring(0, splitIndex);
s3Object.objectName = path.substring(splitIndex + 1);
// 处理s3://bucket/object格式
if (path.startsWith("s3://")) {
String[] parts = path.substring(5).split("/", 2);
if (parts.length >= 2) {
s3Object.bucketName = parts[0];
s3Object.objectName = parts[1];
} else {
s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = parts[0];
}
} else if (path.startsWith("hot-sync/")) {
// 处理hot-sync前缀的路径(兼容旧格式)
s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = path.substring("hot-sync/".length()); // 去掉hot-sync/前缀
} else {
// 如果没有分隔符,使用默认bucket
s3Object.bucketName = "default-bucket";
s3Object.objectName = path;
// 处理相对路径或无前缀路径
int splitIndex = path.lastIndexOf(CommonConstant.PATH_SPLIT);
if (splitIndex != -1) {
s3Object.bucketName = path.substring(0, splitIndex);
s3Object.objectName = path.substring(splitIndex + 1);
} else {
// 如果没有分隔符,使用配置的bucket
s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = path;
}
}
}
return s3Object;