本教程详细介绍如何使用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.DescribeUserConfigsRequest;
import com.aliyuncs.cdn.model.v20180510.DescribeUserConfigsResponse;
import com.aliyuncs.cdn.model.v20180510.DescribeUserDomainsRequest;
import com.aliyuncs.cdn.model.v20180510.DescribeUserDomainsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

/**
 * DescribeUserDomains    查询用户名下所有的域名与状态
 * DescribeUserConfigs    获取用户相应的配置
 */
public class Demo {

    private static Gson gson = new Gson();

    public static void main(String[] args) {

        IAcsClient client = initialization();
        // 询用户名某域名的状态
        DescribeUserDomainsResponse describeUserDomainsResponse = describeUserDomains(client);
        // 获取oos相应的配置
        DescribeUserConfigsResponse describeUserConfigsResponse = describeUserConfigs(client);
        // 此处处理业务逻辑

    }

    /**
     * DescribeUserConfigs    获取用户相应的配置
     */
    private static DescribeUserConfigsResponse describeUserConfigs(IAcsClient client) {
        DescribeUserConfigsRequest request = new DescribeUserConfigsRequest();
        // 需要查询的配置,支持单个查询。取值:
        // oss
        // green_manager
        // waf
        request.setConfig("oss");
        System.out.println("------------------describeUserConfigs-------------------");
        try {
            DescribeUserConfigsResponse response = client.getAcsResponse(request);
            System.out.println(gson.toJson(response));
            return 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());
            throw new RuntimeException(e);
        }
        return null;
    }

    /**
     * DescribeUserDomains    查询用户名下所有的域名与状态
     */
    private static DescribeUserDomainsResponse describeUserDomains(IAcsClient client) {
        DescribeUserDomainsRequest request = new DescribeUserDomainsRequest();
        // 域名查询类型:
        // fuzzy_match:模糊匹配
        // pre_match:前匹配
        // suf_match:后匹配
        // full_match: 完全匹配
        // 默认值:fuzzy_match
        request.setDomainSearchType("fuzzy_match");
        // 域名模糊匹配过滤
        request.setDomainName("oss");
        System.out.println("------------------describeUserDomains-------------------");
        try {
            DescribeUserDomainsResponse response = client.getAcsResponse(request);
            System.out.println(gson.toJson(response));
            return 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());
            throw new RuntimeException(e);
        }
        return null;
    }

    /**
     * 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);
    }
}

执行结果

正确的执行结果类似如下:
------------------describeUserDomains-------------------
{
  "requestId": "4756356E-D005-4CB5-9D7C-1BE1BECAC4E2",
  "pageNumber": 1,
  "pageSize": 20,
  "totalCount": 1,
  "domains": [
    {
      "domainName": "videoliveoss.aliyuntest.club",
      "cname": "videoliveoss.aliyuntest.club.w.alikunlun.com",
      "cdnType": "web",
      "domainStatus": "offline",
      "gmtCreated": "2017-11-02T08:14:23Z",
      "gmtModified": "2017-12-31T20:19Z",
      "description": "沪ICP备17027590号-1",
      "sslProtocol": "off",
      "resourceGroupId": "rg-acfmxazb4ph6aiy",
      "sandbox": "",
      "sources": [
        {
          "type": "oss",
          "content": "videolive1.oss-cn-beijing.aliyuncs.com",
          "port": 80,
          "priority": "20",
          "weight": "10"
        }
      ]
    }
  ]
}
------------------describeUserConfigs-------------------
{
  "requestId": "DF300FD1-0424-43E6-8B75-81823854A162",
  "configs": {
    "ossLogConfig": {...},
    "greenManagerConfig": {...},
    "wafConfig": {...}
  }
}