阿里雲CDN的API操作
阿新 • • 發佈:2019-01-09
阿里雲提供CDN的多種重新整理方式,可以通過阿里雲控制檯重新整理,也可以直接通過阿里雲的SDK進行CDN重新整理,本文主要記錄使用JAVA API重新整理CDN。
1.CDN操作前提是已經開通了阿里雲CDN服務,拿到AccessKeyID和AccessKeySecret。
API說明手冊
2.引入SDK,主要使用maven:
<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>
引入maven可以從阿里雲上獲取:
maven倉庫:下載最新版本SDK
github原始碼庫:這裡寫連結內容
3.初始化client:
public static void init() throws ClientException {
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",
"<your accessKey>", "<your accessSecret>");
client = new DefaultAcsClient(profile);
}
4.CDN API呼叫:
/**
* 重新整理資源方式-域名和資源路徑分開
*/
public static void purgeObjectCaches() {
PurgeObjectCachesRequest request = new PurgeObjectCachesRequest();
//要重新整理的域名
request.setDomainName("<only your domain name>");
//要重新整理的檔案路徑
request.setObjectPath("<file path in your domain>");
//重新整理型別,預設是File,重新整理目錄為Directory
request.setObjectType("File");
//設定返回格式為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() {
RefreshObjectCachesRequest request = new RefreshObjectCachesRequest();
//要重新整理的URI
request.setObjectPath("<your complete resourse url>");
//重新整理型別,預設是File,重新整理目錄為Directory
request.setObjectType("File");
//設定返回格式為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();
}
}