Configure endpoints

更新时间:
复制 MD 格式

An endpoint is the domain name used to access a service API, such as ecs.cn-hangzhou.aliyuncs.com for ECS in the China (Hangzhou) region. Endpoints vary by service and region. The V2.0 SDK supports two methods to configure them.

Endpoint types

Two types are available: public and VPC endpoints. Endpoints.

Endpoint settings

The V2.0 SDK provides two methods to set an endpoint, listed by priority:

Note

We recommend specifying a custom endpoint.

  1. Specify a custom endpoint: Set the endpoint when you initialize the client. Find service endpoints on the product page in OpenAPI Explorer, or follow the steps in the appendix.

    import com.aliyun.ecs20140526.Client;
    import com.aliyun.teaopenapi.models.Config;
    public class Sample {
        public static void main(String[] args) throws Exception {
            com.aliyun.teaopenapi.models.Config config = new Config();
            config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
            config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
            // Set the endpoint.
            config.setEndpoint("<endpoint>");
            Client client = new Client(config);
        }
    }
    
  2. Specify a region ID:

    • If the SDK contains a region-to-endpoint map for the service and the specified region ID exists in it, the SDK retrieves the corresponding endpoint.

    • If the map is missing or does not contain the specified region ID, the SDK constructs the endpoint automatically using a naming convention.

    import com.aliyun.ecs20140526.Client;
    import com.aliyun.teaopenapi.models.Config;
    public class Sample {
        public static void main(String[] args) throws Exception {
            Config config = new Config();
            config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
            config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
            // Map the region ID to an endpoint.
            config.setRegionId("<regionId>");
            Client client = new Client(config);
        }
    }
    

VPC endpoint use cases

Use a VPC endpoint in these scenarios:

  • Your business runs in both on-premises data centers and the cloud, with VPC-isolated modules connected over the public internet.

  • You connect a VPC-based cloud data center to your on-premises environment through Express Connect for hybrid cloud scenarios such as data security, burst capacity, and fast data synchronization.

  • Multiple applications in a VPC with different peak hours share bandwidth across IP addresses to smooth traffic and reduce costs.

  • Your services run entirely in VPCs across multiple nodes and require high-speed connections between them for geographically distributed users.

File upload authorization endpoint

Some API operations accept a custom open platform endpoint. If omitted, the SDK uses the default public endpoint. Internally, the SDK calls a file upload authentication service to obtain upload credentials and default OSS instance information.

The following example sets a VPC endpoint for the VIAPI body counting API:

import com.aliyun.facebody20191230.models.DetectBodyCountAdvanceRequest;
import com.aliyun.facebody20191230.models.DetectBodyCountResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.google.gson.Gson;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class Sample {
    public static void main(String[] args) throws Exception {
        Config config = new com.aliyun.teaopenapi.models.Config();
        config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
        config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        config.setRegionId("<regionId>");
        // To upload files over a private network by using an OSS internal endpoint, uncomment the following line.
        // config.setEndpointType("internal");
        com.aliyun.facebody20191230.Client client = new com.aliyun.facebody20191230.Client(config);
        // Set the VPC endpoint for the authentication service based on the region ID. For example: openplatform-vpc.cn-hangzhou.aliyuncs.com.
        client._openPlatformEndpoint="openplatform-vpc.<regionId>.aliyuncs.com";
        RuntimeOptions runtimeOptions = new RuntimeOptions();
        // Read the file and create an input stream.
        File f = new File("<your-file-path>");
        InputStream in =new FileInputStream(f);
        DetectBodyCountAdvanceRequest request = new DetectBodyCountAdvanceRequest();
        request.setImageURLObject(in);
        DetectBodyCountResponse resp = client.detectBodyCountAdvance(request, runtimeOptions);
        // The response contains the body and headers returned by the server.
        System.out.println(new Gson().toJson(resp.body));
        System.out.println(new Gson().toJson(resp.headers));
    }
}

Appendix: Find an endpoint

Find service endpoints in OpenAPI Explorer:

  1. From the top menu, select Products, and then choose a service such as ECS.

2. On the product homepage, find the list of regions.

3. Select the endpoint for your region ID, then copy and paste it.

Alternatively, on the API Debugging page, click Regions in the left-side navigation pane to view available endpoints.