Spring Boot整合腾讯云点播上传视频接口,后端详细代码及步骤!
程序员文章站
2022-07-05 08:45:27
...
一、申请腾讯云点播平台,获得相应的secretId,secretKey
在以下截图中查看自己的secretId,和secretKey
二、拿到secretId和secretKey后就可以进行开发了,本次使用的是腾讯云点播签名上传功能,只需要后端生成一个签名返回给前端,前端根据签名即可进行上传视频,前端需要引入云点播视频上传的包,根据规则进行上传,因为我是搞后端的,所以前端代码不是我写的,附上云点播前端代码规则,有需要的朋友可以进行查看
https://cloud.tencent.com/document/product/266/9239
后端生成签名代码java接口形式:
Signature工具类
/**
* @Author zhushaojie
* @Date 2020/9/15 11:27
* @Version 1.0
*/
public class Signature {
private String secretId;
private String secretKey;
private long currentTime;
private int random;
private int signValidDuration;
private static final String HMAC_ALGORITHM = "HmacSHA1";
private static final String CONTENT_CHARSET = "UTF-8";
public static byte[] byteMerger(byte[] byte1, byte[] byte2) {
byte[] byte3 = new byte[byte1.length + byte2.length];
System.arraycopy(byte1, 0, byte3, 0, byte1.length);
System.arraycopy(byte2, 0, byte3, byte1.length, byte2.length);
return byte3;
}
public String getUploadSignature() throws Exception {
String strSign;
String contextStr = "";
long endTime = (currentTime + signValidDuration);
contextStr += "secretId=" + java.net.URLEncoder.encode(secretId, "utf8");
contextStr += "¤tTimeStamp=" + currentTime;
contextStr += "&expireTime=" + endTime;
contextStr += "&random=" + random;
try {
Mac mac = Mac.getInstance(HMAC_ALGORITHM);
SecretKeySpec secretKey = new SecretKeySpec(this.secretKey.getBytes(CONTENT_CHARSET), mac.getAlgorithm());
mac.init(secretKey);
byte[] hash = mac.doFinal(contextStr.getBytes(CONTENT_CHARSET));
byte[] sigBuf = byteMerger(hash, contextStr.getBytes("utf8"));
strSign = new String(new BASE64Encoder().encode(sigBuf).getBytes());
strSign = strSign.replace(" ", "").replace("\n", "").replace("\r", "");
} catch (Exception e) {
throw e;
}
return strSign;
}
public void setSecretId(String secretId) {
this.secretId = secretId;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public void setCurrentTime(long currentTime) {
this.currentTime = currentTime;
}
public void setRandom(int random) {
this.random = random;
}
public void setSignValidDuration(int signValidDuration) {
this.signValidDuration = signValidDuration;
}
}
接口SignatureController
/**
* @Author zhushaojie
* @Date 2020/9/15 11:28
* @Version 1.0
*/
@Controller
@RequestMapping("/signature")
public class SignatureController{
@RequestMapping("getUgcUploadSign")
@ResponseBody
public OutputObject getUgcUploadSign() {
String secretId = "你的secretId";
String secretKey = "你的secretKey";
Signature sign = new Signature();
sign.setSecretId(secretId);
sign.setSecretKey(secretKey);
sign.setCurrentTime(System.currentTimeMillis() / 1000);
sign.setRandom(new Random().nextInt(java.lang.Integer.MAX_VALUE));
sign.setSignValidDuration(3600 * 24 * 2);
try {
String signature = sign.getUploadSignature();
Map<String, String> map = Maps.newHashMap();
map.put("signature", signature);
// return JsonResult.succeed(map);
return new OutputObject(ReturnCode.SUCCESS,"签名获取成功",map);
} catch (Exception e) {
e.printStackTrace();
// return JsonResult.fail("获取签名失败");
return new OutputObject(ReturnCode.FAIL,"签名获取失败",null);
}
}
}
到这里就结束了,如果有小伙伴有疑问可以在下方留言,或者关注微信公众号咨询问题!