本文介紹了Node.js SDK,HLS數(shù)據(jù)加密的使用場景和使用限制,并提供了完整的代碼示例。
使用場景
- HLS標(biāo)準(zhǔn)數(shù)據(jù)加密適用于對視頻進行保護的場景,可以防止非法下載和非法傳播。
- 如果對安全有強烈需求,請開啟工作流中的數(shù)據(jù)加密。詳情請參見阿里云私有加密。
使用限制
- HLS標(biāo)準(zhǔn)數(shù)據(jù)加密需要使用提交轉(zhuǎn)碼作業(yè)接口。
- 工作流中不可使用HLS標(biāo)準(zhǔn)數(shù)據(jù)加密。
示例代碼
const base64url = require('base64-url');
var RPCClient = require('@alicloud/pop-core').RPCClient;
var REGION = "cn-shanghai"; //按需配置
var ID = "<your ID>"; //AccessKey控制臺獲取,按需配置
var KEY = "<your key>"; //AccessKey控制臺獲取,按需配置
var LOCATION = "<oss-cn-shanghai>"; //按需配置,使用的MPS服務(wù)、OSS服務(wù)和PipelineId需要是同一個地域
var INPUT_BUCKET = "<your input_bucket>"; //按需配置
var OUTPUT_BUCKET = "<your output_bucket>"; //按需配置
var PIPELINE_ID = "<your pipelineid>"; //MPS控制臺獲取,按需配置
function initMtsClient(accessKeyId, accessKeySecret, regionId) {
var client = new RPCClient({
accessKeyId: accessKeyId,
accessKeySecret: accessKeySecret,
endpoint: 'http://mts.' + regionId + '.aliyuncs.com',
apiVersion: '2014-06-18'
});
return client;
}
function DataEncryptionDemo() {
return initMtsClient(ID, KEY, REGION);
}
function getInputFile() {
var inputFile = {};
inputFile.Location = LOCATION;
inputFile.Bucket = INPUT_BUCKET;
inputFile.Object = 'result.mp4';
return inputFile;
}
function getOutputs() {
var left = '[';
var right = ']';
var output = getOutput();
var outputs = left.concat(JSON.stringify(output), right);
return outputs;
}
function getOutput() {
var output = {};
output.OutputObject = "BaseTest/hls-encryption****.m3u8";
output.TemplateId = "S00000001-100020";
output.Encryption = JSON.stringify(getEncryptionConfigs());
return output;
}
function getEncryptionConfigs() {
var encryption = {};
encryption.Type = "hls-aes-128";
encryption.Key = base64url.encode("encryptionkey123");
encryption.KeyUri = base64url.encode("http://example.aliyundoc.com/document/hls128****.key");
encryption.KeyType = "Base64";
return encryption;
}
function submitJobs() {
inputFile = getInputFile();
outputs = getOutputs();
client = DataEncryptionDemo();
client.request('SubmitJobs', {
Input: JSON.stringify(inputFile),
OutputLocation: LOCATION,
OutputBucket: OUTPUT_BUCKET,
Outputs: outputs,
PipelineId: PIPELINE_ID,
OutputLocation: LOCATION
}).then(function (response) {
console.log(JSON.stringify(response.JobResultList));
console.log(response.JobResultList.JobResult[0].Job.JobId);
}).catch(function (response) {
console.log(JSON.stringify(response));
})
}
submitJobs();