本教程详细介绍如何使用Alibaba Cloud SDK for Java删除和修改CDN配置。

前提条件

在使用本教程前,请确保已完成以下操作:
  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见 阿里云开发工具包(SDK)
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-cdn -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-cdn</artifactId>
            <version>3.0.10</version>
        </dependency>
    </dependencies>

代码示例

本文操作示例主要以代码形式体现,具体代码如下:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.cdn.model.v20180510.*;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

/**
 * 修改和删除CDN配置
 */
public class DeleteAndUpdateCdnConfig {

    /**
     * 查询用户名下,源站对应的所有域名名称列表
     */
    private static String DescribeDomainsBySource(IAcsClient client, String source) throws ClientException {
        DescribeDomainsBySourceRequest request = new DescribeDomainsBySourceRequest();
        //源站域名
        request.setSources(source);
        DescribeDomainsBySourceResponse response = client.getAcsResponse(request);
        return new Gson().toJson(response);
    }

    /**
     * 获取指定加速域名配置的基本信息
     */
    private static String DescribeCdnDomainDetail(IAcsClient client, String domainName) throws ClientException {
        DescribeCdnDomainDetailRequest request = new DescribeCdnDomainDetailRequest();
        //加速域名
        request.setDomainName(domainName);
        DescribeCdnDomainDetailResponse response = client.getAcsResponse(request);
        return new Gson().toJson(response);
    }

    /**
     * 删除加速域名的配置
     */
    private static String DeleteSpecificConfig(IAcsClient client, String domainName) throws ClientException {
        DeleteSpecificConfigRequest request = new DeleteSpecificConfigRequest();
        //加速域名
        request.setDomainName(domainName);
        DeleteSpecificConfigResponse response = client.getAcsResponse(request);
        return new Gson().toJson(response);
    }

    /**
     * 域名批量配置
     */
    private static String BatchSetCdnDomainConfig(IAcsClient client, String domainNames, String functions) throws ClientException {
        BatchSetCdnDomainConfigRequest request = new BatchSetCdnDomainConfigRequest();
        // 加速域名,多个用逗号(,)分隔
        request.setDomainNames(domainNames);
        // 功能列表
        request.setFunctions(functions);
        BatchSetCdnDomainConfigResponse response = client.getAcsResponse(request);
        return new Gson().toJson(response);
    }

    public static void main(String[] args) {
        // 设置鉴权参数,初始化客户端
        DefaultProfile profile = DefaultProfile.getProfile(
                "<your-region-id>",// 地域ID
                "your-access-key-id",// 您的AccessKey ID
                "your-access-key-secret");// 您的AccessKey Secret
        IAcsClient client = new DefaultAcsClient(profile);

        try {
            //查询源站对应所有域名名称列表
            String describeDomainsBySource = DescribeDomainsBySource(client, "xxxxxx.oss-cn-beijing.aliyuncs.com");
            System.out.println(describeDomainsBySource);
            //查询指定加速域名配置的基本信息
            String describeCdnDomainDetail = DescribeCdnDomainDetail(client, "subDomainName1.youDomainName.com");
            System.out.println(describeCdnDomainDetail);
            //删除加速域名的配置
            String deleteSpecificConfig = DeleteSpecificConfig(client, "subDomainName.youDomainName.com");
            System.out.println(deleteSpecificConfig);
            String functions = "[{\"functionArgs\":[{\"argName\":\"ip_list\",\"argValue\":\"'66.66.66.66','88.88.88.88'\"}],\"functionName\":\"ip_allow_list_set\"},{\"functionArgs\":[{\"argName\":\"error_code\",\"argValue\":\"404\"},{\"argName\":\"rewrite_page\",\"argValue\":\"http://www.baidu.com\"}],\"functionName\":\"error_page\"}]";
            //进行域名批量配置
            String batchSetCdnDomainConfig = BatchSetCdnDomainConfig(client, "subDomainName.youDomainName.com", functions);
            System.out.println(batchSetCdnDomainConfig);
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

运行结果

正确的执行结果类似如下:
{
    "requestId": "61268324-F272-46D1-BB5B-257AD271C59F",
    "sources": "bucket-20180423-2.oss-cn-beijing.aliyuncs.com",
    "domainsList": [
        {
            "source": "bucket-20180423-2.oss-cn-beijing.aliyuncs.com",
            "domainInfos": [
                {
                    "domainName": "test002.aliyuntest.club",
                    "domainCname": "test002.aliyuntest.club.w.cdngslb.com",
                    "createTime": "2019-09-10T10:38:42+08:00",
                    "updateTime": "2019-09-10T10:40:19+08:00",
                    "status": "online"
                }
            ],
            "domains": [
                "test002.aliyuntest.club"
            ]
        }
    ]
}
{
    "requestId": "837088BD-5290-4684-A32E-0BDD7141D7A7",
    "getDomainDetailModel": {
        "domainName": "test002.aliyuntest.club",
        "cname": "test002.aliyuntest.club.w.alikunlun.com",
        "domainStatus": "online",
        "cdnType": "web",
        "gmtCreated": "2019-09-10T02:43:31Z",
        "gmtModified": "2019-09-10T10:45Z",
        "resourceGroupId": "rg-acfmxazb4phxxxx",
        "description": "",
        "scope": "domestic",
        "sourceModels": [
            {
                "content": "test006.aliyuntest.club",
                "type": "domain",
                "port": 80,
                "enabled": "online",
                "priority": "20",
                "weight": "10"
            }
        ]
    }
}
{
    "requestId": "5D32613E-8CFC-43D3-B3AB-3E0C26AC85D5"
}
{
    "requestId": "D814A0ED-B984-4F2B-88B2-2F7FF7C944DC"
}