通过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. 将json对象转化成jsonp对象

    这个Demo用来检查是否具有唯一性 //检查 /user/check/{param}/{type} @RequestMapping("/check/{param}/{type}") ...

  2. 漏洞应急响应之批量poc验证

    1.文章难易度 [★★★] 2.文章知识点: python,poc验证; 3.文章作者: 野驴 4.本文参与 i春秋学院原创文章奖励计划,未经许可禁止转载! 0x01前言 当互联网爆出高危漏洞,或者团 ...

  3. mybatis 控制台打印sql语句

    其实很简单,打印SQL只需要加一个setting就可以了.亲测可用. mybatis-config.xml: <settings>        <setting name=&quo ...

  4. Java 大数值类型执行精确计算

    简介 如果基本的整数和浮点数精度不能够满足需求,那么可以使用 java.math 包下两个很有用的类:BigInteger 和 BigDecimal.这两个类可以处理包含任意长度数字序列的数值,Big ...

  5. javaScript 二分查找

    什么是二分查找的,举个栗子: var arr = [1, 3, 5, 7, 9, 11, 14, 15, 17, 19, 20]; 上面有序数组, 随便给你一位 9 ,输出该数在数组中的索引.   当 ...

  6. Testing - 软件测试知识梳理 - 自动化测试

    软件开发的过程是一个持续集成和改进的过程,而每一次的改进都可能引进新bug,因此当软件的一部,或者全部修改时,都需要对软件产品重新进行测试. 其目的是要验证修改后的产品是符合需求的,而当没有自动化测试 ...

  7. Autowired byType 与 byName 策略

    @Autowired是spring的注解,默认使用的是byType的方式向Bean里面注入相应的Bean.例如: @Autowiredprivate UserService userService;这 ...

  8. C# DataGridView 在最左侧显示行号方法

    代码: private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { Da ...

  9. 如何把checkbox做成radio一样的单选效果

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>复 ...

  10. Android SD卡读写

    package com.jredu.zuiyou.activity; import android.os.Bundle;import android.os.Environment;import and ...