node实现阿里云直播转点播签名

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('失败')
          });
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值