Route
Confirm Endpoint
Before configuring the endpoint for SDK usage, please refer to the developer guide section on API Service Domain Name to understand endpoint-related concepts. Baidu AI Cloud currently supports multiple regions. Please refer toRegion Selection Guide.
The currently supported region details are as follows:
| Access region | Endpoint |
|---|---|
| Beijing | bcc.bj.baidubce.com |
| Guangzhou | bcc.gz.baidubce.com |
| Suzhou | bcc.su.baidubce.com |
| Hong Kong | bcc.hkg.baidubce.com |
| Wuhan | bcc.fwh.baidubce.com |
| Baoding | bcc.bd.baidubce.com |
Retrieve access key
To use Baidu AI Cloud route, you need a valid AK (Access Key ID) and SK (Secret Access Key) for signature certification. AK/SK are system-assigned strings used to identify users and perform signature certification for route. Your AK/SK information can be obtained and understood through the following steps:
Register a Baidu AI Cloud account
Create RouteClient
The Route client acts as the client for route services, offering developers various methods to interact with those services.
Create a new route client with AK/SK Users can refer to the following code to create a new route client to access route with AK/SK:
1public class Sample {
2 public static void main(String[] args) {
3 String ACCESS_KEY_ID =<your-access-key-id>; // User’s Access Key ID
4 String SECRET_ACCESS_KEY =<your-secret-access-key>; // User’s Secret Access Key
5 // Initialize a route client
6 BceClientConfiguration config = new BceClientConfiguration();
7 config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY));
8 RouteClient client = new RouteClient(config);
9 }
10}
In the code above, ACCESS_KEY_ID corresponds to “Access Key ID” in the console. SECRET_ACCESS_KEY corresponds to “Access Key Secret” in the console. For the method to get them, refer to Guide - Manage ACCESSKEY.
This method adopts the default domain name as the service address for the route. To use a custom domain name, provide the ENDPOINT parameter.
1String ACCESS_KEY_ID =<your-access-key-id>; // User’s Access Key ID
2 String SECRET_ACCESS_KEY =<your-secret-access-key>; // User’s Secret Access Key
3 String ENDPOINT = <domain-name>; // User-defined domain name
4BceClientConfiguration config = new BceClientConfiguration();
5config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
6config.setEndpoint(ENDPOINT);
7RouteClient client = new RouteClient(config);
Note: The ENDPOINT parameter must use region-specific domain names. If not specified, it defaults to the Beijing region.
Create a route client with STS
Request STS Token
Route supports temporary third-party access authorization through the STS mechanism. STS (Security Token Service) is a temporary authorization service provided by Baidu AI Cloud. Using STS, you can issue access credentials with customized validity periods and permissions to third-party users. These users can leverage these credentials to directly call Baidu AI Cloud APIs or SDKs to access cloud resources.
To access route via STS, users must first request a certification string through the STS client. For instructions on obtaining STS credentials, refer to Baidu AI Cloud STS Usage Guide.
Create route client with STS token
After requesting the STS token, configure it in the route client. The following codes demonstrate how to create a route client:
1public class StsExample {
2 private static final String STS_ENDPOINT = "http://sts.bj.baidubce.com";
3 private static final String ACCESS_KEY_ID = "your accesskey id";
4 private static final String SECRET_ACCESS_KEY = "your secret accesskey";
5 public static void main(String[] args) {
6 BceCredentials credentials = new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY);
7 StsClient client = new StsClient(
8 new BceClientConfiguration().withEndpoint(STS_ENDPOINT).withCredentials(credentials)
9 );
10 GetSessionTokenResponse response = client.getSessionToken(new GetSessionTokenRequest());
11 // or simply call:
12 // GetSessionTokenResponse response = client.getSessionToken();
13 // or you can specify limited permissions with ACL:
14 // GetSessionTokenResponse response = client.getSessionToken(new GetSessionTokenRequest().withAcl("blabla"));
15 // build DefaultBceSessionCredentials object from response:
16 BceCredentials routestsCredentials = new DefaultBceSessionCredentials(
17 response.getAccessKeyId(),
18 response.getSecretAccessKey(),
19 response.getSessionToken());
20 System.out.println("==================================");
21 System.out.println("GetSessionToken result:");
22 System.out.println(" accessKeyId: " + response.getAccessKeyId());
23 System.out.println(" secretAccessKey: " + response.getSecretAccessKey());
24 System.out.println(" securityToken: " + response.getSessionToken());
25 System.out.println(" expiresAt: " + response.getExpiration().toString());
26 System.out.println("==================================");
27 // build Route client
28 BceClientConfiguration config = new BceClientConfiguration();
29 config.setCredentials(routestsCredentials);
30 RouteClient routeClient = new RouteClient(config);
31 }
32}
Note: When configuring a client with STS, regardless of the location of the route service endpoint, the endpoint must be set to http://sts.bj.baidubce.com.
Configure HTTPS protocol access route
Route supports HTTPS transport protocol. You can use HTTPS to access the route service in the Route Java SDK in the following two ways:
- Specify HTTPS in the endpoint:
1String endpoint = "https://bcc.bj.baidubce.com";
2String ak = "ak";
3String sk = "sk";
4BceClientConfiguration config = new BceClientConfiguration();
5config.setCredentials(new DefaultBceCredentials(ak, sk));
6RouteClient client = new RouteClient(config);
- Configure HTTPS by calling setProtocol:
1String endpoint = "bcc.bj.baidubce.com"; // endpoint without protocol
2String ak = "ak";
3String sk = "sk";
4BceClientConfiguration config = new BClientConfiguration();
5config.setCredentials(new DefaultBceCredentials(ak, sk));
6config.setEndpoint(ENDPOINT);
7 config.setProtocol(Protocol.HTTPS); // Defaults to HTTP if unspecified
8RouteClient client = new RouteClient(config);
Note: **If the endpoint already includes a protocol, the protocol within the endpoint takes precedence, and the setProtocol() method will be ignored.
1String endpoint = "https://bcc.bj.baidubce.com";
2String ak = "ak";
3String sk = "sk";
4BceClientConfiguration config = new BceClientConfiguration();
5config.setCredentials(new DefaultBceCredentials(ak, sk));
6config.setEndpoint(ENDPOINT);
7 config.setProtocol(Protocol.HTTPS); // Invalid operation if specified in endpoint, applicable to HTTP cases
8RouteClient client = new RouteClient(config);
Configure RouteClient
To configure specific parameters for the route client, users can provide the BCE client configuration object while creating the route client. The BCE client configuration class allows you to set parameters such as proxy and maximum connection count for the client.
Use a proxy
The following code snippet enables the client to access route service using a proxy:
1String ACCESS_KEY_ID =<your-access-key-id>; // User’s Access Key ID
2 String SECRET_ACCESS_KEY =<your-secret-access-key>; // User’s Secret Access Key
3 String ENDPOINT = <domain-name>; // User-defined domain name
4 // Create BceClientConfiguration instance
5BceClientConfiguration config = new BceClientConfiguration();
6 // Configure proxy to local port 8080
7config.setProxyHost("127.0.0.1");
8config.setProxyPort(8080);
9 // Create route client
10config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
11config.setEndpoint(ENDPOINT);
12RouteClient client = new RouteClient(config);
All client requests will then be routed through the proxy at 127.0.0.1:8080.
For verified proxies, configure credentials:
1// Create BceClientConfiguration instance
2BceClientConfiguration config = new BceClientConfiguration();
3
4 // Configure proxy to local port 8080
5config.setProxyHost("127.0.0.1");
6config.setProxyPort(8080);
7
8 // Set username and password
9 config.setProxyUsername(<username>); // Username
10 config.setProxyPassword(<password>); // Password
Set network parameters
Users may set the basic network parameters with BceClientConfiguration:
1BceClientConfiguration config = new BceClientConfiguration();
2
3 // Set maximum number of HTTP connections to 10
4config.setMaxConnections(10);
5
6 // Set TCP connection timeout to 5,000 milliseconds
7config.setConnectionTimeout(5000);
8
9 // Set timeout for Socket data transmission to 2,000 milliseconds
10config.setSocketTimeout(2000);
Parameter description
The following parameters can be configured via BceClientConfiguration:
| Parameters | Description |
|---|---|
| UserAgent | User agent, refers to HTTP’s User-Agent header |
| Protocol | Connection protocol type |
| ProxyDomain | Windows domain for NTLM-verified proxy |
| ProxyHost | Proxy server host address |
| ProxyPort | Proxy server port |
| ProxyUsername | Proxy verification username |
| ProxyPassword | Proxy verification password |
| ProxyPreemptiveAuthenticationEnabled | Enable user agent verification or not |
| ProxyWorkstation | NTLM proxy workstation name |
| LocalAddress | Local address |
| ConnectionTimeoutInMillis | Timeout for establishing TCP connections (unit: ms) |
| SocketTimeoutInMillis | Timeout for socket data transmission (unit: ms) |
| MaxConnections | Maximum allowable HTTP connections |
| RetryPolicy | Retry policy for connections |
| SocketBufferSizeInBytes | Buffer size for socket operations |
| StreamBufferSize | Stream file buffer size |
Query route table
Query route table. The request parameters routeTableId and vpcId cannot both be left empty.
Function declaration
1 public GetRouteResponse getRoute(String routeTableId, String vpcId) {
2 }
Parameter meaning
Refer to OpenAPI documentation: Request Parameters for Querying Route Table
Response value
- Operation succeeded
1{
2 "routeTableId": "rt-se9wn4za55dx",
3 "vpcId": "vpc-hdq0mqg68si0",
4 "routeRules": [
5 {
6 "routeRuleId": "rr-7651t0c1j546",
7 "sourceAddress": "0.0.0.0/0",
8 "destinationAddress": "172.16.0.0/16",
9 "nexthopId": "tgw-e5eecf9xevtnddga",
10 "nexthopType": "vpc2tgw",
11 "description": "",
12 "pathType": "normal",
13 "routeTableId": "rt-se9wn4za55dx"
14 }
15 ]
16}
- Operation failed
For response exception list of operation failure, refer to the Exception List.
Code example
For specific code examples, refer to ExampleListRouteRule.java
Create route rules
When creating route table rules, note the following points:
- When selecting a custom source network segment, the custom segment must fall within the range of existing subnets, excluding 0.0.0.0/0;
- The destination segment must not overlap with the current VPC CIDR (except when either is 0.0.0.0/0);
- The source and destination network segments of a new route entry must not duplicate those of any existing entries in the route table.
Function declaration
1 public CreateRouteResponse createRoute(CreateRouteRequest request){
2 }
Parameter Meaning
Refer to OpenAPI documentation: Request Parameters for Creating routing rule List
Response value
- Operation succeeded
No return
- Operation failed
For response exception list of operation failure, refer to the Exception List.
Code example
For specific code examples, refer to ExampleCreateRouteRule.java
Delete route rules
Function declaration
1 public GetRouteResponse getRoute(String routeTableId, String vpcId) {
2 }
Parameter Meaning
Refer to OpenAPI documentation: Request Parameters for Querying Route Table
Response Value
- Operation succeeded
No return
- Operation failed
For response exception list of operation failure, refer to the Exception List.
Code example
For specific code examples, refer to ExampleDeleteRouteRule.java
Update route rules
Function declaration
1 public void updateRouteRule(UpdateRouteRuleRequest updateRouteRuleRequest) {
2 }
Parameter Meaning
Refer to OpenAPI documentation: Request Parameters for Updating routing rules
Response Value
- Operation succeeded
No return
- Operation failed
For response exception list of operation failure, refer to the Exception List.
Code example
For specific code examples, refer to ExampleUpdateRouteRule.java
Query routing rules with pagination
Function declaration
1public ListRouteRuleResponse listRouteRule(ListRouteRuleReq listRouteRuleReq) {
2}
Parameter Meaning
Refer to OpenAPI documentation: Request Parameters for Querying Route Table with Pagination
Response Value
- Operation succeeded
1{
2 "metadata": For details, refer to the metadata section in the appendix,
3 "nextMarker": "rr-rbn5yyz6rtn8",
4 "marker": "rr-y43tr5disam1",
5 "maxKeys": 1,
6 "isTruncated": true,
7 "routeRules": [
8 {
9 "routeTableId": "rt-q1zg3i8mx8p6",
10 "description": "",
11 "nexthopId": "vpn-snx074964j9d",
12 "destinationAddress": "10.0.0.1/32",
13 "sourceAddress": "192.168.0.0/20",
14 "routeRuleId": "rr-dvq3cxpghw5e",
15 "nexthopType": "vpn"
16 }
17 ]
18}
- Operation failed
For response exception list of operation failure, refer to the Exception List.
Code example
For specific code examples, refer to ExampleMarkerListRouteRule.java
Active-standby route switch
Function declaration
1 public void switchRouteHa(String ruleId) {
2 }
3 public void switchRouteHa(SwitchRouteHaRequest switchRouteHaRequest) {
4 }
Parameter Meaning
Refer to OpenAPI documentation: Request Parameters for Switching Active-standby Route
Response value
- Operation succeeded
No return
- Operation failed
For response exception list of operation failure, refer to the Exception List.
Code example
For specific code examples, refer to ExampleRouteRuleSwithHa.java
Appendix
Public response information in Metadata format
1 {
2 "bceRequestId":"f5f0821d-45fe-439b-bbf4-fc48b639f84a",
3 "contentLength":28,
4 "contentType":"application/json;charset=UTF-8",
5 "date":1701917695000,
6 "server":"openresty/1.15.8.1"
7 }
