通過AccessKey調用阿里雲CDN接口刷新CDN資源案例


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM