All Products
Search
Document Center

ENS:Example of using ENS SDK for Java

Last Updated:Jun 03, 2024

This topic describes how to call the DescribeEnsRegions operation to query the ENS nodes that you can use.

Step 1: View the API documentation

Before you call the DescribeEnsRegions operation, we recommend that you read the topic to learn about the parameters and permissions that are required to call this operation. For more information, see List of operations by function.

Step 2: Create a RAM user and grant permissions to the RAM user

Important

An Alibaba Cloud account has access permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M.

You can call the operation by using an Alibaba Cloud account, a Resource Access Management (RAM) user, or a RAM role. For more information about the differences between the identities, see Identity.

In this topic, a RAM user is used to call the API operation.

  1. Create a RAM user.

    1. Log on to the RAM console.

    2. In the left-side navigation pane, choose Identities > Users.

    3. On the Users page, click Create User.

    4. On the Create User page, specify the Logon Name and Display Name parameters, and set the Access Mode parameter to Console Access.

    5. Click OK.

      After you create a RAM user, record the logon name and password of the RAM user. When you call API operations, you must use the RAM user to log on to OpenAPI Explorer.

  2. Grant permissions to the RAM user.

    Note

    AliyunENSFullAccess: full access to ENS resources.

    AliyunENSReadOnlyAccess: read-only permissions on ENS resources.

    For more information about how to create a custom policy, see

    RAM authorization.

    1. Access the RAM user list.

    2. On the Users page, find the RAM user and click Add Permissions in the Actions column.

    3. Enter ENS in the search box and select the AliyunENSFullAccess policy.

    4. Click Grant permissions.

  3. Go to the details page of the RAM user. On the Authentication tab, click Create AccessKey.

    For more information, see Create an AccessKey pair.

Step 3: Call the operation

In this topic, ENS SDK for Java is used. You can use ENS SDK for other programming languages in a similar way. For more information, visit the

ENS SDK page. You can use other methods to call API operations based on your business requirements. For more information, see Overview.

Configure environment variables

Environment variables are operating system-specific global parameters. Environment variables contain variable names and variable values and allow you to configure the runtime environment for applications running on the operating system. For information about how to configure Alibaba Cloud AccessKey pairs in environment variables of different systems, see Configure environment variables in Linux, macOS, and Windows.

Download the sample code

  1. Visit

    DescribeEnsRegions.

  2. On the Parameters tab on the left side of the page, configure the request parameters.

  3. On the SDK Sample Code tab of the page, select V2.0 for SDK Version and Java for Languages. Then, click Download Project to download the sample code package.

  4. Decompress the sample code package on your computer and access the alibabacloud_sample directory.

Load the project

Use IntelliJ IDEA to load the project. After the relevant dependencies are loaded, open the src/main/java/com/aliyun/sample/Sample.java file. Add the code that is used to print the return values based on the comments in the code.

Run the project

In the upper-right corner of the IDE, click Run to view the output logs. In this example, the following content is returned:

{
  "headers": {
    "access-control-allow-origin": "*",
    "date": "Tue, 18 Jul 2023 15:26:50 GMT",
    "content-length": "92",
    "keep-alive": "timeout=25",
    "x-acs-request-id": "1589E192-A47E-5224-95E2-A18F2D1ED186",
    "connection": "keep-alive",
    "content-type": "application/json;charset=utf-8",
    "etag": "9fcxMGIK+zckvQOS1NfD6GA2",
    "access-control-expose-headers": "*",
    "x-acs-trace-id": "bdfecedff57c1e8fc1b7970e909bca31"
  },
  "statusCode": 200,
  "body": {
    "code": 0,
    "ensRegions": {
      "ensRegions": [
        {
          "Area": "SouthWestChina",
          "EnName": "",
          "EnsRegionId": "cn-chongqing-unicom",
          "Province": "Chongqing",
          "Name": "China Unicom (Chongqing)"
        }
      ]
    },
    "requestId": "1589E192-A47E-5224-95E2-A18F2D1ED186"
  }
}