Initialization
Confirm Endpoint
Baidu AI Cloud currently supports multiple regions. Please refer to[Region Selection Guide](Reference/Region Selection Instructions/Region.md).
Currently, only the "North China-Beijing" region is supported.
Beijing region: http://iam.bj.baidubce.com
Corresponding information:
| Access region | Endpoint |
|---|---|
| BJ | iam.bj.baidubce.com |
Retrieve access key
To use Baidu AI Cloud IAM, 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 IAM. Your AK/SK information can be obtained and understood through the following steps:
Register a Baidu AI Cloud account
Create IamClient
The IamClient acts as the client for IAM services, offering developers a range of methods to interact with IAM services.
Create a new IamClient with AK/SK
Users can refer to the following code to create an IamClient to access IAM 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
6 // Initialize an IamClient
7 IamClient client = new IamClient(ACCESS_KEY_ID, SECRET_ACCESS_KEY);
8 }
9}
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](Reference/Retrieve AK and SK/How to Obtain AKSK.md).
Configure IamClient
For detailed parameter configuration of IamClient, specify the BceClientConfiguration object when creating IamClient. BceClientConfiguration is the configuration class for IAM services, allowing settings such as proxy and maximum connection limits for the client.
Use a proxy
The following code snippet enables the client to access IAM 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 IAM client
10config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
11config.setEndpoint(ENDPOINT);
12IamClient client = new IamClient(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 |
