BcmClient
BcmClient serves as the client for BCM services, offering developers various methods to interact with BCM services.
Create BcmClient
Create a new BcmClient with AK/SK
Users can refer to the following code to create a new BcmClient to access BCC 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 BcmClient
7 BcmClientConfiguration config = new BcmClientConfiguration();
8 config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY));
9 BcmClient client = new BcmClient(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](Reference/Retrieve AK and SK/How to Obtain AKSK.md).
The method above uses the default domain as the BCM service address. To use a custom domain, pass 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
4BcmClientConfiguration config = new BcmClientConfiguration();
5config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
6config.setEndpoint(ENDPOINT);
7BcmClient client = new BcmClient(config);
Note:
The ENDPOINTparameter must use region-specific domains (e.g., http://aihc.bj.baidubce.com for Beijing). If unspecified, it defaults to the Beijing regionhttp://bcm.bj.baidubce.com.
Configure HTTPS access to BCM
BCM supports HTTPS transport protocol. You can use HTTPS to access the BCM service in the BCM Java SDK in the following two ways:
-
Specify HTTPS in the endpoint:
Java1String endpoint = "https://bcm.bj.baidubce.com"; 2String ak = "ak"; 3String sk = "sk"; 4BcmClientConfiguration config = new BcmClientConfiguration(); 5config.setCredentials(new DefaultBceCredentials(ak, sk)); 6BcmClient client = new BcmClient(config); -
Configure HTTPS by calling setProtocol:
Java1String endpoint = "bcm.bj.baidubce.com"; // endpoint without protocol 2String ak = "ak"; 3String sk = "sk"; 4BcmClientConfiguration config = new BcmClientConfiguration(); 5config.setCredentials(new DefaultBceCredentials(ak, sk)); 6config.setEndpoint(ENDPOINT); 7 config.setProtocol(Protocol.HTTPS); // Defaults to HTTP if unspecified 8BcmClient client = new BcmClient(config);Plain Text1> **Note:** If the endpoint already includes a protocol, the protocol in the endpoint will take precedence, and the setProtocol() method will be ignored.Java1String endpoint = "http://bcm.bj.baidubce.com"; 2String ak = "ak"; 3String sk = "sk"; 4BcmClientConfiguration config = new BcmClientConfiguration(); 5config.setCredentials(new DefaultBceCredentials(ak, sk)); 6config.setEndpoint(ENDPOINT); 7 config.setProtocol(Protocol.HTTPS); // Invalid operation if specified in endpoint, applicable to HTTP cases 8BcmClient client = new BcmClient(config);
Configure BcmClient
If users need to configure detailed parameters for BcmClient, they can specify the BcmClientConfiguration object when creating BcmClient. BcmClientConfiguration is the configuration class for BCM services, allowing configuration of parameters like proxies or the maximum number of client connections.
Use a proxy
The following code snippet enables the client to access BCM 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 BcmClientConfiguration instance
5BcmClientConfiguration config = new BcmClientConfiguration();
6 // Configure proxy to local port 8080
7config.setProxyHost("127.0.0.1");
8config.setProxyPort(8080);
9 // Create BCM client
10config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
11config.setEndpoint(ENDPOINT);
12BcmClient client = new BcmClient(config);
All client requests will then be routed through the proxy at 127.0.0.1:8080.
For verified proxies, configure credentials:
1// Create BcmClientConfiguration instance
2BcmClientConfiguration config = new BcmClientConfiguration();
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 BcmClientConfiguration:
1BcmClientConfiguration config = new BcmClientConfiguration();
2
3 // Set maximum number of HTTP connections to 10
4config.setMaxConnections(10);
5
6 // Set TCP connection timeout to 5,000 milliseconds
7config.setConnectionTimeoutInMillis(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 BcmClientConfiguration:
| Parameters | Description |
|---|---|
| CnameEnabled | Use CNAME to access BCM resources |
| ConnectionTimeoutInMillis | Timeout for establishing TCP connections (unit: ms) |
| Credentials | Client credentials for signing HTTP requests with BCE |
| EnableHttpAsyncPut | Asynchronous put |
| Endpoint | Access a domain name |
| LocalAddress | Local address |
| MaxConnections | Maximum allowable HTTP connections |
| Protocol | Connection protocol type |
| ProxyDomain | Windows domain for NTLM-verified proxy |
| ProxyHost | Proxy server host address |
| ProxyPassword | Proxy verification password |
| ProxyPort | Proxy server port |
| ProxyPreemptiveAuthenticationEnabled | Enable user agent verification or not |
| ProxyUsername | Proxy verification username |
| ProxyWorkstation | NTLM proxy workstation name |
| Region | Region |
| RetryPolicy | Retry policy for connections |
| SocketBufferSizeInBytes | Buffer size for socket operations |
| SocketTimeoutInMillis | Timeout for socket data transmission (unit: ms) |
| StreamBufferSize | Stream file buffer size |
| UserAgent | User agent, refers to HTTP’s User-Agent header |
| RedirectsEnabled | Whether HTTP redirection is enabled. Enabled by default. |
