You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.7 KiB
45 lines
1.7 KiB
import os |
|
from auto_sliceing_operate.utils.oss_redis import ossClient |
|
from auto_sliceing_operate.utils.logs import log |
|
|
|
# 从 OSS 下载文件到本地,并做完整性校验(状态码 + 文件大小) |
|
def download_file_with_check(ossFilePath, localFilePath): |
|
try: |
|
log(f"开始从 OSS 下载文件: {ossFilePath} -> {localFilePath}") |
|
if not checkFileExists(ossFilePath): |
|
log(f"文件不存在: {ossFilePath}") |
|
return False |
|
# 同步阻塞下载,下载完成后才会返回 |
|
result = ossClient().get_object_to_file(ossFilePath, localFilePath) |
|
|
|
# 1. 检查 HTTP 状态码 |
|
status = getattr(result, "status", None) |
|
if status != 200: |
|
log(f"下载失败,HTTP 状态码异常: status={status}") |
|
return False |
|
|
|
# 2. 远端 / 本地文件大小对比,作为二次校验 |
|
remote_meta = ossClient().head_object(ossFilePath) |
|
remote_size = getattr(remote_meta, "content_length", None) |
|
if remote_size is None: |
|
log("无法获取远端文件大小,放弃本次下载结果") |
|
return False |
|
|
|
if not os.path.exists(localFilePath): |
|
log("本地文件不存在,下载可能失败") |
|
return False |
|
|
|
local_size = os.path.getsize(localFilePath) |
|
if remote_size != local_size: |
|
log(f"文件大小不一致,下载可能不完整: remote={remote_size}, local={local_size}") |
|
return False |
|
|
|
log("文件下载成功且完整性校验通过") |
|
return True |
|
except Exception as e: |
|
log(f"下载文件出现异常: {str(e)}") |
|
return False |
|
|
|
|
|
def checkFileExists(ossFilePath): |
|
return ossClient().object_exists(ossFilePath) |