You need to enable JavaScript to run this app.
导航
接口文档-同步任务
最近更新时间:2023.07.05 15:29:49首次发布时间:2023.07.04 20:23:17

本文档意在向用户说明如何在创建【同步任务】后,通过调用平台接口【提交图片数据】

1. 名词解析
AK&SK火山引擎账号API访问控制秘钥
TOP网关服务,通过TOP网关请求此接口
2. 提交图片接口
请求地址https://open.volcengineapi.com?Action=AddSyncTaskData&Version=2022-05-13
请求方法POST
2.1 Request header
参数类别是否必填描述
Regionstringcn-north-1
Servicestringiva
Content-Typestringmultipart/form-data
2.2 Request body
  • 注意此处因涉及文件上传,body格式不再是【json】而是【multipart/form-data】
参数类别是否必填描述
RequeststringJson 格式字符串,具体内容见下文
ImageFileFILE任务图片数据,单张图片大小不超过50M
2.2.1 Request
参数类别是否必填描述
TaskIDstring任务ID
ImageimageInfo结构体,见下表
AlgoParamConfmap[string]interface{}自定义算法参数说明
2.2.2 imageInfo
参数类别是否必填描述
Namestring图片名称

Source

string

枚举:url、file
url:图片url,从Url字段中获取图片
file:表单上传图片文件,为此字段时,从表单File中获取图片

Urlstring图片url
Descstring图片描述
2.3 Response body
{
    "ResponseMetadata": {
        "RequestId": "20220214145936010211209131054BC6F2",
        "Action": "{Action}",
        "Version": "{Version}",
        "Service": "{Service}",
        "Region": "{Region}",
        "Error": { // 优先检测此字段,请求参数校验不通过时,会有相应信息
            "Code": "{ErrorCode}",
            "Message": "{ErrorMessage}"
        }
    },
    "Result":{
        "ImageID":"xxxx-xxxx",
        "Copywriting":{"1","2"}
        "RawData":"{}" // 算法处理结果,json序列化后的字符串,具体结构与任务算法有关
    }
}
3. 接口请求Demo
package main

import (
   "bytes"
   "encoding/json"
   "fmt"
   "github.com/volcengine/volc-sdk-golang/base"
   "io"
   "io/ioutil"
   "mime/multipart"
   "net/http"
   "net/url"
   "os"
)

var (
    ak string
    sk string
)

fumc main() {

    ak = "your_volc_ak"
    sk = "your_volc_sk"
    
    imgFile, err := os.Open("tesst.png") // 本地图片文件
    if err != nil {
        panic(err)
    }
    
    // 通过文件上传
    addSyncTaskData(&addImageDataReq{
        TaskID: "xxx-xxxx-xxxx", // 填写创建同步任务返回的任务ID
        Image: imageInfo{
            Name: "xxxx",
            Source: "file",
            Desc: "desc"
        },
    },imgFile)
    
    // 通过url上传
    addSyncTaskData(&addImageDataReq{
        TaskID: "xxx-xxxx-xxxx", // 填写创建同步任务返回的任务ID
        Image: imageInfo{
            Name: "xxxx",
            Source: "url",
            Url:"https://xxx.xxx.xx/xxx.jpeg" // 图片文件url
            Desc: "desc"
        },
    },nil)
}

type imgInfo struct {
   Name   string `json:"Name" vd:"@:len($)>0; msg:'image name can not be null'"`
   Source string `json:"Source"`
   Url    string `json:"Url"`
   Desc   string `json:"Desc"`
}

type addImageDataReq struct {
   TaskID        string                 `json:"TaskID,required" vd:"@:len($)>0; msg:'task id can not be null'"`
   Image         imgInfo                `json:"Image,required"`
   AlgoParamConf map[string]interface{} `json:"AlgoParamConf"`
}

type addImageDataResult struct {
   ImageID     string   `json:"ImageID"`
   RawData     string   `json:"RawData"`
   Copywriting []string `json:"Copywriting"`
}

type addImageDataResp struct {
   ResponseMetadata baseResp           `json:"ResponseMetadata"`
   Result           addImageDataResult `json:"Result"`
}

func addSyncTaskData(aidr *addImageDataReq, file *os.File) {
   query := url.Values{
      "Action":  []string{"AddSyncTaskData"},
      "Version": []string{"2022-05-13"},
   }

   u := url.URL{
      Scheme:   "https",
      Host:     "open.volcengineapi.com",
      RawQuery: query.Encode(),
   }

   paramBytes, err := json.Marshal(aidr)
   if err != nil {
      panic(err)
   }

   reqBody := &bytes.Buffer{}
   writer := multipart.NewWriter(reqBody)
   if file != nil {
      fileWriter, err := writer.CreateFormFile("ImageFile", file.Name())
      if err != nil {
         panic(err)
      }
      _, err = io.Copy(fileWriter, file)
      if err != nil {
         panic(err)
      }
   }
   err = writer.WriteField("Request", string(paramBytes))
   if err != nil {
      panic(err)
   }
   writer.Close()

   req, err := http.NewRequest(http.MethodPost, u.String(), reqBody)
   if err != nil {
      panic(err)
   }
   req.Header.Set("Region", topRegion)
   req.Header.Set("Service", topService)
   req.Header.Set("Content-Type", writer.FormDataContentType())

   credentials := base.Credentials{
      AccessKeyID:     ak,
      SecretAccessKey: sk,
      Service:         req.Header.Get("Service"),
      Region:          req.Header.Get("Region"),
   }
   credentials.Sign(req) // 请求签名

   resp, err := http.DefaultClient.Do(req)
   if err != nil {
      panic(err)
   }

   if resp.StatusCode != http.StatusOK {
      panic(resp.Status)
   }

   body, err := ioutil.ReadAll(resp.Body)
   resp.Body.Close()
   if err != nil {
      panic(err)
   }

   r := &addImageDataResp{}
   err = json.Unmarshal(body, r)
   if err != nil {
      panic(err)
   }
   fmt.Printf("addSyncTaskData Resp: %+v\n", r)

}