BccClient
BccClient is the client for the BCC service, providing developers with various methods to interact with the BCC service, including instances, disks, images, snapshots, security groups, availability zones, and more.
Create BccClient
Access BCC via AK/SK
Users can refer to the following code to create BccClient:
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 BCCClient
7 BccClientConfiguration config = new BccClientConfiguration();
8 config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY));
9 BccClient client = new BccClient(config);
10 }
11}
In the above code, the variables ACCESS_KEY_ID and SECRET_ACCESS_KEY are assigned to the user by the system, both are strings, used to identify the user and for signature verification to access BCC.
ACCESS_KEY_ID corresponds to “Access Key ID” in the console. SECRET_ACCESS_KEY corresponds to “Access Key Secret” in the console. Refer to the Guide - [ Retrieve ACCESSKEY](Reference/Retrieve AK and SK/How to Obtain AKSK.md).
The above method uses the default domain name as the BCC service address. To use a custom domain name, specify 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
4BccClientConfiguration config = new BccClientConfiguration();
5config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
6config.setEndpoint(ENDPOINT);
7BccClient client = new BccClient(config);
Note: The ENDPOINT parameter can only be defined with the specified domain name containing the region. If not specified, it defaults to the Beijing region
http://bcc.bj.baidubce.com. Baidu AI Cloud currently supports multiple regions. For more information, please refer to [Region Selection Guide](Reference/Region Selection Instructions/Region.md) and Service Domain
Configure HTTPS access to BCC
BCC supports HTTPS transport protocol. You can use HTTPS to access the BCC service in the BCC Java SDK in the following two ways:
- Specify HTTPS in the endpoint:
1String endpoint = "http://bcc.bj.baidubce.com";
2String ak = "ak";
3String sk = "sk";
4BccClientConfiguration config = new BccClientConfiguration();
5config.setCredentials(new DefaultBceCredentials(ak, sk));
6BccClient client = new BccClient(config);
- Configure HTTPS by calling setProtocol:
1String endpoint = "bcc.bj.baidubce.com"; // endpoint without protocol
2String ak = "ak";
3String sk = "sk";
4BccClientConfiguration config = new BccClientConfiguration();
5config.setCredentials(new DefaultBceCredentials(ak, sk));
6config.setEndpoint(ENDPOINT);
7 config.setProtocol(Protocol.HTTPS); // Defaults to HTTP if unspecified
8BccClient client = new BccClient(config);
Note: If the endpoint already includes a protocol, the specified protocol in the endpoint takes precedence, and the setProtocol() method will be ignored.
1String endpoint = "http://bcc.bj.baidubce.com";
2String ak = "ak";
3String sk = "sk";
4BccClientConfiguration config = new BccClientConfiguration();
5config.setCredentials(new DefaultBceCredentials(ak, sk));
6config.setEndpoint(ENDPOINT);
7 config.setProtocol(Protocol.HTTPS); // Invalid operation if specified in endpoint, applicable to HTTP cases
8BccClient client = new BccClient(config);
Configure BccClient
If users need to configure specific parameters for BccClient, they can specify the BccClientConfiguration object when creating BccClient. BccClientConfiguration is the configuration class for the BCC service, allowing users to set parameters such as proxies and the maximum number of connections for the client.
Use a proxy
The following code snippet enables the client to access BCC 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 BccClientConfiguration
5BccClientConfiguration config = new BccClientConfiguration();
6 // Configure proxy to local port 8080
7config.setProxyHost("127.0.0.1");
8config.setProxyPort(8080);
9 // Create BCC client
10config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
11config.setEndpoint(ENDPOINT);
12BccClient client = new BccClient(config);
All client operations will then route through the proxy at 127.0.0.1:8080. For verified proxies, configure credentials:
1// Create BccClientConfiguration
2BccClientConfiguration config = new BccClientConfiguration();
3 // Configure proxy to local port 8080
4config.setProxyHost("127.0.0.1");
5config.setProxyPort(8080);
6 // Set username and password
7 config.setProxyUsername(<username>); // Username
8 config.setProxyPassword(<password>); // Password
Set network parameters
Users may set the basic network parameters with BccClientConfiguration:
1BccClientConfiguration config = new BccClientConfiguration();
2 // Set maximum number of HTTP connections to 10
3config.setMaxConnections(10);
4 // Set TCP connection timeout to 5,000 milliseconds
5config.setConnectionTimeout(5000);
6 // Set timeout for Socket data transmission to 2,000 milliseconds
7config.setSocketTimeout(2000);
Parameter description
The following parameters can be configured via BccClientConfiguration:
| Parameters | Description |
|---|---|
| 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 |
