标签:文件 search 预热 cdn urls import resp
在腾讯云cdn中预热一个文件夹下所有的文件,并对预热失败的文件进行重新预热,直到所有文件都预热完成.
Show you the code:
import json
import os
import time
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.cdn.v20180606 import cdn_client, models
from jproperties import Properties
#配置文件路径,主要用于读取版本
config_path=r"D:/Code/IDEA/Xinxuan-company/src/main/resources/project.properties"
#是否带有版本号
hasVersion=False
#需要预热的文件夹路径(包括子文件夹的文件)
path =r"D:/Code/IDEA/Xinxuan-company/src/main/resources/static/views/custom/"
cdn_root="https://xinxuan-credit-model-test.file.myqcloud.com/cm/views/test"
secretId="你的secretID"
secretKey="你的secretKey"
configs = Properties()
with open(config_path, 'rb') as config_file:
configs.load(config_file)
version=configs.get("project_version")
cred = credential.Credential(secretId, secretKey)
httpProfile = HttpProfile()
httpProfile.endpoint = "cdn.tencentcloudapi.com"
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = cdn_client.CdnClient(cred, "", clientProfile)
def prewarm_urls(urls):
req = models.PushUrlsCacheRequest()
params = {
"Urls": urls,
"Area": "global"
}
req.from_json_string(json.dumps(params))
resp = client.PushUrlsCache(req)
print("sent pre-warm request: "+resp.to_json_string())
return resp
def search_urls(taskId,status):
req = models.DescribePushTasksRequest()
params = {
"TaskId": taskId,
"Status": status
}
req.from_json_string(json.dumps(params))
resp = client.DescribePushTasks(req)
print(status+":"+str(len(resp.PushLogs)))
return get_urls_from_logs(resp.PushLogs)
def get_urls_from_logs(logs):
urls=[]
for log in logs:
urls.append(log.Url)
return urls
def search_process_urls(taskId):
return search_urls(taskId,"process")
def search_fail_urls(taskId):
return search_urls(taskId,"fail")
def generate_urls():
urls=[]
for root, dirs, files in os.walk(path):
root=root.replace("\\","/")
for file in files:
#append the file name to the list
subpath=root.replace(path,'')+"/"+file
if hasVersion:
subpath=subpath+"?"+version.data
subpath=subpath.replace("\\","/")
urls.append(cdn_root+"/"+subpath)
return urls
#no more than 20 urls
def make_sure_prewarm(urls):
try:
resp=prewarm_urls(urls)
while True:
time.sleep(10)
processurls=search_process_urls(resp.TaskId)
if len(processurls)!=0:
continue
failurls=search_fail_urls(resp.TaskId)
if len(failurls)==0:
break
resp=prewarm_urls(failurls)
except TencentCloudSDKException as err:
print(err)
urls=generate_urls()
index=0
#腾讯限制次最多提交20个urls
step=20
while True:
part=urls[index:index+step]
index=index+step
if( len(part)==0):
break
make_sure_prewarm(part)
标签:文件,search,预热,cdn,urls,import,resp 来源: https://blog.csdn.net/oh_futrue/article/details/111109282
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。