zoukankan      html  css  js  c++  java
  • 通过AccessKey调用阿里云CDN接口刷新CDN资源案例

    通过AccessKey远程调用阿里云CDN接口,快速实现自动化集成部署。

    CdnService.java

    package com.nfky.cdn;
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.cdn.model.v20141111.DescribeRefreshQuotaRequest;
    import com.aliyuncs.cdn.model.v20141111.DescribeRefreshTasksRequest;
    import com.aliyuncs.cdn.model.v20141111.PurgeObjectCachesRequest;
    import com.aliyuncs.cdn.model.v20141111.RefreshObjectCachesRequest;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.http.FormatType;
    import com.aliyuncs.http.HttpResponse;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.profile.IClientProfile;
    public class CdnService{
        private static IAcsClient  client ;
        public static void init() throws ClientException {
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",  
            "LRA4Xx0cI1XeDk11", "yJnIHHDTeeGMa6ad0BIZ8X128HW3I5");
            client = new DefaultAcsClient(profile);
        }
        /**
         * 刷新资源方式-域名和资源路径分开
         */
        public static void purgeObjectCaches(String host, String filePath, String fileType) {
            PurgeObjectCachesRequest request = new PurgeObjectCachesRequest();
            //要刷新的域名
            request.setDomainName(host);
            //要刷新的文件路径
            request.setObjectPath(filePath);
            //刷新类型,默认是File,刷新目录为Directory
            request.setObjectType(fileType);
            //设置返回格式为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(String url,String fileType) {
            RefreshObjectCachesRequest request = new RefreshObjectCachesRequest();
            //要刷新的URI
            request.setObjectPath(url);
            //刷新类型,默认是File,刷新目录为Directory
            request.setObjectType(fileType);
            //设置返回格式为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("<your taskid>");
            request.setObjectPath("<your complete url>");
            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();
            }
        }}

    AlyCdn.java

    package com.nfky.cdn;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    import com.aliyuncs.exceptions.ClientException;
    @Controller
    @RequestMapping(value = "/aly")
    public class AlyCdn {
        @RequestMapping(value = "/cdn", method = { RequestMethod.GET })
        @ResponseBody
        public String list(HttpServletRequest request, HttpServletResponse response) {
            String url=request.getParameter("url");
            try {
                Cdn.init() ;
                Cdn.refreshObjectCaches(url, "File");
            } catch (ClientException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return "ok";
        }
    }

    maven三维坐标 pom.xml

      <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-cdn</artifactId>
        <version>2.2.3</version>
    </dependency>
    <dependency>
         <groupId>com.aliyun</groupId>
         <artifactId>aliyun-java-sdk-core</artifactId>
         <version>3.0.6</version>
    </dependency>
            

    测试一把:

    curl http://127.0.0.1:8080/aly/cdn?AccessKeyId=LfAISu1cI0XrVd51&url=http://www.apistor.com
  • 相关阅读:
    C# 数据库访问
    ArcGIS API For Silverlight使用在线地图的多种方法总结
    Google Map Api 谷歌地图接口整理
    定义函数up(ch),如字符变量ch是小写字母就转换成大写字母并通过up返回,否则字符ch不改变。要求在短小而完全的程序中显示这个程序是怎样被调用的-简单
    编写一个求方程ax^2+bx+c=0的根的程序,用3个函数分别求当b^2-4ac大于零、等于零和小于零时的方程的根。要求从主函数输入a、b、c的值并输出结果-简单
    已知一个string的对象str的内容为“we are here!”, 使用多种方法输出字符“h”-简单
    使用多种方法编写将两个字符串连接在一起的程序-简单
    c++简单函数应用
    编写一个完整的程序,它读入15个float值,用指针把它们存放在一个存储快里,然后输出这些值的和以及最小值
    编写一个为int型变量分配100个整型量空间的程序-简单
  • 原文地址:https://www.cnblogs.com/Javame/p/6564189.html
Copyright © 2011-2022 走看看