本文为您提供了服务端 Go SDK 的媒资上传模块的接口调用示例。
视频点播目前支持以下两个版本的获取上传地址和凭证和确认上传接口:
接口请求参数和返回参数详见 OpenAPI:获取上传地址和凭证及确认上传。
注意
版本号为 2022-01-01 时,上传文件必须携带文件后缀。例如,如需上传 MP4 文件,携带 .mp4
或 .MP4
。
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{ // 如希望上传完成后自动执行转码工作流,可将工作流Id填写在此函数里 Templates: []*business.VodUploadTemplate{ { TemplateIds: []string{"transcode template id"}, //点播转码工作流模板, 当前最多支持一个 TemplateType: "transcode", }, { TemplateIds: []string{"imp template id"}, //智能处理工作流模板, 当前最多支持一个 TemplateType: "imp", }, }, }) optionFunc := functions.AddOptionInfoFunc(business.VodUploadFunctionInput{ Title: "title", // 视频的标题 Tags: "Go,编程", // 视频的标签 Description: "Go 语言高级编程", // 视频的描述信息 Format: "MP4", // 音视频格式 ClassificationId: 0, // 分类 Id,上传时可以指定分类,非必须字段 IsHlsIndexOnly: false, //该字段传true表示视频仅关联hls文件,删除时不会删除ts文件 }) 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: "", // 设置文件名,无格式长度限制,用户可自定义,目前文件名不支持空格、+ 字符,如果要使用此字段,请联系技术支持配置白名单,非必须字段 FileExtension: ".mp4", // 设置文件后缀,以 . 开头,不超过8位 VodUploadSource: "upload", // 设置上传来源,值为枚举值 ParallelNum: 2, // 开启2协程进行分片上传,不配置时默认单协程,可根据机器 cpu 内存配置进行协程数设置 UploadHostPrefer: "", // 设置上传域名偏好 } 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()) fmt.Println(resp.GetResult().GetData().GetSourceInfo().GetFileName()) }
视频点播目前支持以下两个版本的获取上传地址和凭证和确认上传接口:
接口请求参数和返回参数详见 OpenAPI:获取上传地址和凭证及确认上传。
注意
版本号为 2022-01-01 时,上传文件必须携带文件后缀。例如,如需上传 MP4 文件,携带 .mp4
或 .MP4
。
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: "", FileExtension: ".mp4", UploadHostPrefer: "", } 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: "", FileExtension: ".jpg", UploadHostPrefer: "", } 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: "", FileExtension: ".vtt", UploadHostPrefer: "", } 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()) }
接口请求参数和返回参数详见 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) { // Create a VOD instance in the specified region. // instance := vod.NewInstanceWithRegion("cn-north-1") instance := vod.NewInstance() // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/65655. // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed. // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account. // 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()) }
可在应用服务端通过 AK 和 SK 在本地签出临时上传 Token,不依赖外网。如希望同时生成多个临时上传 Token,您可以循环调用生成方法。临时上传 Token 用于客户端上传,详见客户端上传。
说明
支持签入上传策略,详见上传策略。
func TestVod_GetUploadAuthWithExpiredTime(t *testing.T) { // Create a VOD instance in the specified region. // instance := vod.NewInstanceWithRegion("cn-north-1") instance := vod.NewInstance() // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/65655. // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed. // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account. // instance.SetCredential(base.Credentials{ // AccessKeyID: "your ak", // SecretAccessKey: "your sk", //}) opts := make([]model.UploadAuthOpt, 0) // 使用 vod.WithUploadKeyPtn("表达式") 来限制上传的文件路径 FileName // 如: "test/*" 表示上传的文件必须包含 "test/" 前缀 // opts = append(opts, vod.WithUploadKeyPtn("表达式")) // 使用 vod.WithUploadSpaceNames([]string{}) 来限制允许上传的空间 // opts = append(opts, vod.WithUploadSpaceNames([]string{})) // 使用 vod.WithUploadPolicy() 来设置上传策略 // opts = append(opts, vod.WithUploadPolicy(&model.UploadPolicy{})) // 默认过期时间为 1 小时 ret, _ := instance.GetUploadAuth(opts...) b, _ := json.Marshal(ret) fmt.Println(string(b)) ret2, _ := instance.GetUploadAuthWithExpiredTime(3*time.Hour, opts...) b2, _ := json.Marshal(ret2) fmt.Println(string(b2)) } type UploadPolicy struct { ContentTypeBlackList []string `json:"ContentTypeBlackList,omitempty"` // 上传文件 Content-Type 黑名单 ContentTypeWhiteList []string `json:"ContentTypeWhiteList,omitempty"` // 上传文件 Content-Type 白名单 FileSizeUpLimit string `json:"FileSizeUpLimit,omitempty"` // 上传文件大小上限 FileSizeBottomLimit string `json:"FileSizeBottomLimit,omitempty"` // 上传文件大小下限 }
接口请求参数和返回参数说明详见 ApplyUploadInfo。
package vod import ( "fmt" "testing" "github.com/volcengine/volc-sdk-golang/service/vod" "github.com/volcengine/volc-sdk-golang/service/vod/models/request" ) func Test_ApplyUploadInfo(t *testing.T) { // Create a VOD instance in the specified region. // instance := vod.NewInstanceWithRegion("cn-north-1") instance := vod.NewInstance() // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/65655. // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed. // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account. // instance.SetCredential(base.Credentials{ // AccessKeyID: "your ak", // SecretAccessKey: "your sk", //}) query := &request.VodApplyUploadInfoRequest{ SpaceName: "your SpaceName", SessionKey: "your SessionKey", FileSize: 0, FileType: "your FileType", FileName: "your FileName", StorageClass: 1, FileExtension: "your FileExtension", ClientNetWorkMode: "your ClientNetWorkMode", ClientIDCMode: "your ClientIDCMode", NeedFallback: false, UploadHostPrefer: "your UploadHostPrefer", } resp, status, err := instance.ApplyUploadInfo(query) fmt.Println(status) fmt.Println(err) fmt.Println(resp.String()) }
接口请求参数和返回参数详见 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) { // Create a VOD instance in the specified region. // instance := vod.NewInstanceWithRegion("cn-north-1") instance := vod.NewInstance() // Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/65655. // The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed. // During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account. // 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()) }