老汉憨憨
发布于 2024-03-02 / 32 阅读
0
1

python脚本调用华为云api刷新cdn缓存

华为云api创建刷新缓存任务

  1. 安装python相关模块

pip3 install huaweicloudsdkcore
pip3 install huaweicloudsdkcdn

  1. 脚本如下

# coding: utf-8

import sys
from huaweicloudsdkcore.auth.credentials import GlobalCredentials
from huaweicloudsdkcdn.v1.region.cdn_region import CdnRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkcdn.v1 import *


def refreshTask(url, sub_dir):
    ak = "**********"
    sk = "****************"
    project_id= "*************"

    credentials = GlobalCredentials(ak, sk ,project_id) \

    client = CdnClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(CdnRegion.value_of("cn-north-1")) \
        .build()

    tmp_url_http = "http://%s/%s/" % (url, sub_dir)
    tmp_url_https = "https://%s/%s/" % (url, sub_dir)

    try:
        request = CreateRefreshTasksRequest()
        listUrlsRefreshTask = [
            tmp_url_http,
            tmp_url_https
        ]
        refreshTaskbody = RefreshTaskRequestBody(
            type="directory",
            urls=listUrlsRefreshTask
        )
        request.body = RefreshTaskRequest(
            refresh_task=refreshTaskbody
        )
        response = client.create_refresh_tasks(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)


if __name__ == "__main__":
    refreshTask("your-cdn-url",sys.argv[1])

3.备注

  • ak&sk,project_id分别为账号的密钥对、项目id


评论