Initialization
BlbClient functions as the Java client for BLB services, enabling the management of BLB resources. To initiate BLB requests using the Java SDK, you need to instantiate a BlbClient and adjust the default ConfigMap settings in the BlbClientConfiguration as needed.
Confirm Endpoint
Before configuring the Endpoint for SDK usage, please refer to the developer guide section on BLB Access Domain Name to understand Endpoint-related concepts. Baidu AI Cloud currently supports multiple regions. Please refer to Region Selection Guide.
Currently, supported regions include "Beijing," "Guangzhou," "Suzhou," "Hong Kong," "Wuhan," "Baoding," "Shanghai," and "Singapore.\
The corresponding information for the service domain name:
| Access region | Endpoint |
|---|---|
| bj | blb.bj.baidubce.com |
| gz | blb.gz.baidubce.com |
| su | blb.su.baidubce.com |
| hkg | blb.hkg.baidubce.com |
| fwh | blb.fwh.baidubce.com |
| bd | blb.bd.baidubce.com |
| fsh | blb.fsh.baidubce.com |
| sin | blb.sin.baidubce.com |
Create a BlbClient
Create a BlbClient with AK/SK
Users can refer to the following code to create a BlbClient to access BLB with AK/SK:
1public class Sample {
2public 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
6 // Initialize a BlbClient
7 BlbClientConfiguration config = new BlbClientConfiguration();
8 config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY));
9 BlbClient client = new BlbClient(config);
10 }
11}
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 retrieve them, refer to the Guide - Manage ACCESSKEY.
By default, the above method assigns the default domain as BLB’s service address. However, to specify a custom domain name, you need to include 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
4BlbClientConfiguration config = new BlbClientConfiguration();
5config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
6config.setEndpoint(ENDPOINT);
7BlbClient client = new BlbClient(config);
Note:
The ENDPOINTparameter must use region-specific domain names (e.g., http://aihc.bj.baidubce.com for Beijing). If unspecified, it defaults to the Beijing regionhttp://blb.bj.baidubce.com.
Set network parameters
Users may set the basic network parameters with BlbClientConfiguration:
1BlbClientConfiguration config = new BlbClientConfiguration();
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 BlbClientConfiguration:
| 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 |
