|
|
import Util.OssUtil
|
|
|
|
|
|
import os
|
|
|
from Util.CommonUtil import *
|
|
|
import sys
|
|
|
|
|
|
|
|
|
def percentage(consumed_bytes, total_bytes):
|
|
|
# 当HTTP响应头部没有Content-Length时,total_bytes的值为None。
|
|
|
if total_bytes:
|
|
|
rate = int(100 * (float(consumed_bytes) / float(total_bytes)))
|
|
|
print('\r{0}% '.format(rate), end='')
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
|
backupPath = "D:/backup"
|
|
|
if not os.path.exists(backupPath):
|
|
|
os.makedirs(backupPath)
|
|
|
|
|
|
# 设置前缀和分页参数
|
|
|
prefix = 'Images/System'
|
|
|
max_keys = 20 # 每页显示的最大文件数量
|
|
|
|
|
|
auth = Util.OssUtil.oss2.Auth(Util.OssUtil.accessKeyId, Util.OssUtil.accessKeySecret)
|
|
|
bucket = Util.OssUtil.oss2.Bucket(auth, Util.OssUtil.endpoint, Util.OssUtil.bucketName)
|
|
|
|
|
|
# 如果你想要手动控制分页,可以使用marker参数
|
|
|
marker = ''
|
|
|
is_truncated = True
|
|
|
|
|
|
# 遍历文件
|
|
|
while is_truncated:
|
|
|
try:
|
|
|
# 使用list_objects方法获取对象列表
|
|
|
result = Util.OssUtil.oss2.ObjectIterator(bucket, prefix=prefix, marker=marker, max_keys=max_keys)
|
|
|
|
|
|
for obj in result:
|
|
|
if obj.key.count('/') < 3:
|
|
|
continue
|
|
|
array = obj.key.split('/')
|
|
|
if not os.path.exists(backupPath + '/' + array[0] + '/' + array[1] + '/' + array[2]):
|
|
|
os.makedirs(backupPath + '/' + array[0] + '/' + array[1] + '/' + array[2])
|
|
|
local_file_path = backupPath + '/' + obj.key
|
|
|
if not os.path.exists(local_file_path):
|
|
|
# 使用get_object_to_file方法下载文件
|
|
|
bucket.get_object_to_file(obj.key, local_file_path, progress_callback=percentage)
|
|
|
printf(f'文件 {obj.key} 已下载到 {local_file_path}')
|
|
|
|
|
|
# 使用os.path.getsize()获取文件大小
|
|
|
file_size = os.path.getsize(local_file_path)
|
|
|
# 检查文件是否为0字节
|
|
|
if file_size == 0:
|
|
|
printf(f'文件 {obj.key} 为0字节,下载错误!!!!')
|
|
|
exit(0)
|
|
|
|
|
|
# 更新marker为下一页的起始点
|
|
|
marker = result.next_marker
|
|
|
|
|
|
# 检查是否还有更多的文件
|
|
|
is_truncated = result.is_truncated
|
|
|
|
|
|
except Exception as e:
|
|
|
# 处理异常情况
|
|
|
printf(e)
|
|
|
|
|
|
printf("恭喜,所有文件备份成功完成!")
|
|
|
# http://hzkc.oss-cn-beijing.aliyuncs.com/Images/System/4/20240514164847051.png
|