通过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. Unity3d让某个物体一直正对着相机

    //将以下代码绑定到相机上 using UnityEngine; using System.Collections;   public class LookatScipt : MonoBehaviou ...

  2. C语言小程序——推箱子(窄字符和宽字符)

    C语言小程序——推箱子(窄字符Version) 推箱子.c #include <stdio.h> #include <conio.h> #include <stdlib. ...

  3. git checkout tags with the same name as a branch

    显式指定某个 tag git checkout refs/tags/ git checkout refs/tags/v0.1 显式指定某个 branch git checkout refs/heads ...

  4. Android Studio在项目中添加assets资源目录

    第一步: 切换到"Project"视图,找到app --> src --> main目录 第二步: 右键点击main目录,New --> Directory -- ...

  5. Java 实现 Http 请求工具类

    package com.demo.util; import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...

  6. Springboot中读取.yml文件

    自定义配置文件application-dev.yml spring: dataresource: druid: driver-class-name: com.mysql.jdbc.Driver url ...

  7. Angular使用总结 --- 通过指令动态添加组件

    之前自己写的公共组件,都是会先引入,需要调起的时候再通过service控制公共组件状态.值.回调函数什么的.但是有一些场景不适合这种方式,还是动态添加组件更加好.通过写过的一个小组件来总结下. 创建组 ...

  8. xamarin.Android ImageView 图片圆角(自定义属性、扩展控件)

    新增 /values/Attrs.xml 文件 <?xml version="1.0" encoding="utf-8" ?> <resour ...

  9. 在Hadoop集群上的HBase配置

    之前,我们已经在hadoop集群上配置了Hive,今天我们来配置下Hbase. 一.准备工作 1.ZooKeeper下载地址:http://archive.apache.org/dist/zookee ...

  10. java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.bjsxt.mapper.PeopleMapper

    报错信息: Type Exception Report Description The server encountered an unexpected condition that prevente ...