All Products
Search
Document Center

ApsaraVideo Media Processing:Initialize a client

Last Updated:Jun 12, 2024

This topic describes how to initialize a client by using ApsaraVideo Media Processing (MPS) SDK for Java.

Prerequisites

  • MPS SDK for Java is installed. For more information, see Install Alibaba Cloud 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.

DefaultProfile profile = DefaultProfile.getProfile(
             mpsRegionId,      // The ID of the region in which your MPS service is deployed.
             System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),      // The AccessKey ID of your Alibaba Cloud account.
             System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")); // The AccessKey secret of your Alibaba Cloud account.
DefaultAcsClient client = new DefaultAcsClient(profile);

Sample code:

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;

/**
 * *****   Usage notes     ******
 * The InitClient method in this example provides the demo code for initializing a client.
 * The accessKeyId and accessKeySecret parameters are required. For more information about how to obtain the parameter values, visit https://www.alibabacloud.com/help/en/apsaravideo-for-media-processing/latest/create-accesskey.
 * The mpsRegionId parameter specifies the region in which your MPS service is deployed. Make sure that the Object Storage Service (OSS) files that you process are stored in the region of your MPS service. For more information about supported regions, visit https://www.alibabacloud.com/help/en/mps/developer-reference/api-mts-2014-06-18-endpoint.
 */

public class InitClient {

    public static String mpsRegionId = "<mpsRegionId>";

    public static DefaultAcsClient initMpsClient() throws ClientException {

        DefaultProfile profile = DefaultProfile.getProfile(mpsRegionId, System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        DefaultAcsClient client = new DefaultAcsClient(profile);

        return client;
    }
}