This topic describes how to initialize the client of ApsaraVideo Media Processing (MPS) SDK for Node.js 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, 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.
NoteWe 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. For more information, see Configure environment variables in Linux, macOS, and Windows.
Initialize the SDK client
Use an AccessKey pair to initialize the SDK client. Example:
let config = new OpenApi.Config({
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.
accessKeyId: process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'],
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
accessKeySecret: process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
});Sample code
The following sample code shows how to initialize the SDK client. In this example, the SearchPipeline operation is called to query MPS queues.
'use strict';
const Mts20140618 = require('@alicloud/mts20140618');
const OpenApi = require('@alicloud/openapi-client');
const Util = require('@alicloud/tea-util');
const Tea = require('@alicloud/tea-typescript');
class Client {
/**
* Use your AccessKey ID and AccessKey secret to initialize a client.
* @return Client
* @throws Exception
*/
static createClient() {
let config = new OpenApi.Config({
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured.
accessKeyId: process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'],
// Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured.
accessKeySecret: process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'],
});
config.endpoint = `mts.cn-qingdao.aliyuncs.com`;
return new Mts20140618.default(config);
}
static async main(args) {
let client = Client.createClient();
let searchPipelineRequest = new Mts20140618.SearchPipelineRequest({ });
let runtime = new Util.RuntimeOptions({ });
try {
// Write your own code to display the response of the API operation if necessary.
await client.searchPipelineWithOptions(searchPipelineRequest, runtime);
} catch (error) {
// Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for reference only.
// The error message.
console.log(error.message);
// The URL of the corresponding error diagnostics page.
console.log(error.data["Recommend"]);
Util.default.assertAsString(error.message);
}
}
}
exports.Client = Client;
Client.main(process.argv.slice(2));