通过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