阿里云CDN的API操作

阿里云提供CDN的多种刷新方式,可以通过阿里云控制台刷新,也可以直接通过阿里云的SDK进行CDN刷新,本文主要记录使用JAVA API刷新CDN。
1.CDN操作前提是已经开通了阿里云CDN服务,拿到AccessKeyID和AccessKeySecret。
API说明手册
2.引入SDK,主要使用maven:

<dependency>
  <groupId>com.aliyungroupId>
  <artifactId>aliyun-java-sdk-cdnartifactId>
  <version>2.2.3version>
dependency>

<dependency>
  <groupId>com.aliyungroupId>
  <artifactId>aliyun-java-sdk-coreartifactId>
  <version>3.0.6version>
dependency>

引入maven可以从阿里云上获取:
maven仓库:下载最新版本SDK
github源码库:这里写链接内容
3.初始化client:

public static void init() throws ClientException {
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",  
        "", "");
        client = new DefaultAcsClient(profile);
    }

4.CDN API调用:

    /**
     * 刷新资源方式-域名和资源路径分开
     */
    public static void purgeObjectCaches() {
        PurgeObjectCachesRequest request = new PurgeObjectCachesRequest();
        //要刷新的域名
        request.setDomainName("");
        //要刷新的文件路径
        request.setObjectPath("");
        //刷新类型,默认是File,刷新目录为Directory
        request.setObjectType("File");
        //设置返回格式为JSON
        request.setAcceptFormat(FormatType.JSON);

        try {
            HttpResponse httpResponse = client.doAction(request);
            System.out.println(httpResponse.getUrl());
            System.out.println(new String(httpResponse.getContent()));
            System.out.println(httpResponse.getStatus());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }


    /**
     * 刷新资源方式-域名和资源路径不分开
     */
    public static void refreshObjectCaches() {
        RefreshObjectCachesRequest request = new RefreshObjectCachesRequest();
        //要刷新的URI
        request.setObjectPath("");
        //刷新类型,默认是File,刷新目录为Directory
        request.setObjectType("File");
        //设置返回格式为JSON
        request.setAcceptFormat(FormatType.JSON);

        try {
            HttpResponse httpResponse = client.doAction(request);
            System.out.println(httpResponse.getUrl());
            System.out.println(new String(httpResponse.getContent()));
            System.out.println(httpResponse.getStatus());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询资源刷新纪录
     */
    public static void describeRefreshTasks() {
        DescribeRefreshTasksRequest request = new DescribeRefreshTasksRequest();
        //request.setTaskId("");
        request.setObjectPath("");
        request.setPageSize(10);
        request.setPageNumber(1);
        //设置返回格式为JSON
        request.setAcceptFormat(FormatType.JSON);

        try {
            HttpResponse httpResponse = client.doAction(request);
            System.out.println(httpResponse.getUrl());
            System.out.println(new String(httpResponse.getContent()));
            System.out.println(httpResponse.getStatus());
    } catch (ServerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    }

    /**
     * 查询CDN刷新剩余量
     */
    public static void describeRefreshQuota() {

        DescribeRefreshQuotaRequest request = new DescribeRefreshQuotaRequest();
        //设置返回格式为JSON
        request.setAcceptFormat(FormatType.JSON);

        try {
            HttpResponse httpResponse = client.doAction(request);
            System.out.println(httpResponse.getUrl());
            System.out.println(new String(httpResponse.getContent()));
            System.out.println(httpResponse.getStatus());
        } catch (ServerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

你可能感兴趣的:(第三方服务)