通过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. HTK计算mfcc/filter_bank源码解析

    HTK计算mfcc/filter_bank源码解析 HTK可以用简单的 HCopy -C config -s scp 求取mfcc或者filter_bank 关于mfcc的原理在 http://my. ...

  2. 消息中间件——RabbitMQ

    RabbitMQ介绍 RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性.扩展性.高可用性等方面表现不俗.RabbitM ...

  3. npm全局目录修改

    转载:http://www.qdfuns.com/notes/30749/0f66fcf5e62eed010f744d0d4adaa870.html 我之前安装npm时全是默认安装,模块全部安装在C盘 ...

  4. web API简介(三):客户端储存之Web Storage API

    概述 前篇:web API简介(二):客户端储存之document.cookie API 客户端储存从某一方面来说和动态网站差不多.动态网站是用服务端来储存数据,而客户端储存是用客户端来储存数据. W ...

  5. 请求报错:“应以Content-Type: application/x-www-form-urlencoded为请求类型,在form表单中提交登录信息。"

    竟然是post 方法少了参数 // // 摘要: // 以异步操作将 POST 请求发送给指定 URI. // // 参数: // requestUri: // 请求发送到的 URI. // // c ...

  6. 使用diskpart命令格式化U盘

    在安装各种linux系统时,经常要使用SD卡,这里就出了个问题,就是SD卡被linux格式化之后无法在widdows下使用. 并且windows下的格式化按键对它毫无作用.笔者在摸索的过程中,找到了格 ...

  7. Win10手记-为应用集成SQLite(二)

    接上篇内容,这里给大家分享我的辅助访问类,采用了异步方法,封装了常用的访问操作,一些操作还是纯CLI的. SQLiteDBManager using System; using System.Coll ...

  8. jsp-静态包含和动态包含的区别

    include指令是静态包含.静态包含的意思就是:把文件的代码内容都包含进来,再编译! include指令是静态包含,include行为是动态包含.其实include行为就是封装了request.ge ...

  9. 在mpvue中使用map如何避坑

    最近在做一个需求,当用户放大地图到某个级别时,自动显示marker的callout标签,当小于这个缩放级别时,则隐藏callout.然而在我实现的过程中,却发现一个严重的问题:当我操作marker数据 ...

  10. Volley Get网络请求

    public class VolleyActivity extends AppCompatActivity { WebView webView; Button button; RequestQueue ...