fix: 修复S3 URL转换问题 - 添加CDN URL转换功能

问题修复:
- 火山云API不支持S3协议URL (s3://...),需要HTTPS URL
- 云上传服务返回S3 URI格式,需要转换为可访问的CDN URL

技术实现:
- 添加convert_s3_to_cdn_url方法,将S3 URL转换为CDN URL
- 支持s3://ap-northeast-2/modal-media-cache/格式转换为https://cdn.roasmax.cn/
- 支持通用s3://格式和amazonaws.com格式的URL转换
- 在文件上传后立即转换URL格式,确保API调用使用正确的HTTPS URL

修复的错误:
- 400 Bad Request: unsupported protocol scheme 's3'
- 错误原因: 使用了S3协议URL而非HTTPS URL
- 解决方案: 自动转换S3 URL为可访问的CDN HTTPS URL

URL转换示例:
- s3://ap-northeast-2/modal-media-cache/upload/... -> https://cdn.roasmax.cn/upload/...
- 确保火山云API能够正确访问图片和视频文件
This commit is contained in:
imeepos 2025-07-31 13:11:08 +08:00
parent 2d9e6f067d
commit 65412e80b7
1 changed files with 32 additions and 2 deletions

View File

@ -230,9 +230,13 @@ impl VolcanoVideoService {
image_upload_result.error_message.unwrap_or_default()));
}
let image_url = image_upload_result.remote_url
let image_s3_url = image_upload_result.remote_url
.ok_or_else(|| anyhow!("图片上传成功但未返回URL"))?;
// 将S3 URL转换为HTTPS CDN URL
let image_url = Self::convert_s3_to_cdn_url(&image_s3_url);
info!("图片S3 URL: {} -> CDN URL: {}", image_s3_url, image_url);
// 上传驱动视频到云端
info!("正在上传驱动视频到云端: {}", driving_video_path);
let video_upload_result = self.cloud_upload_service
@ -244,9 +248,13 @@ impl VolcanoVideoService {
video_upload_result.error_message.unwrap_or_default()));
}
let driving_video_url = video_upload_result.remote_url
let driving_video_s3_url = video_upload_result.remote_url
.ok_or_else(|| anyhow!("驱动视频上传成功但未返回URL"))?;
// 将S3 URL转换为HTTPS CDN URL
let driving_video_url = Self::convert_s3_to_cdn_url(&driving_video_s3_url);
info!("驱动视频S3 URL: {} -> CDN URL: {}", driving_video_s3_url, driving_video_url);
info!("文件上传完成 - 图片: {}, 驱动视频: {}", image_url, driving_video_url);
let request_body = VolcanoVideoGenerationRequest {
@ -566,6 +574,28 @@ impl VolcanoVideoService {
Ok(hex::encode(signature))
}
/// 将S3 URL转换为可访问的CDN地址
fn convert_s3_to_cdn_url(s3_url: &str) -> String {
if s3_url.starts_with("s3://ap-northeast-2/modal-media-cache/") {
// 将 s3://ap-northeast-2/modal-media-cache/ 替换为 https://cdn.roasmax.cn/
s3_url.replace("s3://ap-northeast-2/modal-media-cache/", "https://cdn.roasmax.cn/")
} else if s3_url.starts_with("s3://") {
// 处理其他 s3:// 格式转换为通用CDN格式
s3_url.replace("s3://", "https://cdn.roasmax.cn/")
} else if s3_url.contains("amazonaws.com") {
// 如果是完整的S3 HTTPS URL提取key部分
if let Some(key_start) = s3_url.find(".com/") {
let key = &s3_url[key_start + 5..];
format!("https://cdn.roasmax.cn/{}", key)
} else {
s3_url.to_string()
}
} else {
// 如果不是预期的S3格式返回原URL
s3_url.to_string()
}
}
}
// 实现Clone trait以支持异步任务