1.ali/aliCloud.js
let request = require("./../request");
let crypto = require("crypto");
function randomNumbers(count) {
let num = '';
for (let i = 0; i < count; i++) {
num += Math.floor(Math.random() * 10);
}
return num;
}
function HmacSHA1(str, key) {
const hmac = crypto.createHmac('sha1', key);
hmac.update(str);
return hmac.digest('base64');
}
function AliCloudSign(options) {
this.AccessKeyId = options.AccessKeyId;
this.AccessKeySecret = options.AccessKeySecret;
};
AliCloudSign.prototype = {
sign: function (method = "GET", query = {}, body = {}) {
let date = new Date();
let commonQuery = {
Format: "json",
RegionId: "cn-shanghai",//修改为阿里对应的
Version: "2016-11-01",//版本号
AccessKeyId: this.AccessKeyId,
SignatureMethod: "HMAC-SHA1",
SignatureNonce: String(date.getTime()) + randomNumbers(4), //随机字符串
SignatureVersion: "1.0",
Timestamp: date.toISOString().replace(/\.\d{3}/, '')
};
let tempQuery = Object.assign(commonQuery, query, body);
let signQuery = {};
Object.keys(tempQuery).sort().forEach(function (k) {
signQuery[k] = tempQuery[k];
});
let str = method + "&" + encodeURIComponent("/") + "&" + encodeURIComponent(request.objToUrl(signQuery, true));
let signature = HmacSHA1(str, this.AccessKeySecret + "&");
signQuery.Signature = encodeURIComponent(signature);
return signQuery;
}
};
module.exports = AliCloudSign;
2.ali/index.js
let Request = require("./../request");
let AliCloudSign = require("./aliCloud");
function aliCloudClient(options) {
this.AccessKeyId = options.AccessKeyId;
this.AccessKeySecret = options.AccessKeySecret;
this.request = new Request({
serverUrl: options.serverUrl
});
this.aliCloudSign=new AliCloudSign({
AccessKeyId:this.AccessKeyId,
AccessKeySecret:this.AccessKeySecret
});
}
aliCloudClient.prototype = {
get: function (url = "/", query) {
return this.request.get(url, this.aliCloudSign.sign("GET", query))
}
};
module.exports = aliCloudClient;
3.写接口的 demo.js里
let AliCloudClient = require("../ali");
let aliClient=new AliCloudClient({
AccessKeyId:"AccessKeyId",
AccessKeySecret:"AccessKeySecret",
serverUrl:"http://live.aliyuncs.com"
});
await aliClient.get("/",{
Action:'AddLiveRecordVodConfig',//增加直播录制转点播配置,将录制内容保存到点播媒资库
AppName:'直播流所属应用名称',
AutoCompose:'ON',//开启自动合并
ComposeVodTranscodeGroupId:'自动合并点播转码模板组ID',
DomainName:'加速域名',
VodTranscodeGroupId:'点播转码模板组ID'
}).then(function(){
console.log('成功')
}).catch(function(err){
console.log('失败')
});