get-upload-signature.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (C) 2020 Tencent Cloud.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. 'use strict';
  17. const crypto = require('crypto');
  18. const querystring = require('querystring');
  19. const { secretId, secretKey } = require('../config');
  20. const { uploadExpires } = require('./config');
  21. /**
  22. * 获取腾讯云云点播文件上传签名
  23. * 更多信息请访问 https://cloud.tencent.com/document/product/266/9221
  24. * @return {string} 云点播文件上传签名
  25. */
  26. function getUploadSignature() {
  27. // 配置校验
  28. if (!secretId || !secretKey) {
  29. throw new Error('请云函数配置文件中配置secretId和secretKey');
  30. }
  31. if (isNaN(uploadExpires) || uploadExpires <= 0) {
  32. throw new Error('请在云函数VOD模块中配置有效的uploadExpires');
  33. }
  34. // 生成签名信息
  35. const current = Math.floor(new Date().getTime() / 1000);
  36. const args = {
  37. secretId,
  38. currentTimeStamp: current,
  39. expireTime: current + uploadExpires,
  40. random: Math.round(Math.random() * Math.pow(2, 32))
  41. };
  42. const orignal = querystring.stringify(args);
  43. const orignalBuffer = new Buffer(orignal, 'utf8');
  44. const hmacBuffer = crypto.createHmac('sha1', secretKey).update(orignalBuffer).digest();
  45. const signature = Buffer.concat([hmacBuffer, orignalBuffer]).toString('base64');
  46. return signature;
  47. }
  48. module.exports = getUploadSignature;