数据工坊 DWR-自定义函数开发规范:对接转码函数示例(GO语言)

时间:2024-01-04 09:39:58

对接转码函数示例(GO语言)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main

import (
	"encoding/json"
	"errors"
	"go-runtime/go-api/context"
)

func DemoTranscodeHandler(jsonData []byte, ctx context.RuntimeContext) (interface{}, error) {
	var eventMsg Payload
	err := json.Unmarshal(jsonData, &eventMsg)
	if err != nil {
		return nil, errors.New("not correct format")
	}
	// 存储输入桶和对象值
	record := eventMsg.Records[0]

	// 定义输出
	resp := struct {
		OBSMessages
		Inputs        map[string]interface{} `json:"inputs"`
		ExecutionName string                 `json:"execution_name"`
		GraphName     string                 `json:"graph_name"`
		DynamicSource struct {
			*CreateTranscodeDynamicSourceBody
		} `json:"dynamic_source"`
	}{}
	// 配置截图参数,为下游截图任务提供参数配置
	resp.DynamicSource.CreateTranscodeDynamicSourceBody = &CreateTranscodeDynamicSourceBody{
		Transcodes: []*CreateTranscodeTaskBody{
			&CreateTranscodeTaskBody{
				//源文件地址。
				Input: &FileAddr{
					Location:   "cn-north-4",
					BucketName: record.Obs.Bucket.Name,
					Object:     record.Obs.Object.Key,
				},
				//输出地址。
				Output: &FileAddr{
					Location:   "cn-north-4",
					BucketName: record.Obs.Bucket.Name,
					Object:     "transcode_out",
				},
				TransTemplateID: []int{7000523, 7000524, 7000526, 7000528, 7000530, 7000538},
				OutputFilenames: []string{"out_file1", "out_file2", "out_file3", "out_file4", "out_file5", "out_file6"},
			},
		},
	}
	// 以下参数需要继承传递,方便工作流下游函数获取对应参数值
	resp.Inputs = eventMsg.Inputs
	resp.Records = eventMsg.Records
	resp.GraphName = eventMsg.GraphName
	resp.ExecutionName = eventMsg.ExecutionName
	return resp, nil
}
support.huaweicloud.com/usermanual-dwr/dwr_03_0008.html