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.
 
 
 
 
 
 

139 lines
5.5 KiB

# 引入模块
import os
import traceback
import sys
# https://panduola-cdn-xcx-dev.yingxiong.com/2h/StreamingAssets/main/pm_main.version
from obs import ObsClient, CreateBucketHeader, PutObjectHeader, GetObjectHeader
from obs import ObsClient, CreateBucketHeader, PutObjectHeader, GetObjectHeader
from huaweicloudsdkcore.auth.credentials import GlobalCredentials
from huaweicloudsdkcdn.v1.region.cdn_region import CdnRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkcdn.v1 import *
def upload_file(bucketName, objectKey, file_path):
try:
# 上传对象的附加头域
headers = PutObjectHeader()
# 【可选】待上传对象的MIME类型
headers.contentType = 'text/plain'
# bucketName = "panduola-dev"
# # 对象名,即上传后的文件名
# objectKey = "gjn/a.txt"
# # 待上传文件/文件夹的完整路径,如aa/bb.txt,或aa/
# file_path = 'D:/Crsh.txt'
# 上传文件的自定义元数据
print(f"To Cdn path is {objectKey}")
print('Local path is \'{}\','.format(file_path))
resp = obsClient.putFile(bucketName, objectKey, file_path, None, headers)
# 返回码为2xx时,接口调用成功,否则接口调用失败
if resp.status < 300:
# print('')
return
else:
print('Put File Failed')
print('requestId:', resp.requestId)
print('errorCode:', resp.errorCode)
print('errorMessage:', resp.errorMessage)
except:
print('Put File Failed')
print(traceback.format_exc())
CdnMainPath = "2h/{}"
CdnSubPath = "2h/StreamingAssets/main/{}"
def list_files(startpath, mdict):
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":
upload_file("panduola-dev", CdnSubPath.format(f), os.path.join(root, f))
continue
if ".bytes" in f:
upload_file("panduola-dev", CdnSubPath.format(f), os.path.join(root, f))
continue
if mdict.get(f):
continue
upload_file("panduola-dev", CdnMainPath.format(f), os.path.join(root, f))
# 推荐通过环境变量获取AKSK,这里也可以使用其他外部引入方式传入,如果使用硬编码可能会存在泄露风险。
# 您可以登录访问管理控制台获取访问密钥AK/SK,获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html。
ak = "ZGTHNNBI7EZZZXVBXW9A"
sk = "z7fz3UFjFYCOhpVZFtMPafyLY7prLfeME4Kkk56f"
# 【可选】如果使用临时AKSK和SecurityToken访问OBS,则同样推荐通过环境变量获取
# server填写Bucket对应的Endpoint, 这里以华北-北京四为例,其他地区请按实际情况填写。
server = "https://obs.cn-north-4.myhuaweicloud.com"
# 创建obsClient实例
# 如果使用临时AKSK和SecurityToken访问OBS,需要在创建实例时通过security_token参数指定securityToken值
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}'
obsClient = ObsClient(access_key_id=ak, secret_access_key=sk, server=server)
CdnMainPath = f"{cdnPath}/{{}}"
CdnSubPath = f"{cdnPath}/StreamingAssets/main/{{}}"
log(f"folder is {folder}")
list_files(folder, my_dict)
log(f"upload done")
obsClient.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}")