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
An Alibaba Cloud account is created and real-name verification is complete. To create an Alibaba Cloud account, visit the Alibaba Cloud official website. For more information about how to create an Alibaba Cloud account, see Create an Alibaba Cloud account.
- The AccessKey pair used to access MPS is prepared. For more information about how to obtain an AccessKey pair, see Obtain an AccessKey pair.Note We recommend that you use the AccessKey pair of a RAM user. For more information about how to grant permissions to a RAM user, see Create a RAM user and grant permissions to the RAM user.
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
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.
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));