All Products
Search
Document Center

ApsaraVideo Media Processing:Initialize a client

Last Updated:Aug 24, 2023

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 about how to install MPS SDK for Java, 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.

Procedure

Configure the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET.

  • Linux or macOS

Run the following commands:

export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>

Replace <access_key_id> and <access_key_secret> with your AccessKey ID and AccessKey secret.

  • Windows

    1. Create an environment variable file, add the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET to the file, and then enter your AccessKey ID and AccessKey secret.

    2. Restart the Windows operating system.

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/zh/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/zh/apsaravideo-for-media-processing/latest/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;
    }
}