All Products
Search
Document Center

ENS:Example of calling an API operation by using the ENS SDK

Last Updated:Dec 05, 2023

This topic describes how to call an API operation to query the Edge Node Service (ENS) nodes that are available for your account by using the ENS SDK.

View the API documentation

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

Create a RAM user and grant permissions to the RAM user

Identity

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 the "Identity, credential, and authorization" section of the Overview topic. In this topic, a RAM user is used as an example. Log on to the RAM console, create a RAM user, grant the RAM user permissions to call API operations, and then record the AccessKey ID and AccessKey secret. For more information, see Create a RAM user.

Credential

When you create a RAM user, an AccessKey pair is generated by default. You can also go to the RAM user details page. On the Authentication tab, click Create AccessKey to create an AccessKey pair. For more information, see Create an AccessKey pair.

Authorization

  1. On the Users page, find the RAM user whose permissions you want to manage and click Add Permissions in the Actions column.

  2. In the panel that appears, enter ens in the search box and select the AliyunENSFullAccess policy.

    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.

  3. Click OK to complete the authorization.

Call the operation

In this topic, ENS SDK for Java is used.

Configure environment variables

Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.

  • If you use a Linux or macOS operating system, replace the <access_key_id> and <access_key_secret> fields in the following commands with your AccessKey ID and AccessKey secret and then run the commands on your terminal:

    export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
  • If you use a Windows operating system, create a file to add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables, set the variables to your AccessKey ID and AccessKey secret, and then restart the system.

Download the sample code

  1. Visit OpenAPI Explorer.

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

  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 package to a local path and go to 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 returned values based on the comments in the code.

Run the project

In the upper-right corner, click Run to view the output logs. Sample response:

{
  "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"
  }
}