All Products
Search
Document Center

ApsaraVideo Media Processing:Initialize the SDK client

Last Updated:Aug 28, 2023

This topic describes how to initialize the client of ApsaraVideo Media Processing (MPS) SDK for Node.js client by using an AccessKey pair or a Security Token Service (STS) token.

Prerequisites

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 the SDK client

  • Use an AccessKey pair to initialize the SDK client. Sample code:

    /** Initialize the SDK client. */
        static async createClient(accessKeyId: string, accessKeySecret: string, regionId: string): Promise<mts20140618> {
            let config = new $OpenApi.Config({ });
            config.accessKeyId = accessKeyId;
            config.accessKeySecret = accessKeySecret;
            /** The ID of the region in which your MPS service is deployed. */
            config.regionId = "cn-shanghai";
            return new mts20140618(config);
        }
  • Use an STS token to initialize the SDK client. Sample code:

    var RPCClient = require('@alicloud/pop-core').RPCClient;
    function initMtsClient(accessKeyId, accessKeySecret, securityToken,regionId) {
        var client = new RPCClient({
            accessKeyId: accessKeyId,
            accessKeySecret: accessKeySecret,
            securityToken: securityToken,
            endpoint: 'http://mts.' + regionId + '.aliyuncs.com',
            apiVersion: '2014-06-18'
        });
        return client;
    }

Complete sample code

The following sample code describes how to initialize and use the SDK to query MPS queues:

import Console from '@alicloud/tea-console';
import OpenApi, * as $OpenApi from '@alicloud/openapi-client';
import Env from '@alicloud/darabonba-env';
import Util from '@alicloud/tea-util';
import mts20140618, * as $mts20140618 from '@alicloud/mts20140618';
import * as $tea from '@alicloud/tea-typescript';

/**
 * Install Node.js 8.x or later. 
 * Install Alibaba Cloud SDK for Node.js. npm install @alicloud/pop-core --save
 * Install the Alibaba Cloud SDK Credentials package.   npm install @alicloud/credentials
 * Install MPS SDK for Node.js. npm install --save @alicloud/mts20140618
 * 
 */
 export default class Client {

    /** Initialize the SDK client. */
    static async createClient(accessKeyId: string, accessKeySecret: string, regionId: string): Promise<mts20140618> {
        let config = new $OpenApi.Config({ });
        config.accessKeyId = accessKeyId;
        config.accessKeySecret = accessKeySecret;
        /** The ID of the region in which your MPS service is deployed. */
        config.regionId = "cn-shanghai";
        return new mts20140618(config);
    }

    static async main(args: string[]): Promise<void> {
        let client = await Client.createClient(Env.getEnv("ALIBABA_CLOUD_ACCESS_KEY_ID"), Env.getEnv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), args[0]);

        let request = new $mts20140618.SearchPipelineRequest({
            state: "All",
        });
        let response = await client.searchPipeline(request);
        Console.log(Util.toJSONString($tea.toMap(response)));
    }
}

Client.main(process.argv.slice(2));