文档

Endpoint 配置

更新时间:

本节主要介绍升级版 SDK 对于 Endpoint 的配置。

说明

Endpoint 是请求接口服务的网络域名,如产品 ECS 的某个 Endpoint:ecs.cn-hangzhou.aliyuncs.com。每个产品都有其独立的 Endpoint,并且 Endpoint 与服务区域 RegionId 有关,不同地域可能是不同的 Endpoint。

Endpoint 设置

升级版 SDK 在 Endpoint 寻址上简化了许多,只提供了最容易理解的两种方式,下面按优先级排列:

  • 用户自定义:用户可以通过在初始化时指定云产品 Client 实例的请求地址,产品的 Endpoint 可以通过OpenAPI 开发者门户的产品主页中查找,具体参考文末。

config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));

config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));

public static void main(String[] args) {
    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"));
    // 直接设置服务域名 endpoint
    config.setEndpoint("<endpoint>");
    com.aliyun.ecs20140526.Client client = new com.aliyun.ecs20140526.Client(config);
}
  • Endpoint 拼接规则:

    • 若请求的产品 SDK 具有 Endpoint 数据文件时,当前通过 RegionId 寻址的逻辑才会生效,否则会在实例化 Client 对象时抛出异常 TeaException ,其 message 为config.endpoint can not be empty

    • 若请求的产品 SDK 缺少 Endpoint 数据文件时,则只能使用用户自定义的方式指定 Endpoint。 Endpoint 数据文件示例(Ecs Endpoint Data),其中不具备的 RegionId 会通过拼接规则:${产品 Id}.${RegionId}.aliyuncs.com来进行拼接。

public static void main(String[] args) {
    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"));
    // 通过 regionId 映射到 endpoint
    config.setRegionId("<regionId>");
    com.aliyun.ecs20140526.Client client = new com.aliyun.ecs20140526.Client(config);
}

文件上传 Endpoint

若升级版本 SDK 还为用户提供了文件上传的 Endpoint 配置

  • 鉴权服务 openplatform 的地址,可以在 VPC 环境配置 VPC 地址来请求上传文件的鉴权信息。

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>");
        com.aliyun.facebody20191230.Client client = new com.aliyun.facebody20191230.Client(config);
        // 根据 regionId 设置鉴权服务的 VPC请求地址,例如杭州 openplatform-vpc.cn-hangzhou.aliyuncs.com
        client._openPlatformEndpoint="openplatform-vpc.<regionId>.aliyuncs.com";
        RuntimeOptions runtimeOptions = new RuntimeOptions();
        // 读取文件并实例化文件流
        File f = new File("test.txt");
        InputStream in =new FileInputStream(f);

        DetectBodyCountAdvanceRequest request = new DetectBodyCountAdvanceRequest();
        request.setImageURLObject(in);
        DetectBodyCountResponse resp = client.detectBodyCountAdvance(request, runtimeOptions);
        // response 包含服务端响应的 body 和 headers
        System.out.println(new Gson().toJson(resp.body));
        System.out.println(new Gson().toJson(resp.headers));
    }
}
  • 上传文件的地址,可以在 VPC 或者内网环境配置内网的地址来上传文件。

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>");
        // 设置通过 OSS 内网地址上传文件
        config.setEndpointType("internal");
        com.aliyun.facebody20191230.Client client = new com.aliyun.facebody20191230.Client(config);
        // 根据 regionId 设置鉴权服务的 VPC请求地址,例如杭州 openplatform-vpc.cn-hangzhou.aliyuncs.com
        client._openPlatformEndpoint="openplatform-vpc.<regionId>.aliyuncs.com";
        RuntimeOptions runtimeOptions = new RuntimeOptions();
        // 读取文件并实例化文件流
        File f = new File("test.txt");
        InputStream in =new FileInputStream(f);

        DetectBodyCountAdvanceRequest request = new DetectBodyCountAdvanceRequest();
        request.setImageURLObject(in);
        DetectBodyCountResponse resp = client.detectBodyCountAdvance(request, runtimeOptions);
        // response 包含服务端响应的 body 和 headers
        System.out.println(new Gson().toJson(resp.body));
        System.out.println(new Gson().toJson(resp.headers));
    }
}

附:Endpoint 查找方式

产品的 Endpoint 可以通过在平台 开发者门户 上寻找:

  1. 打开云产品主页:在首页选择相应云产品,例如 ECS:

image.png

2. 找到服务区域列表:

image.png

3. 选择相应 Region 所对应的 Endpoint,进行复制粘贴即可。

还可以这样查找

在 API 调试界面,找到对应接口,选择服务地址 Region,平台会自动生成 SDK 代码,代码中可以查看需要的 Endpoint。

image.png

  • 本页导读 (1)
文档反馈