This topic describes how to call the AddCdnDomain operation to add a domain to accelerate its content delivery when the accelerated region is set to global or global (excluding mainland China).

Prerequisites

Before you call this operation, complete the following steps or read the following topics.

Background information

CDN can be applied in a wide array of scenarios, including but not limited to:
  • Small image files: Your websites or applications primarily host images and small files in formats such as HTML, CSS, and JavaScript.
  • Downloads of large files: Your websites or applications primarily host files larger than 20 MB, such as gaming applications, client applications, or app stores.
  • On-demand audio and video streaming: Your websites or applications primarily host on-demand video streams or short video streams. Mainstream formats such as MP4 and FLV are supported.

If your workloads encounter the preceding scenarios, and the origin server is deployed in a region outside mainland China, see the following section describing how to accelerate content delivery by using SDK for Java.

Procedure

  1. Add Maven dependencies.
    Note The version of the SDK in this example may be different from that of your actual SDK. For more information about the latest version, visit Alibaba Cloud Open Platform.
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-ads</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-core</artifactId>
        <version>4.4.6</version>
    </dependency>
  2. Call the AddCdnDomain operation.
    This example adds an Object Storage Service (OSS) domain to CDN. The accelerated region is set to global. The OSS domain primarily processes downloads of images and small files. For more information about the API operation, see AddCdnDomain.
    Note If your CDN is deployed outside mainland China, select global or global (excluding mainland China) as the accelerated region. When you select global as the accelerated region, the accelerated domain must obtain an ICP filing. We recommend that you apply for an ICP filing through Alibaba Cloud ICP Filing System.
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.profile.DefaultProfile;
    import java.util.*;
    import com.aliyuncs.cms.model.v20190101.*;
    
    public class AddCdnDomain {
        public static void main(String[] args) {
            // Specify your AccessKey ID and AccessKey secret.
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
            IAcsClient client = new DefaultAcsClient(profile);
    
            AddImageRequest request = new AddImageRequest();
            request.setRegionId("cn-hangzhou");
            request.setCdnType("web");
            request.setDomainName("****.aliyun.com");
            request.setSources("[{\"content\":\"test.oss-cn-hangzhou.aliyuncs.com\",\"type\":\"oss\",\"priority\":\"20\",\"port\":80,\"weight\":\"15\"}]");
            //Select the accelerated region. If the accelerated region is outside mainland China, enter global or overseas.
            request.setScope("global");
            try {
                AddImageResponse 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());
            }
    
        }
    }

Result

If the API call is successful, the system returns a request ID to you.
{ 
  "RequestId": "0AEDAF20-4DDF-4165-8750-47FF9C1929C9" 
}

What to do next

You can log on to the Alibaba Cloud CDN console to view the accelerated domain and configure CNAME records. For more information about what to do next, see Configure a CNAME record.