本教程详细介绍如何使用Alibaba Cloud SDK for Java查询域名访问日志及源服务器流量。

前提条件

在使用本教程前,请确保已完成以下操作:
  • 使用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.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;

/**
 * DescribeCdnCertificateList    获取证书列表信息
 * DescribeUserDomains            查询用户名下所有的域名与状态
 * AddCdnDomain                    添加加速域名
 * BatchSetCdnDomainConfig    进行域名批量配置
 * RefreshObjectCaches            刷新指定URL内容至Cache节点
 */
public class AddCdnDomain {

    private static String domainName = "******";
    private static String objectPath = "******";

    public static void main(String[] args) {
        Gson gson = new Gson();
        IAcsClient client = initialization();
        try {
            // 获取用户名下所有的证书列表信息
            DescribeCdnCertificateListResponse describeCdnCertificateListResponse = describeCdnCertificateList(client);
            System.out.println(gson.toJson(describeCdnCertificateListResponse));
            // 查询用户名下所有的域名与状态
            DescribeUserDomainsResponse describeUserDomainsResponse = describeUserDomains(client);
            System.out.println(gson.toJson(describeUserDomainsResponse));
            // 添加加速域名
            AddCdnDomainResponse addCdnDomainResponse = addCdnDomain(client, domainName);
            System.out.println(gson.toJson(addCdnDomainResponse));
            // 进行域名批量配置
            BatchSetCdnDomainConfigResponse batchSetCdnDomainConfigResponse = batchSetCdnDomainConfig(client, domainName);
            System.out.println(gson.toJson(batchSetCdnDomainConfigResponse));
            // 刷新指定URL内容至Cache节点
            RefreshObjectCachesResponse refreshObjectCachesResponse = refreshObjectCaches(client, objectPath);
            System.out.println(gson.toJson(refreshObjectCachesResponse));

        } 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());
        }
    }

    /**
     * RefreshObjectCaches        刷新指定URL内容至Cache节点
     */
    private static RefreshObjectCachesResponse refreshObjectCaches(IAcsClient client, String objectPath) throws ClientException {
        RefreshObjectCachesRequest request = new RefreshObjectCachesRequest();
        // 多个URL之间需要用换行符(\n)或(\r\n)分隔
        request.setObjectPath(objectPath);
        return client.getAcsResponse(request);
    }

    /**
     * BatchSetCdnDomainConfig        进行域名批量配置
     */
    private static BatchSetCdnDomainConfigResponse batchSetCdnDomainConfig(IAcsClient client, String domainName) throws ClientException {
        System.out.println("------------------addCdnDomain-------------------");
        BatchSetCdnDomainConfigRequest request = new BatchSetCdnDomainConfigRequest();
        request.setDomainNames(domainName);
        // 封装functions参数
        String functions = getFunctions();
        // 功能列表
        request.setFunctions(functions);
        return client.getAcsResponse(request);
    }

    /**
     * AddCdnDomain        添加加速域名
     */
    private static AddCdnDomainResponse addCdnDomain(IAcsClient client,String domainName) throws ClientException {
        System.out.println("------------------addCdnDomain-------------------");
        AddCdnDomainRequest request = new AddCdnDomainRequest();
        request.setCdnType("web");
        request.setDomainName(domainName);
        request.setCheckUrl(domainName);
        request.setSources("[" +
                            "  {" +
                            "    'content': '1.1.1.1'," +
                            "    'type': 'ipaddr'," +
                            "    'priority': '20'," +
                            "    'port': 80," +
                            "    'weight': '15'" +
                            "  }" +
                            "]");
        return client.getAcsResponse(request);
    }

    /**
     * DescribeUserDomains        查询用户名下所有的域名与状态
     */
    private static DescribeUserDomainsResponse describeUserDomains(IAcsClient client) throws ClientException {
        System.out.println("------------------describeUserDomains-------------------");
        DescribeUserDomainsRequest request = new DescribeUserDomainsRequest();
        return client.getAcsResponse(request);
    }

    /**
     * DescribeCdnCertificateList        获取证书列表信息
     */
    private static DescribeCdnCertificateListResponse describeCdnCertificateList(IAcsClient client) throws ClientException {
        System.out.println("------------------describeCdnCertificateList-------------------");
        DescribeCdnCertificateListRequest request = new DescribeCdnCertificateListRequest();
        return client.getAcsResponse(request);
    }

    /**
     *  域名配置功能列表
     */
    private static String getFunctions(){
        Functions functions = new Functions();
        List<FunctionArg> functionArgs = new ArrayList<FunctionArg>();
        // 设置文件过期时间
        functions.setFunctionName("filetype_based_ttl_set");
        // 设置此配置ID
        functions.setConfigId("5068995");
        // 设置第一个参数
        FunctionArg functionArg = new FunctionArg();
        // 文件类型,支持多个,用逗号(,)隔开,例如:txt,jpg
        functionArg.setArgName("file_type");
        functionArg.setArgValue("jpg");
        functionArgs.add(functionArg);

        // 设置第二个参数
        FunctionArg functionArg1 = new FunctionArg();
        // cache时间,单位:秒
        functionArg1.setArgName("ttl");
        functionArg1.setArgValue("18");
        functionArgs.add(functionArg1);

        // 设置第三个参数
        FunctionArg functionArg2 = new FunctionArg();
        // 权重,取值范围1~199
        functionArg2.setArgName("weight");
        functionArg2.setArgValue("18");
        functionArgs.add(functionArg2);

        functions.setFunctionArgs(functionArgs);
        String json = new Gson().toJson(functions);
        System.out.println("Functions 功能列表:" + json);
        return json;
    }

    /**
     * Initialization  初始化公共请求参数
     */
    private static IAcsClient initialization() {
        // 初始化请求参数
        DefaultProfile profile = DefaultProfile.getProfile(
                "<your-region-id>", // 您的可用区ID
                "<your-access-key-id>", // 您的AccessKey ID
                "<your-access-key-secret>"); // 您的AccessKey Secret
        return new DefaultAcsClient(profile);
    }

    private static class Functions{
        private String configId;

        private String functionName;

        private List<FunctionArg> functionArgs ;

        public void setFunctionName(String functionName) {
            this.functionName = functionName;
        }


        public void setFunctionArgs(List<FunctionArg> functionArgs) {
            this.functionArgs = functionArgs;
        }

        public void setConfigId(String configId) {
            this.configId = configId;
        }
    }

    private static class FunctionArg{
        private String argValue;
        private String argName;

        public void setArgName(String argName) {
            this.argName = argName;
        }

        public void setArgValue(String argValue) {
            this.argValue = argValue;
        }
    }
}

执行结果

正确的执行结果类似如下:
------------------describeCdnCertificateList-------------------
{
  "requestId": "AD7003B9-93B7-476B-A5BD-911C5A6E0816",
  "certificateListModel": {
    "count": 2,
    "certList": [
      {
        "certName": "cert-2660227",
        "certId": 2855182,
        "fingerprint": "54145F84000380D8CF05CFCC13E8EE75C015E75F",
        "common": "tmp.letime.fun",
        "issuer": "DigiCert Inc",
        "lastTime": 1569319303
      },
      {
        "certName": "cert-2660226",
        "certId": 2856610,
        "fingerprint": "5A46761893F8C449323E4F2CCDBF306391688F30",
        "common": "tmp.letime.fun",
        "issuer": "DigiCert Inc",
        "lastTime": 1569374469
      }
    ]
  }
}
------------------describeUserDomains-------------------
{
  "requestId": "FD3B79AA-D57E-45AF-AE1D-A08A1831353D",
  "pageNumber": 1,
  "pageSize": 20,
  "totalCount": 4,
  "domains": [
    {
      "domainName": "video005.******.club",
      "cname": "video005.******.club.w.*****.com",
      "cdnType": "web",
      "domainStatus": "online",
      "gmtCreated": "2019-09-10T02:43:31Z",
      "gmtModified": "2019-09-10T10:45Z",
      "description": "",
      "sslProtocol": "off",
      "resourceGroupId": "rg-***",
      "sandbox": "",
      "sources": [
        {
          "type": "domain",
          "content": "video006.*****.club",
          "port": 80,
          "priority": "20",
          "weight": "10"
        }
      ]
    },
    {
      "domainName": "live002.*****.club",
      "cname": "live002.*****.club.w.cdngslb.com",
      "cdnType": "web",
      "domainStatus": "online",
      "gmtCreated": "2019-09-10T02:38:42Z",
      "gmtModified": "2019-09-10T10:40Z",
      "description": "",
      "sslProtocol": "off",
      "resourceGroupId": "rg-*****",
      "sandbox": "",
      "sources": [
        {
          "type": "oss",
          "content": "bucket-*****-2.oss-cn-***.***.com",
          "port": 443,
          "priority": "20",
          "weight": "10"
        }
      ]
    }
  ]
}
------------------addCdnDomain-------------------
{
  "RequestId": "04F0F334-1335-436C-A1D7-6C044FE73368"
}
------------------refreshObjectCaches-------------------
{
    "RefreshTaskId":"704222904",
    "RequestId":"D61E4801-EAFF-4A63-AAE1-FBF6CE1CFD1C"
}