本文介绍了Node.js SDK,HLS数据加密的使用场景和使用限制,并提供了完整的代码示例。
简介
示例调用API进行创建HLS标准加密工作流。创建工作流,请参见新增媒体工作流。
示例代码
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';
/**
* Node.js环境版本为8.x及以上。
* 安装阿里云SDK npm install @alicloud/pop-core --save
* 安装Credentials工具。 npm install @alicloud/credentials
* 安装MPS SDK npm install --save @alicloud/mts20140618
*
*/
/** 管道ID 可在控制台查看 */
var pipelineId = "d7cedd984be7dd63395c*****";
/** 转码模板ID,m3u8模板,按需配置 */
var templateId = "S00000001-100020";
var ossLocation = "oss-cn-shanghai";
var inputBucket = "<input bucket name>";
var inputPath = "<input path>";
var outputBucket = "<output bucket name>";
var encryptionType = "hls-aes-128";
/** 替换为真实解密服务地址 */
var hlsKeyUri = "http://example.aliyundoc.com/decryption";
var actStart = "Act-Start";
var actEncryption = "Act-HLS-Encryption";
var actReport = "Act-Report";
export default class Client {
/** 初始化 Client */
static async createClient(accessKeyId: string, accessKeySecret: string, regionId: string): Promise<mts20140618> {
let config = new $OpenApi.Config({ });
config.accessKeyId = accessKeyId;
config.accessKeySecret = accessKeySecret;
/** 接口服务地域 */
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.AddMediaWorkflowRequest({
name: "HLSWorkFlow",
topology: JSON.stringify(createWorkflow())
});
let response = await client.addMediaWorkflow(request);
Console.log(Util.toJSONString($tea.toMap(response)));
}
}
Client.main(process.argv.slice(2));
function createWorkflow() {
var workflow:any = {};
var activities:any = {};
activities[actStart] = createStartActivity();
activities[actEncryption] = createTrancodeActivity();
activities[actReport] = createReportActivity();
workflow.Activities = activities;
workflow.Dependencies = createDependencies();
return workflow;
}
function createStartActivity() {
var startActivity:any = {};
startActivity.Name = actStart;
startActivity.Type = "Start";
startActivity.Parameters = buildStartParameters();
return startActivity;
}
function buildStartParameters() {
var parameters:any = {};
parameters.PipelineId = pipelineId;
parameters.InputFile = buildInputFile();
return parameters;
}
function buildInputFile() {
var inputFile:any = {};
inputFile.Bucket = inputBucket;
inputFile.Location = ossLocation;
inputFile.ObjectPrefix = inputPath;
return inputFile;
};
function createTrancodeActivity() {
var transcodeActivity:any = {};
transcodeActivity.Name = actEncryption;
transcodeActivity.Type = "Transcode";
transcodeActivity.Parameters = buildTranscodeParameters();
return transcodeActivity;
}
function buildTranscodeParameters() {
var transcodeParamters:any = {};
transcodeParamters.OutputBucket = outputBucket;
transcodeParamters.OutputLocation = ossLocation;
transcodeParamters.Outputs = buildOutputsConfig();
return transcodeParamters;
}
function buildOutputsConfig() {
var output:any = {};
output.ObjectRegex = actEncryption.concat('/{RunId}/{FileName}');
output.TemplateId = templateId;
output.Encryption = buildEncryption();
var outputs = new Array();
outputs.push(output);
outputs = JSON.parse(JSON.stringify(outputs));
return outputs;
}
function buildEncryption() {
var encryption:any = {};
encryption.Type = encryptionType;
encryption.KeyUri = hlsKeyUri;
// console.log("here is "+ JSON.stringify(encryption));
return encryption;
}
function createReportActivity() {
var reportActivity:any = {};
reportActivity.Name = actReport;
reportActivity.Parameters = {};
reportActivity.Type = "Report";
return reportActivity;
}
function createDependencies() {
var dependencies = {};
var subActivityOfStart = new Array();
subActivityOfStart.push(actEncryption);
subActivityOfStart = JSON.parse(JSON.stringify(subActivityOfStart));
dependencies[actStart] = subActivityOfStart;
var subActivityOfTranscode = new Array();
subActivityOfTranscode.push(actReport);
subActivityOfTranscode = JSON.parse(JSON.stringify(subActivityOfTranscode));
dependencies[actEncryption] = subActivityOfTranscode;
dependencies[actReport] = JSON.parse(JSON.stringify(new Array()));
return dependencies;
}
文档内容是否对您有帮助?