BcmClient
BcmClient is the client of BCM service, which provides a series of methods for interaction between the developers and the BCM services.
Create a BcmClient
Create a BcmClient Using AK/SK
Access BCM through AK/SK and create a BcmClient by referring to the following code:
public class Sample {
public static void main(String[] args) {
String ACCESS_KEY_ID = <your-access-key-id>; // The user’s Access Key ID
String SECRET_ACCESS_KEY = <your-secret-access-key>; // The user’s Secret Access Key
// Initialize a BcmClient
BcmClientConfiguration config = new BcmClientConfiguration ();
config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY));
BcmClient client = new BcmClient(config);
}
}
In the above codes,ACCESS_KEY_ID
corresponds to the “Access Key ID” in the console andSECRET_ACCESS_KEY
to the “Access Key Secret” in the console. For their acquisition method, see Operation Guide for ACCESSKEY Management.
As mentioned above, the user can use the default domain name as the service address of Bcm. Also, the user can designate the domain name by inputting the ENDPOINT parameter.
String ACCESS_KEY_ID = <your-access-key-id>; // The user’s Access Key ID
String SECRET_ACCESS_KEY = <your-secret-access-key>; // The user’s Secret Access Key
String ENDPOINT = <domain-name>; // Userspecified Domain Name
BcmClientConfiguration config = new BcmClientConfiguration();
config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
config.setEndpoint(ENDPOINT);
BcmClient client = new BcmClient(config);
Note: You can define the
ENDPOINT
parameter only using the assigned domain name containing the region. When no domain name is assigned, it is Beijing Regionhttp://bcm.bj.baidubce.com
by default.
Configure the HTTPS Protocol to Access BCM
The BCM supports the 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:
String endpoint = "https://bcm.bj.baidubce.com"; String ak = "ak"; String sk = "sk"; BcmClientConfiguration config = new BcmClientConfiguration (); config.setCredentials(new DefaultBceCredentials(ak, sk)); BcmClient client = new BcmClient(config);
-
Set the HTTPS protocol by calling the setProtocol:
String endpoint = "bcm.bj.baidubce.com"; // The endpoint does not contain any protocol String ak = "ak"; String sk = "sk"; BcmClientConfiguration config = new BcmClientConfiguration(); config.setCredentials(new DefaultBceCredentials(ak, sk)); config.setEndpoint(ENDPOINT); config.setProtocol(Protocol.HTTPS); // If not specified, use http BcmClient client = new BcmClient(config);
**Note: **If being specified in endpoint, the protocol is effective in endpoint.and besides, calling setProtocol () separately is inoperative.
String endpoint = "http://bcm.bj.baidubce.com"; String ak = "ak"; String sk = "sk"; BcmClientConfiguration config = new BcmClientConfiguration(); config.setCredentials(new DefaultBceCredentials(ak, sk)); config.setEndpoint(ENDPOINT); config.setProtocol(Protocol.HTTPS); // It has been indicated in the endpoint that this is an invalid operation, and the same is true for http BcmClient client = new BcmClient(config);
Configure a BcmClient
If configuring the parameters of BcmClient in detail, introduce the BcmClientConfiguration object when BcmClient is constructed. BcmClientConfiguration is the configuration of the BCM service, which can configure the proxy, maximum number of connections, and other parameters for the client.
Use a proxy
The following code segment allows the client to access the BCM service using a proxy:
String ACCESS_KEY_ID = <your-access-key-id>; // user’s Access Key ID
String SECRET_ACCESS_KEY = <your-secret-access-key>; // user’s Secret Access Key
String ENDPOINT = <domainname>; // The user-specified domain name
// Create the BcmClientConfiguration instance
BcmClientConfiguration config = new BcmClientConfiguration ();
// The configuration proxy is the local 8080 port
config.setProxyHost("127.0.0.1");
config.setProxyPort(8080);
// Create a BCM client
config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
config.setEndpoint(ENDPOINT);
BcmClient client = new BcmClient(config);
The code segment above allows the client to perform all operations by proxy through the 8080 port with the address of 127.0.0.1.
For the proxy with user authentication, you can configure its user name and password using the following code segment:
// Create a BcmClientConfiguration instance
BcmClientConfiguration config = new BcmClientConfiguration();
// The configuration proxy is the local 8080 port
config.setProxyHost("127.0.0.1");
config.setProxyPort(8080);
//Set the username and the password
config.setProxyUsername(<username>); //The user name
config.setProxyPassword(<password>); //The password
Set Network Parameters
The user can set basic network parameters using BcmClientConfiguration:
BcmClientConfiguration config = new BcmClientConfiguration();
//Set the maximum number of connections of HTTP to 10
config.setMaxConnections(10);
// Set the connection timeout of TCP to 5,000 ms
config.setConnectionTimeoutInMillis(5000);
//Set the time-out period of Socket transmission data as 2,000 ms
config.setSocketTimeout(2000);
Parameter Description
All parameters designated by BcmClientConfiguration are shown in the following table:
Parameters | Description |
---|---|
CnameEnabled | Accesses the BCM resource through cname |
ConnectionTimeoutInMillis | Connection timeout period (in millisecond) |
Credentials | The BCE credential used by the client to sign the HTTP request |
EnableHttpAsyncPut | The asynchronous put |
Endpoint | Access domain name |
LocalAddress | The local address |
MaxConnections | The maximum number of HTTP connections |
Protocol | The connection protocol type |
ProxyDomain | The Windows domain name of the proxy server for access to NTLM authentication |
ProxyHost | The proxy server host address |
ProxyPassword | The password for authentication of the proxy server |
ProxyPort | The proxy server port |
ProxyPreemptiveAuthenticationEnabled | Whether to set the user proxy authentication |
ProxyUsername | The user name for proxy server authentication |
ProxyWorkstation | The name of the Windows workstation for the NTLM proxy server |
Region | The region |
RetryPolicy | The connection retry policy |
SocketBufferSizeInBytes | Socket buffer size |
SocketTimeoutInMillis | The timeout to transfer the data via an open connection (ms) |
StreamBufferSize | The stream file buffer size |
UserAgent | The user agent, which is the UserAgent head of HTTP |
RedirectsEnabled | Whether to enable the HTTP redirection. Enabled by default |