MediaClient
Create MediaClient
MediaClient is the Java client of Media service, which provides a series of methods for the interactions between the caller and the Media service.
You can create a MediaClient by referring to the following code:
public class Sample {
public static void main(String[] args) {
String ACCESS_KEY_ID = "your-access-key-id";
String SECRET_ACCESS_KEY = "your-secret-access-key";
//Initialize a MediaClient.
BceClientConfiguration config = new BceClientConfiguration();
config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY));
MediaClient client = new MediaClient(config);
}
}
In the above codes, the variables ACCESS_KEY_ID
and SECRET_ACCESS_KEY
are allocated to the user by the system, both of which are strings to identify the user, and are used for signature authentication to access Media. Among them, ACCESS_KEY_ID corresponds to the “Access Key ID” in the console, and SECRET_ACCESS_KEY
corresponds to “Access Key Secret” in the console. Please refer to Get AK/SK.
The above method uses the default domain name as the service address of the Media. If the user needs to specify the domain name himself, he can specify it by passing in the ENDPOINT parameter.
String ACCESS_KEY_ID = "your-access-key-id";
String SECRET_ACCESS_KEY = "your-secret-access-key";
String ENDPOINT = "http://media.bj.baidubce.com";
BceClientConfiguration config = new BceClientConfiguration();
config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
config.setEndpoint(ENDPOINT);
MediaClient client = new MediaClient(config);
Note ENDPOINT parameter can only be defined by the assigned domain name containing the Region; currently, the Media only provides one region, Beijing, so the ENDPOINT supports the main domain name http://media.bj.baidubce.com
and backup domain name http://digitialmedia.bj.baidubce.com
, and other domain names will be supported with the increasing of Regions.
Configure MediaClient
If you need to configure some detailed parameters of the MediaClient, introduce into the BceClientConfiguration object when the MediaClient is constructed. BceClientConfiguration is the configuration class of Media service, and it can configure agent, maximum connection number and other parameters for the client.
Use agent
The following codes enable client to use agent to access Media service:
String ACCESS_KEY_ID = "your-access-key-id";
String SECRET_ACCESS_KEY = "your-secret-access-key";
String ENDPOINT = "http://media.bj.baidubce.com";
//Creation of BceClientConfiguration instance
BceClientConfiguration config = new BceClientConfiguration();
//Configure the agent as local 8080 port
config.setProxyHost("127.0.0.1");
config.setProxyPort(8080);
//Configure authentication key and server information
config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID,SECRET_ACCESS_KEY));
config.setEndpoint(ENDPOINT);
//Create the Media client
MediaClient client = new MediaClient(config);
Using the code segment above, all operations of client are executed as an agent via the 8080 port of 127.0.0.1 address.
For the agent with user authentication, the following code segment can be used to configure username and password:
//Creation of BceClientConfiguration instance
BceClientConfiguration config = new BceClientConfiguration();
//Configure the agent as local 8080 port
config.setProxyHost("127.0.0.1");
config.setProxyPort(8080);
//Set username and password
config.setProxyUsername("username");
config.setProxyPassword("password");
Set network parameters
You can set the basic network parameters with BceClientConfiguration.
BceClientConfiguration c****onfig = new BceClientConfiguration();
//Set the maximum number of HTTP connections to 10.
config.setMaxConnections(10);
//Set TCP connection timeout to 5,000 milliseconds
config.setConnectionTimeout(5000);
//Set the timeout for socket data transmission to 2,000 milliseconds
config.setSocketTimeout(2000);
BceClientConfigurationParameter description
All parameters that can be specified via BceClientConfiguration are listed as follows:
Parameter | Description |
---|---|
UserAgent | User agent, referring to the HTTP User-Agent header |
Protocol | Connection protocol type |
ProxyDomain | Windows domain name for access to NTLM authenticated proxy server |
ProxyHost | Proxy server host address |
ProxyPort | Proxy server port |
ProxyUsername | User name for proxy server authentication |
ProxyPassword | Password for proxy server authentication |
ProxyPreemptiveAuthenticationEnabled | Whether to set up user agent authentication |
ProxyWorkstation | Windows workstation name of the NTLM proxy server |
LocalAddress | Local address |
ConnectionTimeoutInMillis | Timeout for establishing a connection (unit: millisecond) |
SocketTimeoutInMillis | Timeout for transmitting data over open connections (unit: millisecond) |
MaxConnections | Maximum number of HTTP connections allowed to open |
RetryPolicy | Connection retry policy |
SocketBufferSizeInBytes | Socket buffer size |