通过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();
request.setPageNumber();
//设置返回格式为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.</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.0.</version>
</dependency>

测试一把:

curl http://127.0.0.1:8080/aly/cdn?AccessKeyId=LfAISu1cI0XrVd51&url=http://www.apistor.com

通过AccessKey调用阿里云CDN接口刷新CDN资源案例的更多相关文章

  1. 用ajax与fetch调用阿里云免费接口

    最近学习态度比较积极,打算用react做一个小个人应用网站...所以从阿里云上买了些免费的接口,什么QQ音乐排行查询接口.IP地址查询.天气预报等等.调用时,发现身份校验可以通过简单修改头部信息的方式 ...

  2. C#调用阿里云CDN API刷新缓存

    使用CDN必须要解决CDN缓存的问题,要么在每次更新文件时生成不同的URL,要么在每次更新文件时刷新CDN缓存.我们在一个实际应用场景中用到了后者,所以需要调用阿里云CDN的API进行缓存刷新的操作. ...

  3. 使用Node.js调用阿里云短信的发送以及接收

    为了使用Node.js调用阿里云短信服务,我自己写了个npm包, 目前实现了: 使用Node.js调用阿里云短信服务,发送短信: 使用Node.js调用阿里云短信服务以及MNS服务,接收用户上行短信 ...

  4. Python 调用阿里云 API 收集 ECS 数据

    #!/usr/bin/env python # coding: utf-8 # author: Wang XiaoQiang ''' 功能介绍: 1.调用阿里云API,收集所有区域 ECS 信息 2. ...

  5. 用Python调用华为云API接口发短信

    [摘要] 用Python调用华为云API接口实现发短信,当然能给调用发短信接口前提条件是通过企业实名认证,而且有一个通过审核的短信签名,话不多说,showcode #!/usr/bin/python3 ...

  6. 阿里云oss对象存储配置CDN

    阿里云oss对象存储配置CDN 1.打开阿里云CDN 2.填写信息,这个地方要注意,我的备案域名是www.ljwXXX.work,我们可以自定义一个域名,test.ljwXXX.work作为加速域名. ...

  7. Java调用阿里云短信接口发送手机验证码

    前五步可参考阿里云服务文档:https://help.aliyun.com/document_detail/59210.html?spm=a2c4g.11174283.4.1.2b152c42DoJ7 ...

  8. 使用visual studio 2015调用阿里云oss .net sdk 2.2的putobject接口抛出outofmemory异常

    问题描述: 使用阿里云oss .net sdk 2.2版本,使用putobject接口上传文件时,抛出outofmemory异常. 原因分析: 上传时,用于准备上传的数据缓冲区内存分配失败.与应用软件 ...

  9. java 调用阿里云短信接口,报InvalidTimeStamp.Expired : Specified time stamp or date value is expired.

    官网解释: 问题所在: 自己的电脑(或者服务器) 的时间与阿里云的服务器时间 相差15分钟了. 解决方法 : 把自己的电脑时间 (或者服务器)的时间 改成标准的北京时间就行了.

随机推荐

  1. 中国剩余定理poj1006

    中国剩余定理即解一组带余除法的不定方程组(同余式组解法). 例如:求一个最小数x,已知x%3=2且x%5=3且x%7=2. 思路就是: 1.先从(3,5)的公倍数中找一个%7=1的最小公倍数,这里是1 ...

  2. RxSwift学习笔记9:amb/tabkeWhile/tabkeUntil/skipWhile/skipUntil

    //amb基本介绍 //当传入多个 Observables 到 amb 操作符时,它将取第一个发出元素或产生事件的 Observable,然后只发出它的元素. //并忽略掉其他的 Observable ...

  3. pb数据导出

    pb数据导出(一) 1.在窗口新建用户事件  ue_export    2.事件调用函数 gf_dw_to_excel(THIS.dw_dict) 3.写函数 :boolean lb_setborde ...

  4. Repository 简化实现多条件查询

    Repository 在做查询的时候,如果查询条件多的话,linq查询表达式会写的很复杂,比如: public IQueryable<Student> Get(int id, string ...

  5. C++中const关键字 理解

    const:符号常量  使用符号常量写出的代码更容易维护:指针是边读边移动,而不是边写边移动,许多函数参数是只读不写的. const最常见用途是作为数组的界和switch分情况标号(也可以用枚举符代替 ...

  6. 一个可遇不可求的 bug 全局变量初始化顺序问题 哈哈

    这是今天下午帮同事查的一个客户端 C++ 的 bug,前人留下的谜之代码.. 具体情况是,客户端实现了有一个简单的内存池,每次申请内存的时候会把新申请到的内存信息存到一个 map 里,据说是为了检查内 ...

  7. DOMContentLoaded事件中使用异步

    概述 我在之前的博文(Performance面板看js加载)中提到过,如果利用监听DOMContentLoaded事件的方式来加载js是不能优化加载的,不能够替代jquery中的ready方法,原因是 ...

  8. javascript History对象属性和方法

    History对象 History对象包含用户(在浏览器窗口中)访问过的URL length: 返回浏览器历史列表中的URL数量(打开浏览器,访问淘宝,返回1,再访问百度,返回2) History对象 ...

  9. linux下应用程序性能剖分神器gprofiler-tools-安装和使用

    最近在摆弄算法的的优化,需要剖分一下算法的瓶颈,就找了一些代码剖分工具,其中 gprofileer-tools是很不错的工具,gperftools时google开源的一款C++性能分析分析工具,git ...

  10. [P1329] 数列

    设F[i,j]为长度为i是,前缀和为j的方案数. [转移] F[i,j] => F[i+1,j+i] F[i,j] => F[i+1,j-i] [原理] 由于A[0]=0,所以有A[1]= ...