Disclaimer: This article may contain information about third-party products. Such information is for reference only. Alibaba Cloud does not make a guarantee, explicitly or implicitly, with respect to the performance and reliability of the third-party products, and potential impacts of operations on the products.

Description

This topic describes how to use the JAVA SDK for Alibaba Cloud Content Delivery Network to refresh the cache.

References

Alibaba Cloud CDN provides SDKs for multiple languages such as JAVA, Python, PHP, and. Net. For more information, see Alibaba Cloud SDK Center. This topic uses the JAVA SDK as an example.

  1. When you use the JAVA SDK, add the following code to the pom.xml file. Note that you must add the exact SDK version number. For more information, see Alibaba Cloud SDK Center.
    <! -- Add Alibaba Cloud core dependencies and Alibaba Cloud Content Delivery Network dependencies --> 
    <dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-Alibaba Cloud Content Delivery Network </artifactId>
    <version>3.0.10</version>
    </dependency>
    <dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.1.0</version>
    </dependency>
  2. Before you initiate a call, initialize the IAcsClient instance. The sample code is as follows.
    DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");        
    IAcsClient client = new DefaultAcsClient(profile);
  3. To construct a refresh request, see the following complete sample code.
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.profile.DefaultProfile;
    import com.google.gson.Gson;
    import java.util.*;
    import com.aliyuncs.cdn.model.v20180510.*;
    public class RefreshObjectCaches {
        public static void main(String[] args) {
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
            IAcsClient client = new DefaultAcsClient(profile);
            RefreshObjectCachesRequest request = new RefreshObjectCachesRequest();
            request.setObjectPath("example.aliyundoc.com/abc/1.png");
            request.setObjectType("File");
            try {
                RefreshObjectCachesResponse response = client.getAcsResponse(request);
                System.out.println(new Gson().toJson(response));
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                System.out.println("ErrCode:" + e.getErrCode());
                System.out.println("ErrMsg:" + e.getErrMsg());
                System.out.println("RequestId:" + e.getRequestId());
            }
        }
    }

Applicable scope

  • CDN