This topic describes how to initialize a client of ApsaraVideo Media Processing (MPS) SDK for Java V2.0.
Prerequisites
MPS SDK for Java V2.0 is installed. For more information, see Install MPS SDK for Java.
The region ID of your MPS service is obtained. For example, if your MPS service is deployed in the China (Shanghai) region, the region ID is
cn-shanghai
. For more information about the regions in which MPS is available, see Endpoints.An AccessKey pair is obtained. This ensures that you can complete identity verification when you initialize the client. For more information about how to obtain an AccessKey pair, see Create an AccessKey pair.
Obtain an AccessKey pair from environment variables
You can define the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET to configure the default credentials. When you call an API operation, the system reads the AccessKey pair from the default credentials and uses the AccessKey pair to complete authentication. For more information, see Configure environment variables in Linux, macOS, and Windows.
Initialize a client
Create a client to send requests.
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
// The endpoint of MPS that you want to access.
config.endpoint = "mts.cn-hangzhou.aliyuncs.com";
Sample code:
package com.aliyun.sample;
public class Sample {
/**
* <b>description</b> :
* <p>Use your AccessKey ID and AccessKey secret to initialize the client.</p>
* @return Client
*
* @throws Exception
*/
public static com.aliyun.mts20140618.Client createClient() throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.endpoint = "mts.cn-shanghai.aliyuncs.com";
return new com.aliyun.mts20140618.Client(config);
}
}