BmrClient
Create BmrClient
BmrClient is the Java client for BMR services and provides a set of methods for interaction between the caller and the BMR services.
You can create a BmrClient with the following code:
Public class Sample {
public static void main(String[] args) {
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
// initialize a BmrClient
BceClientConfiguration config = new BceClientConfiguration();
config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY));
BmrClient client = new BmrClient(config);
}
}
In the above code, the variables ACCESS_KEY_ID
and SECRET_ACCESS_KEY
are assigned to the user by the system, and they are both strings used to identify the user and verify the signature for the access to BMR. The ACCESS_KEY_ID
corresponds to the "Access Key ID" in the console, and the SECRET_ACCESS_KEY
corresponds to the "Access Key Secret" in the console. For the acquisition, please see the “ACCESSKEY Administration”.
In the above situation, the default domain name is used as BMR's service address, and the access failure may occur. Currently, the BMR product uses the exclusive service address, which is specified by the user by importing endpoint parameters.
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 = "http://bmr.bj.baidubce.com"; // user specifies BMR’s service domain name
BceClientConfiguration config = new BceClientConfiguration();
config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
config.setEndpoint(ENDPOINT);
BmrClient client = new BmrClient(config);
Notes: ENDPOINT parameters are only defined with the specified Region-inclusive domain name; eg: Beijing Region’s endpoint domain name is
http://bmr.bj.baidubce.com
and Guangzhou Region’s endpoint domain name ishttp://bmr.gz.baidubce.com
,For more description of service domain name, please refer to service domain name.
Configure BmrClient
For the configuration of some specific parameters, the user can import the BceClientConfiguration object when constructing the BmrClient. BceClientConfiguration is used for the parameter configuration of BMR services, for example, the configuration of proxy and maximum linking number for the client.
Proxy Use
The following code allows the client to access BMR services by using the 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 = "domain-name"; // domain name specified by user
// create BceClientConfiguration instance
BceClientConfiguration config = new BceClientConfiguration();
// configuration of proxy as local 8080 port
config.setProxyHost("127.0.0.1");
config.setProxyPort(8080);
// configuration of certification secret key and server information
config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
config.setEndpoint(ENDPOINT);
// create BMR client
BmrClient client = new BmrClient(config);
The above code segment 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 verification, the following code segment is used for the configuration of user name and password:
// create BceClientConfiguration instance
BceClientConfiguration config = new BceClientConfiguration();
// configuration of proxy as local 8080 port
config.setProxyHost("127.0.0.1");
config.setProxyPort(8080);
//setup of user name and password
config.setProxyUsername("username");
config.setProxyPassword("password");
Set Network Parameters
BceClientConfiguration is used to set the basic network parameters:
BceClientConfiguration config = new BceClientConfiguration();
// set maximum HTTP connections to be 10
config.setMaxConnections(10);
// set TCP connection timeout to be 5000 milliseconds
config.setConnectionTimeout(5000);
// set Socket data transmission timeout to be 2000 milliseconds
config.setSocketTimeout(2000);
BceClientConfiguration Parameters Description
All the parameters which can be specified with BceClientConfiguration are as follows:
Parameters | Description |
---|---|
UserAgent | User agent, which is the User-Agent head of HTTP |
Protocol | Connection protocol type |
ProxyDomain | Windows domain name of the proxy server accessing NTLM verification |
ProxyHost | Host address of proxy server |
ProxyPort | Port of proxy server |
ProxyUsername | User name for verification of proxy server |
ProxyPassword | Password for verification of proxy server |
ProxyPreemptiveAuthenticationEnabled | Whether to set user proxy authentication |
ProxyWorkstation | Name of Windows work station for NTLM proxy server |
LocalAddress | Local address |
ConnectionTimeoutInMillis | Connection timeout period (in millisecond) |
SocketTimeoutInMillis | Timeout period for data transmission with established connection (in millisecond) |
MaxConnections | Maximum number of HTTP connections |
RetryPolicy | Connection retry policy |
SocketBufferSizeInBytes | Socket buffer size |