当前位置: 首页 > news >正文

在web应用后端接入内容审核——以腾讯云音频审核为例(Go语言示例)

腾讯云对象存储数据万象(Cloud Infinite,CI)为用户提供图片、视频、语音、文本等文件的内容安全智能审核服务,帮助用户有效识别涉黄、违法违规和广告审核,规避运营风险。本文以音频审核为例给出go语言示例代码与相应结果。

一、预备条件

  1. 获取API密钥,不可泄漏
    https://console.cloud.tencent.com/cam/capi
  2. 将SecretId与SecretKey写到环境变量

二、实现代码

package mainimport ("context""fmt""net/http""net/url""os""path/filepath""github.com/gin-gonic/gin"cos "github.com/tencentyun/cos-go-sdk-v5"
)var cosClient *cos.Clientfunc initCOSClient() error {secretID := os.Getenv("SecretId")secretKey := os.Getenv("SecretKey")if secretID == "" || secretKey == "" {return fmt.Errorf("SecretId and SecretKey environment variables are required")}bu, _ := url.Parse("https://radio-1259203851.cos.ap-shanghai.myqcloud.com")cu, _ := url.Parse("https://radio-1259203851.ci.ap-shanghai.myqcloud.com")b := &cos.BaseURL{BucketURL: bu, CIURL: cu}cosClient = cos.NewClient(b, &http.Client{Transport: &cos.AuthorizationTransport{SecretID:  secretID,SecretKey: secretKey,},})return nil
}func main() {if err := initCOSClient(); err != nil {fmt.Printf("Failed to initialize COS client: %v\n", err)fmt.Println("Please set the following environment variables:")fmt.Println("export SecretId=YOUR_SECRET_ID")fmt.Println("export SecretKey=YOUR_SECRET_KEY")os.Exit(1)}r := gin.Default()// 文件上传接口r.POST("/upload", func(c *gin.Context) {file, err := c.FormFile("file")if err != nil {c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})return}// 保存上传的文件到临时目录tempDir := "temp"if err := os.MkdirAll(tempDir, 0755); err != nil {c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})return}tempFilePath := filepath.Join(tempDir, file.Filename)if err := c.SaveUploadedFile(file, tempFilePath); err != nil {c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})return}// 上传文件到COScosKey := "audios/" + file.Filename_, err = cosClient.Object.PutFromFile(context.Background(), cosKey, tempFilePath, nil)if err != nil {c.JSON(http.StatusInternalServerError, gin.H{"error":   err.Error(),"message": "Failed to upload file to COS",})return}// 提交审核任务opt := &cos.PutAudioAuditingJobOptions{InputObject: cosKey,Conf:        &cos.AudioAuditingJobConf{},}res, _, err := cosClient.CI.PutAudioAuditingJob(context.Background(), opt)if err != nil {c.JSON(http.StatusInternalServerError, gin.H{"error":   err.Error(),"message": "Failed to submit audio auditing job",})return}// 清理临时文件os.Remove(tempFilePath)// 返回任务IDc.JSON(http.StatusOK, gin.H{"job_id":  res.JobsDetail.JobId,"cos_key": cosKey,})})// 查询审核结果接口r.GET("/result/:job_id", func(c *gin.Context) {jobID := c.Param("job_id")// 查询审核结果res, _, err := cosClient.CI.GetAudioAuditingJob(context.Background(), jobID)if err != nil {c.JSON(http.StatusInternalServerError, gin.H{"error":   err.Error(),"message": "Failed to get audio auditing result",})return}c.JSON(http.StatusOK, res)})// 启动服务器r.Run(":8080")
}

三、调用方法

  1. 上传文件
    使用postman,调用以下接口:
http://localhost:8080/upload

在这里插入图片描述

  1. 查询任务
    在这里插入图片描述
    查询任务的结果说明可参考:查询音频审核任务结果,需要重点关注的是result,该字段表示本次判定的审核结果,可以根据该结果,进行后续的操作。有效值:0(审核正常),1 (判定为违规敏感文件),2(疑似敏感,建议人工复核)。

相关文章:

  • Spark Mllib 机器学习
  • 将你的本地项目发布到 GitHub (新手指南)
  • Unreal Engine 实现软件测试方案的仿真体验
  • 【大模型】图像生成 - Stable Diffusion 深度解析:原理、应用与实战指南
  • R语言操作n
  • 数据集下载(AER 和causaldata R包)
  • Stable Diffusion 技术全景解析与行业竞争力分析
  • DAY8-GDB调试及打桩
  • 相机DreamCamera2录像模式适配尺寸
  • 知识体系_数据量纲化处理方式
  • 详细图解 Path-SAM2: Transfer SAM2 for digital pathology semantic segmentation
  • 计算机网络-运输层(1)
  • 2025.4.27_C_Struct,Enum,Union
  • 如何通过OKR管理项目目标
  • 【第三十三周】BLIP论文阅读笔记
  • Flink02-学习-套接字分词
  • Ldap高效数据同步- MirrorMode双主复制模式配置详解(上)
  • 在 Cursor 中 配置 GitHub MCP Server
  • AI 应用同质化:一场看不见的资源 “吞噬战”
  • 软考:软件设计师考试数据结构知识点详解
  • 暴涨96%!一季度“中国游中国购”持续升温,还有更多利好
  • 51岁国家移民管理局移民事务服务中心联络部副主任林艺聪逝世
  • 六朝文物草连空——丹阳句容南朝石刻考察纪
  • 苏迪曼杯即将在厦门打响,国羽向创纪录的14冠进军
  • 保时捷中国研发中心落户上海虹桥商务区,计划下半年投入运营
  • 蚂蚁财富28亿港元要约收购耀才证券,筹谋香港券商牌照