You need to enable JavaScript to run this app.
导航

媒资上传

最近更新时间2023.10.13 17:01:59

首次发布时间2021.02.23 10:42:40

本文为您提供了服务端 Go SDK 的媒资上传模块相关的 API 调用示例。主要包含:媒资上传、素材上传、URL 批量拉取上传、查询 URL 上传任务状态等。

注意事项

媒资上传模块的获取上传地址和凭证确认上传的 OpenAPI,目前支持 2 个版本。

  • OpenAPI 的版本号为 2022-01-01

说明

上传文件时,要求携带文件后缀。例如,mp4 文件上传,携带 .mp4 或 .MP4。

  • OpenAPI 的版本号为 2020-08-01

初始化

使用前先完成初始化,参考初始化

签发 UploadAuthToken

由 App/Web Server 持有的 AK/SK 在本地签出,不依赖外网。若希望同时生成多个UploadAuthToken,您可以循环调用生成方法。UploadAuthToken用于客户端上传,详见客户端上传

package vod

import (
        "encoding/json"
        "fmt"
        "testing"
        "time"

        "github.com/volcengine/volc-sdk-golang/service/vod"
        "github.com/volcengine/volc-sdk-golang/service/vod/models/request"
)

func TestVod_GetUploadAuthWithExpiredTime(t *testing.T) {
       instance := vod.NewInstance()
       instance.SetAccessKey("")
       instance.SetSecretKey("")
       ret, _ := instance.GetUploadAuth()
       b, _ := json.Marshal(ret)
       fmt.Println(string(b))

       ret2, _ := instance.GetUploadAuthWithExpiredTime(3 * time.Hour)
       b2, _ := json.Marshal(ret2)
       fmt.Println(string(b2))
}

媒资上传

接口请求参数和返回参数详见 OpenAPI:获取上传地址和凭证确认上传

package upload

import (
        "encoding/json"
        "fmt"
        "testing"

        "github.com/volcengine/volc-sdk-golang/base"
        "github.com/volcengine/volc-sdk-golang/service/vod"
        "github.com/volcengine/volc-sdk-golang/service/vod/models/business"
        "github.com/volcengine/volc-sdk-golang/service/vod/models/request"
        "github.com/volcengine/volc-sdk-golang/service/vod/upload/functions"
)

func TestVod_UploadMediaWithCallback(t *testing.T) {
        // call below method if you dont set ak and sk in ~/.vcloud/config
        instance := vod.NewInstance()
        instance.SetCredential(base.Credentials{
                AccessKeyID:     "your ak",
                SecretAccessKey: "your sk",
        })

        // or set ak and ak as follow
        //vod.NewInstance().SetAccessKey("")
        //vod.NewInstance().SetSecretKey("")

        spaceName := "your space"
        filePath := "media file path"

        getMetaFunc := functions.GetMetaFunc()                                                                      // 抽取视频元信息&封面功能,如需要则添加
        snapShotFunc := functions.SnapshotFunc(business.VodUploadFunctionInput{SnapshotTime: 1.3})                  // 抽取特定时刻的封面截图
        startWorkFlowFunc := functions.StartWorkflowFunc(business.VodUploadFunctionInput{TemplateId: "templateId"}) // 如希望上传完成后自动执行转码工作流,可将工作流Id填写在此函数里
        optionFunc := functions.AddOptionInfoFunc(business.VodUploadFunctionInput{
                Title:            "title",     // 视频的标题
                Tags:             "Go,编程",     // 视频的标签
                Description:      "Go 语言高级编程", // 视频的描述信息
                Format:           "MP4",       // 音视频格式
                ClassificationId: 0,           // 分类 Id,上传时可以指定分类,非必须字段
        })
        vodFunctions := []business.VodUploadFunction{snapShotFunc, getMetaFunc, startWorkFlowFunc, optionFunc}
        fbts, _ := json.Marshal(vodFunctions)

        vodUploadMediaRequset := &request.VodUploadMediaRequest{
                SpaceName:    spaceName,     // 空间名称
                FilePath:     filePath,      // 本地文件路径
                CallbackArgs: "my callback", // 透传信息,业务希望透传的字段可以写入,返回和回调中会返回此字段,非必须字段
                Functions:    string(fbts),  // 函数功能,具体可以参考火山引擎点播文档 开发者API-媒资上传-确认上传的 Functions 部分,可选功能字段
                FileName:     "",            // 设置文件路径
        }

        resp, _, err := instance.UploadMediaWithCallback(vodUploadMediaRequset)
        if err != nil {
                fmt.Printf("error %v", err)
        } else {
                bts, _ := json.Marshal(resp)
                fmt.Printf("resp = %s", bts)
        }

        fmt.Println()
        fmt.Println(resp.GetResponseMetadata().GetRequestId())
        fmt.Println(resp.GetResult().GetData().GetVid())

} 

素材上传

接口请求参数和返回参数详见 OpenAPI:获取上传地址和凭证确认上传

package upload

import (
        "encoding/json"
        "fmt"
        "testing"

        "github.com/volcengine/volc-sdk-golang/base"
        "github.com/volcengine/volc-sdk-golang/service/vod"
        "github.com/volcengine/volc-sdk-golang/service/vod/models/business"
        "github.com/volcengine/volc-sdk-golang/service/vod/models/request"
        "github.com/volcengine/volc-sdk-golang/service/vod/upload/consts"
        "github.com/volcengine/volc-sdk-golang/service/vod/upload/functions"
)

func TestVod_UploadMediaMaterialWithCallback(t *testing.T) {
        // call below method if you dont set ak and sk in ~/.vcloud/config
        instance := vod.NewInstance()
        instance.SetCredential(base.Credentials{
                AccessKeyID:     "your ak",
                SecretAccessKey: "your sk",
        })

        // or set ak and ak as follow
        //vod.NewInstance().SetAccessKey("")
        //vod.NewInstance().SetSecretKey("")

        spaceName := "your space"
        filePath := "material file path"

        snapShotFunc := functions.SnapshotFunc(business.VodUploadFunctionInput{SnapshotTime: 1.3})
        getMetaFunc := functions.GetMetaFunc()
        addOptionFunc := functions.AddOptionInfoFunc(business.VodUploadFunctionInput{
                Title:       "素材测试视频",             // 标题
                Tags:        "test",               // 多个标签可用逗号隔开
                Description: "素材测试,视频文件",          // 素材描述信息
                Category:    consts.CategoryVideo, // 素材分类,在 video、audio、image、dynamic_img、subtitle、font 中枚举
                RecordType:  2,                    // 素材上传 Type 值为 2
                Format:      "mp4",                //格式。若传入 Format 的话,以您传入参数为准,否则以系统识别出的 Format 为准。若遇到特殊文件无法识别,Format 可能为空。
        })

        vodFunctions := []business.VodUploadFunction{addOptionFunc, getMetaFunc, snapShotFunc}
        fbts, _ := json.Marshal(vodFunctions)

        vodUploadMaterialRequest := &request.VodUploadMaterialRequest{
                SpaceName:    spaceName,
                FilePath:     filePath,
                CallbackArgs: "my callback",
                Functions:    string(fbts),
                FileType:     consts.FileTypeMedia,
                FileName:     "",
        }

        resp, _, err := instance.UploadMaterialWithCallback(vodUploadMaterialRequest)
        if err != nil {
                fmt.Printf("error %v", err)
        } else {
                bts, _ := json.Marshal(resp)
                fmt.Printf("
resp = %s", bts)
        }

        fmt.Println(resp.GetResponseMetadata().GetRequestId())
        fmt.Println(resp.GetResult().GetData().GetMid())
        fmt.Printf("%d
", int(resp.GetResult().GetData().GetSourceInfo().GetSize()))

}

func TestVod_UploadImageMaterialWithCallback(t *testing.T) {
        // call below method if you dont set ak and sk in ~/.vcloud/config
        instance := vod.NewInstance()
        instance.SetCredential(base.Credentials{
                AccessKeyID:     "your ak",
                SecretAccessKey: "your sk",
        })

        // or set ak and ak as follow
        //vod.NewInstance().SetAccessKey("")
        //vod.NewInstance().SetSecretKey("")

        spaceName := "your space"
        filePath := "material file path"

        getMetaFunc := functions.GetMetaFunc()
        addOptionFunc := functions.AddOptionInfoFunc(business.VodUploadFunctionInput{
                Title:       "素材测试图片",
                Tags:        "test",
                Description: "素材测试,图片文件",
                Category:    consts.CategoryImage,
                RecordType:  2,
                Format:      "jpg",
        })

        vodFunctions := []business.VodUploadFunction{addOptionFunc, getMetaFunc}
        fbts, _ := json.Marshal(vodFunctions)

        vodUploadMaterialRequest := &request.VodUploadMaterialRequest{
                SpaceName:    spaceName,
                FilePath:     filePath,
                CallbackArgs: "my callback",
                Functions:    string(fbts),
                FileType:     consts.FileTypeImage,
                FileName:     "",
        }

        resp, _, err := instance.UploadMaterialWithCallback(vodUploadMaterialRequest)
        if err != nil {
                fmt.Printf("error %v", err)
        } else {
                bts, _ := json.Marshal(resp)
                fmt.Printf("
resp = %s", bts)
        }

        fmt.Println(resp.String())
        fmt.Println(resp.GetResponseMetadata().GetRequestId())
        fmt.Println(resp.GetResult().GetData().GetMid())

}

func TestVod_UploadObjectMaterialWithCallback(t *testing.T) {
        // call below method if you dont set ak and sk in ~/.vcloud/config
        instance := vod.NewInstance()
        instance.SetCredential(base.Credentials{
                AccessKeyID:     "your ak",
                SecretAccessKey: "your sk",
        })

        // or set ak and ak as follow
        //vod.NewInstance().SetAccessKey("")
        //vod.NewInstance().SetSecretKey("")

        spaceName := "your space"
        filePath := "material file path"

        getMetaFunc := functions.GetMetaFunc()
        addOptionFunc := functions.AddOptionInfoFunc(business.VodUploadFunctionInput{
                Title:       "素材测试字幕",
                Tags:        "test",
                Description: "素材测试,字幕文件",
                Category:    consts.CategorySubtitle,
                RecordType:  2,
                Format:      "vtt",
        })

        vodFunctions := []business.VodUploadFunction{addOptionFunc, getMetaFunc}
        fbts, _ := json.Marshal(vodFunctions)

        vodUploadMaterialRequest := &request.VodUploadMaterialRequest{
                SpaceName:    spaceName,
                FilePath:     filePath,
                CallbackArgs: "my callback",
                Functions:    string(fbts),
                FileType:     consts.FileTypeObject,
                FileName:     "",
        }

        resp, _, err := instance.UploadMaterialWithCallback(vodUploadMaterialRequest)
        if err != nil {
                fmt.Printf("error %v", err)
        } else {
                bts, _ := json.Marshal(resp)
                fmt.Printf("
resp = %s", bts)
        }

        fmt.Println(resp.String())
        fmt.Println(resp.GetResponseMetadata().GetRequestId())
        fmt.Println(resp.GetResult().GetData().GetMid())

}
 

URL 批量拉取上传

接口请求参数和返回参数详见 OpenAPI:URL 批量拉取上传

package upload

import (
	"fmt"
	"github.com/volcengine/volc-sdk-golang/service/vod/models/business"

	"github.com/volcengine/volc-sdk-golang/base"
	"github.com/volcengine/volc-sdk-golang/service/vod"
	"github.com/volcengine/volc-sdk-golang/service/vod/models/request"

	"testing"
)

func TestVod_UploadMediaByUrl(t *testing.T) {
	// call below method if you dont set ak and sk in ~/.vcloud/config
	instance := vod.NewInstance()
	instance.SetCredential(base.Credentials{
		AccessKeyID:     "your ak",
		SecretAccessKey: "your sk",
	})

	urlSets := make([]*business.VodUrlUploadURLSet, 0)
	urlSet := &business.VodUrlUploadURLSet{
		SourceUrl:        "your source url",    // 源视频 URL
		CallbackArgs:     "your callback args", // 透传信息,业务希望透传的字段可以写入,返回和回调中会返回此字段,非必须字段
		FileName:         "",                   // 设置文件路径
		FileExtension:    ".mp4",               // 设置文件后缀,以 . 开头,不超过 8 位,非必须字段
		CustomURLHeaders: map[string]string{},  // 自定义 Header,业务希望访问源视频URL携带的Header(例如User-Agent)可以通过该参数传入,非必须字段
	}
	urlSets = append(urlSets, urlSet)

	query := &request.VodUrlUploadRequest{
		SpaceName: "your SpaceName",
		URLSets:   urlSets,
	}
	resp, status, err := instance.UploadMediaByUrl(query)
	fmt.Println(status)
	fmt.Println(err)
	fmt.Println(resp.String())
}

查询 URL 批量上传任务状态

接口请求参数和返回参数详见 OpenAPI:查询 URL 批量上传任务状态

package vod

import (
        "fmt"
        "testing"

        "github.com/volcengine/volc-sdk-golang/base"
        "github.com/volcengine/volc-sdk-golang/service/vod"
        "github.com/volcengine/volc-sdk-golang/service/vod/models/request"
)

func Test_QueryUploadTaskInfo(t *testing.T) {
        instance := vod.NewInstance()
        instance.SetCredential(base.Credentials{
                AccessKeyID:     "your ak",
                SecretAccessKey: "your sk",
        })

        query := &request.VodQueryUploadTaskInfoRequest{
                JobIds: "your JobIds",
        }

        resp, status, err := instance.QueryUploadTaskInfo(query)
        fmt.Println(status)
        fmt.Println(err)
        fmt.Println(resp.String())
}