前端
- 前端使用的
vue3
- 需要安装
vod-js-sdk-v6
<script setup>
import TcVod from "vod-js-sdk-v6";
const inputChange = (e) => {
const tcVod = new TcVod({
getSignature: getSignature,
});
const vUploader = tcVod.upload({
mediaFile: e.target.files[0],
});
vUploader.done().then((res) => {
console.log(res);
});
};
const getVodSignature = () => {
const url = "http://localhost:3000/api/video/getvod";
return fetch(url)
.then((response) => response.json())
.then((data) => data);
};
const getSignature = async () => {
const data = await getVodSignature();
return data;
};
</script>
<template>
<input type="file" @change="inputChange" />
</template>
<style scoped></style>
后端 返回签名
- 使用
nodejs express
框架 querystring
和crypto
是node
自带的模块,无需安装
const querystring = require("querystring");
const crypto = require("crypto");
exports.getvod = async (req, res) => {
const querystring = require("querystring");
const crypto = require('crypto');
const secret_id = "这里填secret_id";
const secret_key = "这里填secret_key";
const current = parseInt((new Date()).getTime() / 1000)
const expired = current + 86400;
const arg_list = {
secretId: secret_id,
currentTimeStamp: current,
expireTime: expired,
random: Math.round(Math.random() * Math.pow(2, 32))
}
const orignal = querystring.stringify(arg_list);
const orignal_buffer = new Buffer(orignal, "utf8");
const hmac = crypto.createHmac("sha1", secret_key);
const hmac_buffer = hmac.update(orignal_buffer).digest();
const signature = Buffer.concat([hmac_buffer, orignal_buffer]).toString("base64");
console.log(signature);
res.json(signature)
}
点击上传