fix(fam-edge): 整视频上传方式修正 - Gemini 改 resumable 可续传协议(370MB 不再 raw 单发超时);NVIDIA 改 Assets API 上传拿 asset_id(video_url 引用),规避 25MB payload 上限
This commit is contained in:
@@ -122,24 +122,52 @@ class GeminiAdapter(BaseModelAdapter):
|
||||
self._delete_file(file_uri)
|
||||
|
||||
def _upload_file(self, video_path: str) -> Optional[str]:
|
||||
"""用 Files API 上传完整视频,返回可引用 URI。"""
|
||||
"""用 Files API resumable 可续传协议上传完整视频,返回可引用 URI。"""
|
||||
name = os.path.basename(video_path)
|
||||
upload_url = f"{self._base_url}/files?key={self.api_key}"
|
||||
size = os.path.getsize(video_path)
|
||||
upload_timeout = max(self.timeout, 900)
|
||||
base = f"{self._base_url}/files?key={self.api_key}"
|
||||
# 1) 创建可续传上传会话
|
||||
try:
|
||||
r0 = requests.post(
|
||||
base,
|
||||
headers={
|
||||
"X-Goog-Upload-Protocol": "resumable",
|
||||
"X-Goog-Upload-Command": "start",
|
||||
"X-Goog-Upload-Header-Content-Length": str(size),
|
||||
"X-Goog-Upload-Header-Content-Type": "video/mp4",
|
||||
"X-Goog-Upload-File-Name": name,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
json={"file": {"display_name": name}},
|
||||
timeout=60,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Gemini 创建上传会话异常: {e}")
|
||||
return None
|
||||
if r0.status_code not in (200, 201):
|
||||
logger.warning(f"Gemini 创建上传会话失败 HTTP {r0.status_code}: {r0.text[:200]}")
|
||||
return None
|
||||
session_url = r0.headers.get('X-Goog-Upload-URL')
|
||||
if not session_url:
|
||||
logger.warning("Gemini 上传响应缺少 X-Goog-Upload-URL")
|
||||
return None
|
||||
# 2) 上传文件体(流式)
|
||||
try:
|
||||
with open(video_path, 'rb') as f:
|
||||
data = f.read()
|
||||
except OSError as e:
|
||||
logger.error(f"读取视频失败 {video_path}: {e}")
|
||||
return None
|
||||
headers = {
|
||||
"X-Goog-Upload-Protocol": "raw",
|
||||
"X-Goog-Upload-File-Name": name,
|
||||
"Content-Type": "video/mp4",
|
||||
}
|
||||
try:
|
||||
resp = requests.post(upload_url, headers=headers, data=data, timeout=300)
|
||||
resp = requests.post(
|
||||
session_url,
|
||||
headers={
|
||||
"Content-Length": str(size),
|
||||
"X-Goog-Upload-Command": "upload, finalize",
|
||||
"X-Goog-Upload-Offset": "0",
|
||||
"Content-Type": "video/mp4",
|
||||
},
|
||||
data=f,
|
||||
timeout=upload_timeout,
|
||||
)
|
||||
except requests.Timeout:
|
||||
logger.warning("Gemini 文件上传超时 (300s)")
|
||||
logger.warning(f"Gemini 文件上传超时 ({upload_timeout}s)")
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.error(f"Gemini 文件上传异常: {e}")
|
||||
|
||||
Reference in New Issue
Block a user