Java对接数据万象API实现视频查重、视频相似度校验处理

使用依赖

            <dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.13</version></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.1</version></dependency><dependency><groupId>com.qcloud</groupId><artifactId>cos_api</artifactId><version>5.6.36</version></dependency>

视频DNA校验签名

1、腾讯官方提供的签名工具类

package com.hongmeng.util.tencent;import static com.qcloud.cos.auth.COSSignerConstants.LINE_SEPARATOR;
import static com.qcloud.cos.auth.COSSignerConstants.Q_AK;
import static com.qcloud.cos.auth.COSSignerConstants.Q_HEADER_LIST;
import static com.qcloud.cos.auth.COSSignerConstants.Q_KEY_TIME;
import static com.qcloud.cos.auth.COSSignerConstants.Q_SIGNATURE;
import static com.qcloud.cos.auth.COSSignerConstants.Q_SIGN_ALGORITHM_KEY;
import static com.qcloud.cos.auth.COSSignerConstants.Q_SIGN_ALGORITHM_VALUE;
import static com.qcloud.cos.auth.COSSignerConstants.Q_SIGN_TIME;
import static com.qcloud.cos.auth.COSSignerConstants.Q_URL_PARAM_LIST;import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;import com.qcloud.cos.Headers;
import com.qcloud.cos.auth.AnonymousCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.auth.COSSessionCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.http.CosHttpRequest;
import com.qcloud.cos.http.HttpMethodName;
import com.qcloud.cos.internal.CosServiceRequest;
import com.qcloud.cos.utils.UrlEncoderUtils;import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.HmacUtils;/*** @author zym* date:  2023-10-18 15:44*/
public class COSSigner {private static Set<String> needSignedHeaderSet = new HashSet<>();private Boolean isCIWorkflowRequest = false;// Time offset between local and serverprivate int localTimeDelta = 0;static {needSignedHeaderSet.add("cache-control");needSignedHeaderSet.add("content-disposition");needSignedHeaderSet.add("content-encoding");needSignedHeaderSet.add("content-length");needSignedHeaderSet.add("content-md5");needSignedHeaderSet.add("content-type");needSignedHeaderSet.add("expect");needSignedHeaderSet.add("expires");needSignedHeaderSet.add("host");needSignedHeaderSet.add("if-match");needSignedHeaderSet.add("if-modified-since");needSignedHeaderSet.add("if-none-match");needSignedHeaderSet.add("if-unmodified-since");needSignedHeaderSet.add("origin");needSignedHeaderSet.add("range");needSignedHeaderSet.add("transfer-encoding");}private boolean isAnonymous(COSCredentials cred) {return cred instanceof AnonymousCOSCredentials;}public <X extends CosServiceRequest> void sign(CosHttpRequest<X> request, COSCredentials cred, Date expiredTime) {if (isAnonymous(cred)) {return;}String authoriationStr =buildAuthorizationStr(request.getHttpMethod(), request.getResourcePath(),request.getHeaders(), request.getParameters(), cred, expiredTime, true);request.addHeader(Headers.COS_AUTHORIZATION, authoriationStr);if (cred instanceof COSSessionCredentials) {request.addHeader(Headers.SECURITY_TOKEN,((COSSessionCredentials) cred).getSessionToken());}}public String buildPostObjectSignature(String secretKey, String keyTime, String policy) {String signKey = HmacUtils.hmacSha1Hex(secretKey, keyTime);String stringToSign = DigestUtils.sha1Hex(policy);return HmacUtils.hmacSha1Hex(signKey, stringToSign);}public String buildAuthorizationStr(HttpMethodName methodName, String resouce_path,COSCredentials cred,Date expiredTime) {Date startTime = new Date();return buildAuthorizationStr(methodName, resouce_path, new HashMap<>(), new HashMap<>(),cred, startTime, expiredTime, true);}public String buildAuthorizationStr(HttpMethodName methodName, String resouce_path,Map<String, String> headerMap, Map<String, String> paramMap, COSCredentials cred,Date expiredTime) {Date startTime = new Date();return buildAuthorizationStr(methodName, resouce_path, headerMap, paramMap,cred, startTime, expiredTime,true);}public String buildAuthorizationStr(HttpMethodName methodName, String resouce_path,Map<String, String> headerMap, Map<String, String> paramMap, COSCredentials cred,Date expiredTime, Boolean signHost) {Date startTime = new Date();return buildAuthorizationStr(methodName, resouce_path, headerMap, paramMap,cred, startTime, expiredTime, signHost);}public String buildAuthorizationStr(HttpMethodName methodName, String resouce_path,Map<String, String> headerMap, Map<String, String> paramMap, COSCredentials cred,Date startTime, Date expiredTime, Boolean signHost) {if (isAnonymous(cred)) {return null;}//万象工作流接口会出现uri带问号的情况 例如 /workflow/xxxxxx?active 这种情况?后面的参数不参与鉴权if (isCIWorkflowRequest){resouce_path = resouce_path.split("\\?")[0];}Map<String, String> signHeaders = buildSignHeaders(headerMap, signHost);// 签名中的参数和http 头部 都要进行字符串排序//对请求中的参数和http头部进行处理:对key先urlencode再小写处理,对value进行urlencode处理;//生成 key 到 value 的映射 Map,根据key按照字典序排序TreeMap<String, String> encodedSortedSignHeaders = buildEncodeSortedMemberMap(signHeaders);TreeMap<String, String> encodedSortedParams = buildEncodeSortedMemberMap(paramMap);//生成keylistString qHeaderListStr = buildSignMemberStr(encodedSortedSignHeaders);String qUrlParamListStr = buildSignMemberStr(encodedSortedParams);String qKeyTimeStr, qSignTimeStr;qKeyTimeStr = qSignTimeStr = buildTimeStr(startTime, expiredTime);String signKey = HmacUtils.hmacSha1Hex(cred.getCOSSecretKey(), qKeyTimeStr);String formatMethod = methodName.toString().toLowerCase();String formatUri = resouce_path;String formatParameters = formatMapToStr(encodedSortedParams);String formatHeaders = formatMapToStr(encodedSortedSignHeaders);String formatStr = new StringBuilder().append(formatMethod).append(LINE_SEPARATOR).append(formatUri).append(LINE_SEPARATOR).append(formatParameters).append(LINE_SEPARATOR).append(formatHeaders).append(LINE_SEPARATOR).toString();String hashFormatStr = DigestUtils.sha1Hex(formatStr);String stringToSign = new StringBuilder().append(Q_SIGN_ALGORITHM_VALUE).append(LINE_SEPARATOR).append(qSignTimeStr).append(LINE_SEPARATOR).append(hashFormatStr).append(LINE_SEPARATOR).toString();String signature = HmacUtils.hmacSha1Hex(signKey, stringToSign);String authoriationStr = new StringBuilder().append(Q_SIGN_ALGORITHM_KEY).append("=").append(Q_SIGN_ALGORITHM_VALUE).append("&").append(Q_AK).append("=").append(cred.getCOSAccessKeyId()).append("&").append(Q_SIGN_TIME).append("=").append(qSignTimeStr).append("&").append(Q_KEY_TIME).append("=").append(qKeyTimeStr).append("&").append(Q_HEADER_LIST).append("=").append(qHeaderListStr).append("&").append(Q_URL_PARAM_LIST).append("=").append(qUrlParamListStr).append("&").append(Q_SIGNATURE).append("=").append(signature).toString();return authoriationStr;}public boolean needSignedHeader(String header) {return needSignedHeaderSet.contains(header) || header.startsWith("x-cos-");}private Map<String, String> buildSignHeaders(Map<String, String> originHeaders, Boolean signHost) {Boolean hasHost = false;Map<String, String> signHeaders = new HashMap<>();for (Entry<String, String> headerEntry : originHeaders.entrySet()) {String key = headerEntry.getKey().toLowerCase();if (key.equals("host")) {hasHost = true;}if(needSignedHeader(key)) {String value = headerEntry.getValue();signHeaders.put(key, value);}}if (!hasHost && signHost) {String msg = String.format("buildAuthorization missing header: host. %s", originHeaders);throw new CosClientException(msg);}return signHeaders;}private TreeMap<String, String> buildEncodeSortedMemberMap(Map<String, String> signElements){TreeMap<String, String> encodeSortedSignElements = new TreeMap<>();for (Entry<String, String> header : signElements.entrySet()) {if (header.getKey() == null) {continue;}String encodeLowerKey = UrlEncoderUtils.encode(header.getKey().trim()).toLowerCase();String value = "";if (header.getValue()!=null){value = header.getValue().trim();}String encodeValue = UrlEncoderUtils.encode(value);encodeSortedSignElements.put(encodeLowerKey, encodeValue);}return encodeSortedSignElements;}private String buildSignMemberStr(Map<String, String> signHeaders) {StringBuilder strBuilder = new StringBuilder();boolean seenOne = false;for (String key : signHeaders.keySet()) {if (!seenOne) {seenOne = true;} else {strBuilder.append(";");}strBuilder.append(key);}return strBuilder.toString();}private String formatMapToStr(Map<String, String> kVMap) {StringBuilder strBuilder = new StringBuilder();boolean seeOne = false;for (Entry<String, String> entry : kVMap.entrySet()) {String key = entry.getKey();String value = entry.getValue();if (!seeOne) {seeOne = true;} else {strBuilder.append("&");}strBuilder.append(key).append("=").append(value);}return strBuilder.toString();}private String buildTimeStr(Date startTime, Date endTime) {StringBuilder strBuilder = new StringBuilder();long startTimestamp = startTime.getTime() / 1000 + localTimeDelta;long endTimestamp = endTime.getTime() / 1000 + localTimeDelta;strBuilder.append(startTimestamp).append(";").append(endTimestamp);return strBuilder.toString();}public static Set<String> getNeedSignedHeaderSet() {return needSignedHeaderSet;}public static void setNeedSignedHeaderSet(Set<String> needSignedHeaderSet) {COSSigner.needSignedHeaderSet = needSignedHeaderSet;}public void setCIWorkflowRequest(Boolean CIRequest) {isCIWorkflowRequest = CIRequest;}public int getLocalTimeDelta() {return localTimeDelta;}public void setLocalTimeDelta(int localTimeDelta) {this.localTimeDelta = localTimeDelta;}
}

2、基于腾讯云提供的官方签名方法,做一个适应于自己业务的方法封装

    /*** 获取一个临时签名* https://cloud.tencent.com/document/product/436/35217#.E7.94.9F.E6.88.90.E7.AD.BE.E5.90.8D** @param methodName 请求的 HTTP 方法* @param path       需要访问的路径* @param params     本次请求参数* @param headers    填写本次请求的头部*/public static String getSigner(HttpMethodName methodName, String path, Map<String, String> params, Map<String, String> headers) {// 这里需要已经获取到临时密钥的结果。JSONObject credentials = getSnapToken().getJSONObject("credentials");String sessionToken = credentials.getString("sessionToken");//腾讯云cos中bucket对应secretId与secretKeyCOSCredentials cred = new BasicSessionCredentials(secretId, secretKey, sessionToken);//若key不是以“/”开头,则需要在 key 的开头加上“/”,否则直接 resource_path=keyString resourcePath = path.startsWith("/") ? path : "/" + path;// 用来生成签名COSSigner signer = new COSSigner();// 这里设置签名在半个小时后过期Date expirationDate = new Date(System.currentTimeMillis() + 30L * 60L * 1000L);//获取签名return signer.buildAuthorizationStr(methodName, resourcePath, headers, params, cred, expirationDate, true);}

视频DNA相关API

public class CosDNATest {private final String host = "https://ad-zym-test.ci.ap-guangzhou.myqcloud.com";//基于HttpClient依赖进行封装的工具类private final HttpClientUtil instance = HttpClientUtil.getInstance();/*** 提交一个视频DNA任务*/@Testpublic void commitVideoDNA() {//存储在cos中的视频keyString key = "2023-10-12/8d00c3344784276dd9b3e19447638c57.mp4";String reqStr = "<Request>" +"    <Tag>DNA</Tag>" +"    <Input>" +"        <Object>" + key + "</Object>" +"    </Input>" +"    <Operation>" +"        <DnaConfig>" +"            <RuleType>GetFingerPrint</RuleType>" +"            <DnaDbId>dev_238</DnaDbId>" +"        </DnaConfig>" +"        <UserData>zym</UserData>" +"        <JobLevel>0</JobLevel>" +"    </Operation>" +"    <CallBackType>Url</CallBackType>" +"    <CallBack>http://adtech.test.link.hmgreat.com:8089/dnaCallBack</CallBack>" +"    <CallBackFormat>JSON</CallBackFormat>" +"</Request>";Map<String, String> headers = new HashMap<>();headers.put("host", "ad-zym-test.ci.ap-guangzhou.myqcloud.com");//获取本次任务提交的签名String signer = TencentCosUtil.getSigner(HttpMethodName.POST, "/jobs", Collections.emptyMap(), headers);headers.put("Content-Type", "application/xml");headers.put("Authorization", signer);String xmlBody = instance.postHttpResponseXmlBody(host + "/jobs", headers, reqStr);//将XML转化为JSONJSONObject body = XML.toJSONObject(xmlBody);System.out.println("body = " + body);}/*** 获取视频DNA提交任务后的信息,如任务处理状态*/@Testpublic void getVideoDNAJobInfo() {String tempUrl = host + "/jobs/j5894a76c6e2311eeb97b6d08ebe82b8f";Map<String, String> headers = new HashMap<>();headers.put("host", "ad-zym-test.ci.ap-guangzhou.myqcloud.com");String signer = TencentCosUtil.getSigner(HttpMethodName.GET, "/jobs/j5894a76c6e2311eeb97b6d08ebe82b8f", Collections.emptyMap(), headers);headers.put("Authorization", signer);String responseBody = instance.getHttpResponseBody(tempUrl, headers);System.out.println("responseBody = " + responseBody);}/*** 查询cos中对应bucket所关联的DNA库*/@Testpublic void getDnaDBList() {String tempUrl = host + "/dnadb";Map<String, String> headers = new HashMap<>();headers.put("host", "ad-zym-test.ci.ap-guangzhou.myqcloud.com");String signer = TencentCosUtil.getSigner(HttpMethodName.GET, "/dnadb", Collections.emptyMap(), headers);headers.put("Authorization", signer);String responseBody = instance.getHttpResponseBody(tempUrl, headers);System.out.println("responseBody = " + responseBody);}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/126902.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一台服务器安装两个mysql、重置数据库用于测试使用

文章目录 一、切数据库数据存储文件夹已经存在数据库数据文件夹新建数据库数据文件夹 二、安装第二个mysql安装新数据库初始化数据库数据启动数据库关闭数据库 三、mysqld_multi单机多实例部署参考文档 一、切数据库数据存储文件夹 这个方法可以让你不用安装新的数据库&#x…

kubernetes存储-configmap

一、字面值创建 二、通过文件创建 三、通过目录创建 四、通过yaml文件创建 五、使用configmap设置环境变量 六、使用conigmap设置命令行参数 七、通过数据卷使用configmap 八、configmap热更新

10.30寄存器,寄存器堆

寄存器 8位环形移位寄存器 module shift_regist (input wire clk,input wire rstn,input wire [7:0]D,output reg [7:0]Q ); always (posedge clk or negedge rstn) beginif(!rstn)Q<8b000000;elseQ<{D[6:0],D[7]} ; end endmodule //shift_regist 输入有时钟…

Springboot引入mybatis-plus及操作mysql的json字段

springboot引入mybatis-plus&#xff0c;创建springboot项目省略 pom文件 <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.3.4</version></dependency> <!…

【Verilog 教程】7.3 Verilog 串行 FIR 滤波器设计

串行 FIR 滤波器设计 设计说明 设计参数不变&#xff0c;与并行 FIR 滤波器参数一致。即&#xff0c;输入频率为 7.5 MHz 和 250 KHz 的正弦波混合信号&#xff0c;经过 FIR 滤波器后&#xff0c;高频信号 7.5MHz 被滤除&#xff0c;只保留 250KMHz 的信号。 输入频率&#x…

关于构建springboot2.x出现报错镜像找不到springboot包的问题

报错原因 今天第一次学习springboot出现了报错&#xff0c;在2023.10.31构建springboot时springboot2只能够选择2.7.17版本&#xff0c;在JDK选择了与之匹配的1.8以后仍旧出现报错org.springframework.boot:spring-boot-starter-parent:pom:2.7.17.RELEASE was not found in h…

【Linux】安装使用Nginx负载均衡,并且部署前端项目

目录 一、Nginx概述 1. 什么 2. 背景 3. 作用 二、Nginx负载均衡 1. 讲述 2. 使用 1. 下载 2. 安装 3. 负载均衡 三、前端部署 1. 准备 2. 部署 一、Nginx概述 1. 什么 Nginx是一个高性能的开源Web服务器和反向代理服务器。它具有轻量级、高并发、低内存消耗的…

Jupyter Notebook的安装方法以及生成ipykernel

1. 安装Python和pip Jupyter Notebook是基于Python的&#xff0c;因此首先需要在电脑上安装Python和pip。可以通过访问Python官网(Welcome to Python.org)下载安装包进行安装。在安装Python的过程中&#xff0c;会提示是否安装pip&#xff0c;选择安装即可。 2. 安装Jupyter …

一文深入了解 CPU 的型号、代际架构与微架构

在 10 月 16 号的时候&#xff0c;Intel 正式发布了第 14 代的酷睿处理器。但还有很多同学看不懂这种发布会上发布的各种 CPU 参数。借着这个时机&#xff0c;给大家深入地讲讲 CPU 的型号规则、代际架构与微架构方面的知识。 CPU 在整个计算机硬件中、技术体系中都算是最最重…

18.自监督视觉`transformer`模型DINO

文章目录 自监督视觉`transformer`模型DINO总体介绍DINO中使用的SSL和KD方法multicrop strategy损失函数定义`teacher`输出的中心化与锐化模型总体结构及应用reference欢迎访问个人网络日志🌹🌹知行空间🌹🌹 自监督视觉transformer模型DINO 总体介绍 论文:1.Emerging …

京东协议算法最新版

环境准备 1 com.jingdong.app.mall11.6.4 入口定位 逆向分析&#xff0c;发现 params 里面有一个 sign 以及请求头里面有一个 jdgs 首先我们发现京东的 sign 是 32 位的&#xff0c;猜测其可能是 md5 之类的 hash 算法&#xff0c;既然是 hash 算法&#xff0c;那么就大概率…

Test-Agent----基于Centos7系统部署Test-Agent

【Test-Agent----基于Centos7系统部署Test-Agent】 一、部署 Test-Agent 1.1 环境准备 环境基本信息&#xff1a;Centos7.9操作系统&#xff0c;16核64G1T硬盘&#xff0c;Python3.9.7 1.2 部署 Test-Agent &#xff08;1&#xff09;安装git-lfs cd /opt curl -s https:…

LuatOS-SOC接口文档(air780E)--mobile - 蜂窝网络

示例 -- 简单演示log.info("imei", mobile.imei()) log.info("imsi", mobile.imsi()) local sn mobile.sn() if sn thenlog.info("sn", sn:toHex()) end log.info("muid", mobile.muid()) log.info("iccid", mobile.icc…

ORANGE室内高尔夫—韩国室内模拟高尔夫原装进口真实体验身临其境

ORANGE室内高尔夫—韩国室内模拟高尔夫 真实体验 身临其境 室内高尔夫的产品优势&#xff1a; 1. 实际高尔夫球场的限制&#xff1a;室内高尔夫可以弥补室外高尔夫球场数量有限的问题&#xff0c;使得更多人能够享受高尔夫运动。 2. 天气和季节的限制&#xff1a;室内高尔夫可…

centos7 部署 Flink

1. 准备 安装的前提是虚拟机里已安装了jdk 去官网下载 Flink 所有版本下载地址&#xff1a;https://archive.apache.org/dist/flink/ 找到下图的安装包&#xff0c;下载即可 下载完后&#xff0c;将其上传至虚拟机的某个地方&#xff0c;本人将其放在 /home/flink/ 下 解压…

【Apache Flink】Flink DataStream API的基本使用

Flink DataStream API的基本使用 文章目录 前言1. 基本使用方法2. 核心示例代码3. 完成工程代码pom.xmlWordCountExample测试验证 4. Stream 执行环境5. 参考文档 前言 Flink DataStream API主要用于处理无界和有界数据流 。 无界数据流是一个持续生成数据的数据源&#xff0…

基于MFC的串口通信(Mscomm)

1、串口通信的概述&#xff1a; 串口是一种重要的通信资源&#xff0c;例如鼠标口、USB接口都是串口。串行端口是CPU和串行设备间的编码转换器。当数据从CPU经过端口发送出去的时候&#xff0c;字节数据会被转为串行的位&#xff0c;在接收数据时&#xff0c;串行的位被转换为…

机器人仿真-gazebo学习笔记(4)xacro和传感器添加

1.xacro简介 URDF文件不具备代码复用的特性&#xff08;在上一篇文章也能发现&#xff0c;其实左右轮是极其相似的但还是要单独描述&#xff09;&#xff0c;一个复杂的机器人模型会拥有大量了的传感器和关节组件&#xff0c;这时候使用URDF文件就太难阅读了。精简化、可复用、…

学习视频剪辑:如何从指定时段快速抽出视频图片!高效技巧分享

随着数字媒体的普及&#xff0c;越来越多的人开始接触视频剪辑。在视频剪辑过程中&#xff0c;有时候我们需要从指定时段快速抽出视频图片。这不仅可以帮助我们提高剪辑效率&#xff0c;还可以让我们的视频更加丰富多彩。本文将分享一些高效技巧&#xff0c;帮助你轻松实现从指…

Vue路由(router)的安装和使用

Vue路由&#xff08;router&#xff09;的安装和使用 安装vue-router插件 第一步&#xff1a;在CMD窗口中&#xff0c;使用命令跳转到vue的安装路径下第二步&#xff1a;输入命令&#xff1a;npm i vue-router3 vue2 要安装 vue-router3 npm i vue-router3 vu3 要安装 vue-ro…