fix(文件中心):修复冷数据访问问题彻底修复,携程回调流程数据解析完成

This commit is contained in:
2025-08-04 00:38:16 +08:00
parent 537b13e60e
commit 86b0158710
12 changed files with 788 additions and 28 deletions
@@ -6,6 +6,7 @@ import com.central.common.model.Result;
import com.central.file.service.IFileService;
import com.central.file.service.StorageHealthCheckService;
import com.central.file.service.TimeWindowStorageService;
import com.central.file.service.OssToS3SyncService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
@@ -42,6 +43,9 @@ public class FileController {
@Autowired
private StorageHealthCheckService storageHealthCheckService;
@Autowired(required = false)
private OssToS3SyncService ossToS3SyncService;
@Value("${zlt.security.tencent.accessTokenUrl}")
private String accessTokenUrl;
@@ -243,4 +247,133 @@ public class FileController {
public Result<String> getStorageHealth() {
return Result.succeed(storageHealthCheckService.getStorageStatus(), "获取成功");
}
/**
* 获取智能存储文件URL
*/
@GetMapping("/storage/smart-url/{id}")
public Result<Map<String, Object>> getSmartStorageUrl(@PathVariable String id) {
Map<String, Object> result = new HashMap<>();
try {
String smartUrl = timeWindowStorageService.getSmartViewUrl(id);
result.put("fileId", id);
result.put("smartUrl", smartUrl);
result.put("success", !smartUrl.isEmpty());
result.put("hasUrl", smartUrl != null && !smartUrl.isEmpty());
return Result.succeed(result);
} catch (Exception e) {
log.error("获取智能存储URL失败: {}", id, e);
result.put("fileId", id);
result.put("error", e.getMessage());
result.put("success", false);
return Result.failed("获取智能存储URL失败");
}
}
/**
* 根据路径获取智能存储文件URL
*/
@GetMapping("/storage/smart-url-by-path")
public Result<Map<String, Object>> getSmartStorageUrlByPath(@RequestParam String path) {
Map<String, Object> result = new HashMap<>();
try {
String smartUrl = timeWindowStorageService.getSmartViewUrlByPath(path);
result.put("path", path);
result.put("smartUrl", smartUrl);
result.put("success", !smartUrl.isEmpty());
result.put("hasUrl", smartUrl != null && !smartUrl.isEmpty());
return Result.succeed(result);
} catch (Exception e) {
log.error("获取智能存储URL失败: path={}", path, e);
result.put("path", path);
result.put("error", e.getMessage());
result.put("success", false);
return Result.failed("获取智能存储URL失败");
}
}
/**
* 测试S3文件访问
*/
@GetMapping("/storage/test-s3-access")
public Result<Map<String, Object>> testS3Access(@RequestParam String path) {
Map<String, Object> result = new HashMap<>();
try {
// 使用TimeWindowStorageService来测试S3访问
String smartUrl = timeWindowStorageService.getSmartViewUrlByPath(path);
result.put("path", path);
result.put("smartUrl", smartUrl);
result.put("success", !smartUrl.isEmpty());
result.put("hasUrl", smartUrl != null && !smartUrl.isEmpty());
return Result.succeed(result);
} catch (Exception e) {
log.error("测试S3访问失败: path={}", path, e);
result.put("path", path);
result.put("error", e.getMessage());
result.put("success", false);
return Result.failed("测试S3访问失败");
}
}
/**
* 手动同步OSS文件到S3
*/
@PostMapping("/storage/sync-oss-to-s3")
public Result<Map<String, Object>> syncOssToS3(@RequestParam String path) {
Map<String, Object> result = new HashMap<>();
try {
if (ossToS3SyncService == null) {
result.put("path", path);
result.put("error", "OSS到S3同步服务未配置");
result.put("success", false);
return Result.failed("OSS到S3同步服务未配置");
}
boolean syncResult = ossToS3SyncService.syncFileToS3(path);
result.put("path", path);
result.put("syncResult", syncResult);
result.put("success", syncResult);
if (syncResult) {
log.info("手动同步OSS到S3成功: path={}", path);
return Result.succeed(result, "同步成功");
} else {
log.error("手动同步OSS到S3失败: path={}", path);
return Result.failed("同步失败");
}
} catch (Exception e) {
log.error("手动同步OSS到S3异常: path={}", path, e);
result.put("path", path);
result.put("error", e.getMessage());
result.put("success", false);
return Result.failed("同步异常:" + e.getMessage());
}
}
/**
* 测试OSS文件访问
*/
@GetMapping("/storage/test-oss-access")
public Result<Map<String, Object>> testOssAccess(@RequestParam String path) {
Map<String, Object> result = new HashMap<>();
try {
// 使用现有的fileService来测试OSS访问
String ossUrl = fileService.getViewUrlByPath(path);
result.put("path", path);
result.put("ossUrl", ossUrl);
result.put("success", ossUrl != null && !ossUrl.isEmpty());
result.put("hasUrl", ossUrl != null && !ossUrl.isEmpty());
return Result.succeed(result);
} catch (Exception e) {
log.error("测试OSS访问失败: path={}", path, e);
result.put("path", path);
result.put("error", e.getMessage());
result.put("success", false);
return Result.failed("测试OSS访问失败");
}
}
}
@@ -0,0 +1,144 @@
package com.central.file.service;
import com.central.common.model.FileInfo;
import com.central.file.service.impl.EnhancedS3Service;
import com.central.file.service.impl.OssService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* OSS到S3同步服务
* 用于将OSS上的文件同步到S3存储
*/
@Slf4j
@Service
public class OssToS3SyncService {
@Autowired
private EnhancedS3Service enhancedS3Service;
@Autowired
private OssService ossService;
/**
* 同步OSS文件到S3
*
* @param ossUrl OSS文件URL
* @param s3ObjectName S3对象名称
* @return 是否同步成功
*/
public boolean syncOssToS3(String ossUrl, String s3ObjectName) {
try {
log.info("开始同步OSS文件到S3: {} -> {}", ossUrl, s3ObjectName);
// 检查S3中是否已存在
if (enhancedS3Service.fileExists(s3ObjectName)) {
log.info("S3中已存在文件,跳过同步: {}", s3ObjectName);
return true;
}
// 从OSS下载文件
InputStream inputStream = downloadFromOss(ossUrl);
if (inputStream == null) {
log.error("从OSS下载文件失败: {}", ossUrl);
return false;
}
// 使用现有的downloadAndUpload方法
boolean uploadResult = enhancedS3Service.downloadAndUpload(ossUrl, s3ObjectName);
if (uploadResult) {
log.info("OSS文件同步到S3成功: {} -> {}", ossUrl, s3ObjectName);
} else {
log.error("OSS文件同步到S3失败: {} -> {}", ossUrl, s3ObjectName);
}
return uploadResult;
} catch (Exception e) {
log.error("同步OSS文件到S3异常: {} -> {}", ossUrl, s3ObjectName, e);
return false;
}
}
/**
* 根据文件信息同步到S3
*
* @param fileInfo 文件信息
* @return 是否同步成功
*/
public boolean syncFileToS3(FileInfo fileInfo) {
if (fileInfo == null || fileInfo.getPath() == null) {
log.error("文件信息为空,无法同步");
return false;
}
// 使用OSS服务获取带签名的URL
String ossUrl = ossService.getViewUrlByPath(fileInfo.getPath());
if (ossUrl == null || ossUrl.isEmpty()) {
log.error("无法获取OSS文件URL: {}", fileInfo.getPath());
return false;
}
String s3ObjectName = fileInfo.getPath();
return syncOssToS3(ossUrl, s3ObjectName);
}
/**
* 根据文件路径同步到S3
*
* @param filePath 文件路径
* @return 是否同步成功
*/
public boolean syncFileToS3(String filePath) {
if (filePath == null || filePath.isEmpty()) {
log.error("文件路径为空,无法同步");
return false;
}
// 使用OSS服务获取带签名的URL
String ossUrl = ossService.getViewUrlByPath(filePath);
if (ossUrl == null || ossUrl.isEmpty()) {
log.error("无法获取OSS文件URL: {}", filePath);
return false;
}
String s3ObjectName = filePath;
return syncOssToS3(ossUrl, s3ObjectName);
}
/**
* 从OSS下载文件
*
* @param ossUrl OSS文件URL
* @return 文件输入流
*/
private InputStream downloadFromOss(String ossUrl) {
try {
log.debug("从OSS下载文件: {}", ossUrl);
URL url = new URL(ossUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(30000);
connection.setReadTimeout(30000);
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
log.debug("OSS文件下载成功: {}", ossUrl);
return connection.getInputStream();
} else {
log.error("OSS文件下载失败,响应码: {}, URL: {}", responseCode, ossUrl);
return null;
}
} catch (Exception e) {
log.error("从OSS下载文件异常: {}", ossUrl, e);
return null;
}
}
}
@@ -3,6 +3,7 @@ package com.central.file.service;
import com.central.common.model.FileInfo;
import com.central.file.service.impl.OssService;
import com.central.file.service.impl.S3Service;
import com.central.file.service.impl.EnhancedS3Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -31,6 +32,13 @@ public class TimeWindowStorageService {
@Qualifier("s3FileService")
private S3Service s3Service;
@Autowired(required = false)
@Qualifier("enhancedS3FileService")
private EnhancedS3Service enhancedS3Service;
@Autowired(required = false)
private OssToS3SyncService ossToS3SyncService;
// 配置参数
@Value("${zlt.storage.hot-data-days:30}")
private int hotDataDays; // 热数据保留天数,默认30天
@@ -60,7 +68,7 @@ public class TimeWindowStorageService {
// 3. 根据存储类型获取URL
String viewUrl = getViewUrlByStorageType(fileInfo, storageType);
log.debug("智能存储查询成功: fileId={}, storageType={}, url={}",
log.info("智能存储查询成功: fileId={}, storageType={}, url={}",
fileId, storageType, viewUrl);
return viewUrl;
@@ -134,10 +142,15 @@ public class TimeWindowStorageService {
* 根据存储类型获取访问URL
*/
private String getViewUrlByStorageType(FileInfo fileInfo, String storageType) {
log.info("根据存储类型获取URL: fileId={}, storageType={}, path={}",
fileInfo.getId(), storageType, fileInfo.getPath());
if ("S3".equals(storageType)) {
// 冷数据,优先从S3查询
log.info("冷数据,优先从S3查询: fileId={}", fileInfo.getId());
String s3Url = queryFromS3(fileInfo);
if (s3Url != null && !s3Url.isEmpty()) {
log.info("S3查询成功: fileId={}, url={}", fileInfo.getId(), s3Url);
return s3Url;
}
// S3查询失败,降级到OSS
@@ -145,8 +158,10 @@ public class TimeWindowStorageService {
return queryFromOss(fileInfo);
} else {
// 热数据,优先从OSS查询
log.info("热数据,优先从OSS查询: fileId={}", fileInfo.getId());
String ossUrl = queryFromOss(fileInfo);
if (ossUrl != null && !ossUrl.isEmpty()) {
log.info("OSS查询成功: fileId={}, url={}", fileInfo.getId(), ossUrl);
return ossUrl;
}
// OSS查询失败,降级到S3
@@ -172,10 +187,15 @@ public class TimeWindowStorageService {
*/
private String queryFromS3(FileInfo fileInfo) {
try {
if (s3Service != null) {
// 优先使用EnhancedS3Service
if (enhancedS3Service != null) {
log.debug("使用EnhancedS3Service查询: fileId={}, path={}", fileInfo.getId(), fileInfo.getPath());
return enhancedS3Service.getViewUrlByPath(fileInfo.getPath());
} else if (s3Service != null) {
log.debug("使用S3Service查询: fileId={}, path={}", fileInfo.getId(), fileInfo.getPath());
return s3Service.getViewUrlByPath(fileInfo.getPath());
} else {
log.warn("S3Service未配置,无法查询S3: fileId={}", fileInfo.getId());
log.warn("S3服务未配置,无法查询S3: fileId={}", fileInfo.getId());
return "";
}
} catch (Exception e) {
@@ -189,10 +209,29 @@ public class TimeWindowStorageService {
*/
private String queryFromS3ByPath(String filePath) {
try {
if (s3Service != null) {
// 优先使用EnhancedS3Service
if (enhancedS3Service != null) {
log.debug("使用EnhancedS3Service查询: path={}", filePath);
String s3Url = enhancedS3Service.getViewUrlByPath(filePath);
// 如果S3中没有找到文件,尝试从OSS同步
if ((s3Url == null || s3Url.isEmpty()) && ossToS3SyncService != null) {
log.info("S3中未找到文件,尝试从OSS同步: path={}", filePath);
boolean syncResult = ossToS3SyncService.syncFileToS3(filePath);
if (syncResult) {
log.info("OSS同步到S3成功,重新获取URL: path={}", filePath);
s3Url = enhancedS3Service.getViewUrlByPath(filePath);
} else {
log.warn("OSS同步到S3失败: path={}", filePath);
}
}
return s3Url;
} else if (s3Service != null) {
log.debug("使用S3Service查询: path={}", filePath);
return s3Service.getViewUrlByPath(filePath);
} else {
log.warn("S3Service未配置,无法查询S3: path={}", filePath);
log.warn("S3服务未配置,无法查询S3: path={}", filePath);
return "";
}
} catch (Exception e) {
@@ -241,8 +280,9 @@ public class TimeWindowStorageService {
path.startsWith("cold-storage") ||
path.contains("S3") ||
path.startsWith("hot-sync/") || // 兼容旧的hot-sync前缀
// 对于没有前缀的路径,如果数据库中的storageType是S3,也认为是S3路径
(!path.contains("/") && !path.startsWith("http"))
// 只有明确包含S3标识的路径才认为是S3路径
path.contains("s3://") ||
path.contains("minio://")
);
}
@@ -251,10 +291,11 @@ public class TimeWindowStorageService {
*/
private boolean isOssPath(String path) {
return path != null && (
path.contains("oss://") ||
path.startsWith("oss://") ||
path.contains("aliyun") ||
path.contains("ali-oss") ||
path.contains("OSS")
path.contains("OSS") ||
path.startsWith("https://goflyapp.oss-cn-beijing.aliyuncs.com/")
);
}
@@ -68,11 +68,20 @@ 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);
log.info("解析S3路径: {} -> bucket: {}, object: {}", name, s3Object.bucketName, s3Object.objectName);
// 检查文件是否存在
boolean exists = s3Template.doesObjectExist(s3Object.bucketName, s3Object.objectName);
log.info("S3文件存在检查: bucket={}, object={}, exists={}", s3Object.bucketName, s3Object.objectName, exists);
if (!exists) {
log.warn("S3文件不存在: bucket={}, object={}", s3Object.bucketName, s3Object.objectName);
return "";
}
// 使用getViewUrl方法获取预签名URL
String url = s3Template.getViewUrl(s3Object.bucketName, s3Object.objectName, 60); // 60分钟有效期
log.debug("生成S3 URL: {}", url);
log.info("生成S3 URL: {}", url);
return url;
} catch (Exception e) {
log.error("获取S3文件URL失败: {}", name, e);
@@ -332,28 +341,51 @@ public class EnhancedS3Service extends AbstractIFileService {
String[] parts = path.substring(5).split("/", 2);
if (parts.length >= 2) {
s3Object.bucketName = parts[0];
s3Object.objectName = parts[1];
s3Object.objectName = decodeUrl(parts[1]);
} else {
s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = parts[0];
s3Object.objectName = decodeUrl(parts[0]);
}
} else if (path.startsWith("hot-sync/")) {
// 处理hot-sync前缀的路径(兼容旧格式)
s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = path.substring("hot-sync/".length()); // 去掉hot-sync/前缀
s3Object.objectName = decodeUrl(path.substring("hot-sync/".length())); // 去掉hot-sync/前缀并解码
} else {
// 处理相对路径或无前缀路径
int splitIndex = path.lastIndexOf(CommonConstant.PATH_SPLIT);
if (splitIndex != -1) {
s3Object.bucketName = path.substring(0, splitIndex);
s3Object.objectName = path.substring(splitIndex + 1);
s3Object.objectName = decodeUrl(path.substring(splitIndex + 1));
} else {
// 如果没有分隔符,使用配置的bucket
s3Object.bucketName = fileServerProperties.getS3().getBucketName();
s3Object.objectName = path;
s3Object.objectName = decodeUrl(path);
}
}
}
return s3Object;
}
/**
* URL解码,处理特殊字符
*/
private String decodeUrl(String encodedString) {
try {
if (encodedString == null || encodedString.isEmpty()) {
return encodedString;
}
// 检查是否包含URL编码字符
if (encodedString.contains("%")) {
String decoded = java.net.URLDecoder.decode(encodedString, "UTF-8");
log.debug("URL解码: {} -> {}", encodedString, decoded);
return decoded;
}
return encodedString;
} catch (Exception e) {
log.warn("URL解码失败: {}, 使用原始字符串", encodedString, e);
return encodedString;
}
}
}