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.

114 lines
4.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# pip install esdk-obs-python --trusted-host pypi.org
# pip install tqdm
# python.exe -m pip install --upgrade pip
# 引入模块
import os
from datetime import datetime
from obs import ObsClient
import time
from tqdm import tqdm
import requests
import urllib.parse
# 替换为你的华为云AK/SK
ak = "WAFBGJACKDOQZDH1MKZ1"
sk = "dlWTUbqgCICaYJG3n0Rot4jXaen2HnfFtMVxiPEo"
server = "https://obs.cn-north-1.myhuaweicloud.com"
bucketName = "dsideal"
prefix = "HuangHai/HuiZhiKeJi/"
localPath = "D:/课程备份/"
def getTime():
# 获取当前时间
now = datetime.now()
# 格式化到秒
current_time_to_seconds = now.strftime("%H:%M:%S")
return current_time_to_seconds
# 执行一次
def solve():
# 创建obsClient实例
obsClient = ObsClient(access_key_id=ak, secret_access_key=sk, server=server)
# 使用访问OBS
max_keys = 1000
mark = None
resp = None
while True:
fileList = obsClient.listObjects(bucketName=bucketName, prefix=prefix, max_keys=max_keys, marker=mark)
for file in fileList.body.contents:
fullName = file.key.replace(prefix, "")
array = fullName.split("/")
fileName = array[-1]
dirStr = ''
for i in range(len(array) - 1):
dirStr += array[i] + "/"
if dirStr == '' or fileName == '':
continue
localDirStr = localPath + dirStr
if not os.path.isdir(localDirStr):
os.makedirs(localDirStr, exist_ok=True)
downloadPath = localDirStr + fileName
# 下载对象
resp = obsClient.getObject(bucketName=bucketName, objectKey=file.key)
# 如果已存在,但是,与云存储中文件大小不一样大,那么删除掉
if os.path.isfile(downloadPath):
localFileSize = os.path.getsize(downloadPath)
if file.size != localFileSize:
os.remove(downloadPath)
print(getTime() + " " + "发现文件大小不一致,删除本地文件:" + downloadPath)
if not os.path.isfile(downloadPath):
print(
getTime() + " 下载文件:" + fileName + ",文件大小:" + str(
"{:.2f}".format(file.size / 1024 / 1024)) + "MB")
# 发送HTTP GET请求
# 对URL进行编码
url_to_encode = dirStr + fileName
encoded_url = urllib.parse.quote(url_to_encode, safe='')
url = "https://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/HuiZhiKeJi/" + encoded_url
response = requests.get(url, stream=True)
# 获取文件总大小
total_size_in_bytes = int(response.headers.get('content-length', 0))
# 创建一个进度条
progress_bar = tqdm(total=total_size_in_bytes, unit='iB', unit_scale=True)
# 定义一个函数来处理数据块
def callback_chunk(chunk):
progress_bar.update(len(chunk))
# 打开一个本地文件用于写入
with open(downloadPath, 'wb') as file:
# 迭代响应内容
for chunk in response.iter_content(chunk_size=1024):
file.write(chunk)
callback_chunk(chunk) # 更新进度条
# 关闭进度条
progress_bar.close()
else:
print(
getTime() + " 相同的文件:" + fileName + ",文件大小:" + str(
"{:.2f}".format(file.size / 1024 / 1024)) + "MB跳过...")
# 是否已经完成了所有任务
if resp != None and resp.body.is_truncated is True:
mark = resp.body.next_marker
else:
break
# 关闭obsClient
obsClient.close()
print(getTime() + " 同步完成")
if __name__ == '__main__':
while True:
solve()
time.sleep(3600 * 2) # 每2小时执行一次同步操作