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.
 
 
 
 
 
 

165 lines
6.1 KiB

# 引入模块
import os
import traceback
import sys
import tos
# https://panduola-cdn-xcx-dev.yingxiong.com/2h/StreamingAssets/main/pm_main.version
# from huaweicloudsdkcore.auth.credentials import GlobalCredentials
# from huaweicloudsdkcdn.v1.region.cdn_region import CdnRegion
# from huaweicloudsdkcore.exceptions import exceptions
# from huaweicloudsdkcdn.v1 import *
ak = "AKLTOTY0N2JmMDczZTdhNDNlZWE5Y2QyMTVkZmM5ZjU1ZDA"
sk = "TjJFNE5qZ3hNR0ZqWlRJME5EUXpZMkk0T1dRelpHSmlaREptWXpJeU56UQ=="
endpoint = "tos-cn-beijing.volces.com"
region = "cn-beijing"
bucket_name = "huoliqiang"
current_directory = os.getcwd().replace("\\", "/")
def upload_file(bucketName, objectKey, file_path):
try:
print(f"To Cdn path is {objectKey}")
tosClient.put_object_from_file(bucketName, objectKey, file_path)
print(f"文件上传成功: {file_path}")
return True
except tos.exceptions.TosClientError as e:
# Operation failed, catch client error, usually caused by invalid request parameters or network issues
print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
return False
except tos.exceptions.TosServerError as e:
# Operation failed, catch server error, detailed error information can be obtained from the response
print('fail with server error, code: {}'.format(e.code))
# request id can be used to locate specific issues, it is strongly recommended to save it in the log.
print('error with request id: {}'.format(e.request_id))
print('error with message: {}'.format(e.message))
print('error with http code: {}'.format(e.status_code))
print('error with ec: {}'.format(e.ec))
print('error with request url: {}'.format(e.request_url))
return False
except Exception as e:
print('fail with unknown error: {}'.format(e))
return False
CdnMainPath = "2h/{}"
CdnSubPath = "2h/StreamingAssets/main/{}"
def list_files(startpath, mdict):
success_count = 0
fail_count = 0
retry_list = []
sub_retry_list = []
for root, dirs, files in os.walk(startpath):
if 'OutputCache' in dirs:
dirs.remove('OutputCache')
if 'Simulate' in dirs:
dirs.remove('Simulate')
level = root.replace(startpath, '').count(os.sep)
indent = ' ' * 4 * (level)
# print('{}/'.format(os.path.join(startpath, root)))
subindent = ' ' * 4 * (level + 1)
for f in files:
if "version" in f:
if IsUploadAndRefreshVersion == "true":
if upload_file(bucket_name, CdnSubPath.format(f), os.path.join(root, f)):
success_count += 1
else:
fail_count += 1
sub_retry_list.append(f)
continue
if ".bytes" in f:
if upload_file(bucket_name, CdnSubPath.format(f), os.path.join(root, f)):
success_count += 1
else:
fail_count += 1
sub_retry_list.append(f)
continue
if mdict.get(f):
continue
if upload_file(bucket_name, CdnMainPath.format(f), os.path.join(root, f)):
success_count += 1
else:
fail_count += 1
retry_list.append(f)
# 重试上传失败的文件 while !!!
for f in sub_retry_list:
if upload_file(bucket_name, CdnSubPath.format(f), os.path.join(root, f)):
success_count += 1
fail_count -= 1
else:
print(f"重试上传失败: {f}")
sys.exit(1)
for f in retry_list:
if upload_file(bucket_name, CdnMainPath.format(f), os.path.join(root, f)):
success_count += 1
fail_count -= 1
else:
print(f"重试上传失败: {f}")
sys.exit(1)
def log(*str):
print(*str, flush=True)
# 构建版本如jp
Version = ""
BundlePath = ""
IsUploadAndRefreshVersion = "false"
if __name__ == '__main__':
Version = sys.argv[1] # 版本
BundlePath = sys.argv[2] # 资源路径
cdnPath = sys.argv[3] # cdn目录路径
cdnUrl = sys.argv[4] # cdn url
IsUploadAndRefreshVersion = sys.argv[5] #是否同时上传pm_main.version
# 输出版本
log(f"upload assset of version {Version} to {cdnPath}")
my_dict = {}
folder = f'{BundlePath}/Bundles/WebGL/main/{Version}'
tosClient = tos.TosClientV2(ak, sk, endpoint, region)
CdnMainPath = f"{cdnPath}/{{}}"
CdnSubPath = f"{cdnPath}/StreamingAssets/main/{{}}"
log(f"folder is {folder}")
list_files(folder, my_dict)
log(f"upload done")
tosClient.close()
# 如果上传了version文件,执行刷新
log(f"IsUploadAndRefreshVersion is {IsUploadAndRefreshVersion}")
# if IsUploadAndRefreshVersion == "true":
# credentials = GlobalCredentials(ak, sk)
# client = CdnClient.new_builder() \
# .with_credentials(credentials) \
# .with_region(CdnRegion.value_of("cn-north-1")) \
# .build()
#
# try:
# request = CreateRefreshTasksRequest()
# listUrlsRefreshTask = [
# f"{cdnUrl}{cdnPath}/StreamingAssets/main/pm_main.version"
# ]
# refreshTaskbody = RefreshTaskRequestBody(
# type="file",
# mode="all",
# zh_url_encode=False,
# urls=listUrlsRefreshTask
# )
# request.body = RefreshTaskRequest(
# refresh_task=refreshTaskbody
# )
# response = client.create_refresh_tasks(request)
# print(f"RefreshVersion result e.response {response}")
# except exceptions.ClientRequestException as e:
# print(f"RefreshVersion result e.status_code {e.status_code}")
# print(f"RefreshVersion result e.request_id {e.request_id}")
# print(f"RefreshVersion result e.error_code {e.error_code}")
# print(f"RefreshVersion result e.error_msg {e.error_msg}")