You need to enable JavaScript to run this app.
导航
媒资上传
最近更新时间:2025.04.17 16:57:48首次发布时间:2021.02.23 10:42:40
我的收藏
有用
有用
无用
无用

本文为您介绍如何使用视频点播服务端 Go SDK 将音频、视频、图片等媒资文件上传至视频点播服务。

费用说明

媒资上传到视频点播中会产生存储费用,具体参见媒资存储计费

前提条件

上传音视频

直接上传本地文件

视频点播服务端 SDK 封装了 ApplyUploadInfo - 获取上传地址和凭证CommitUploadInfo - 确认上传接口以及上传逻辑,您只需简单配置即可进行上传。

说明

  • 完整的上传流程请见服务端上传
  • 上传任务成功提交后,您可通过媒资上传完成事件通知获取任务结果。配置方式请见事件通知概述
  • 上传文件时必须携带文件后缀。例如,如需上传 MP4 文件,携带 .mp4.MP4
  • 以下示例为 2022-01-01 版本的 ApplyUploadInfo 和 CommitUploadInfo 接口。若您使用 2020-08-01 版本,示例代码请见历史版本
package upload

import (
        "encoding/json"
        "fmt"
        "github.com/volcengine/volc-sdk-golang/service/vod/upload/model"
        "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())

}

URL 批量拉取上传

视频点播服务端 SDK 封装了 URL 批量拉取上传接口,您只需简单配置即可进行上传。完整的上传流程请见服务端上传

说明

  • 本接口主要适用于文件没有存储在本地服务器或终端,需要通过公网访问的 URL 地址上传的场景。源文件 URL 支持 HTTP 和 HTTPS。
  • 本接口为异步上传接口。上传任务成功提交后,系统会生成异步执行的任务,排队执行,不保证时效性。
  • 本接口的请求参数和返回参数说明详见 UploadMediaByUrl
  • 上传任务成功提交后,可通过以下方式获取任务结果:
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())
    }

流式上传

package upload

import (
        "encoding/json"
        "fmt"
        "github.com/volcengine/volc-sdk-golang/service/vod/upload/model"
        "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_UploadMediaStreamWithCallback(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"

        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 := &model.VodStreamUploadRequest{
                SpaceName:        spaceName,     // 空间名称
                Content:          nil,           // 上传内容
                Size:             0,             // 上传内容大小
                ChunkSize:        0,             // 分片大小,默认20M
                CallbackArgs:     "my callback", // 透传信息,业务希望透传的字段可以写入,返回和回调中会返回此字段,非必须字段
                Functions:        string(fbts),  // 函数功能,具体可以参考火山引擎点播文档 开发者API-媒资上传-确认上传的 Functions 部分,可选功能字段
                FileName:         "",            // 设置文件名,无格式长度限制,用户可自定义,目前文件名不支持空格、+ 字符,如果要使用此字段,请联系技术支持配置白名单,非必须字段
                FileExtension:    ".mp4",        // 设置文件后缀,以 . 开头,不超过8位
                VodUploadSource:  "upload",      // 设置上传来源,值为枚举值
                UploadHostPrefer: "",            // 设置上传域名偏好
        }

        resp, _, err := instance.UploadMediaStreamWithCallback(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())

}

上传素材

视频点播服务端 SDK 封装了 ApplyUploadInfo - 获取上传地址和凭证CommitUploadInfo - 确认上传接口以及上传逻辑,您只需简单配置即可进行上传。

说明

  • 完整的上传流程请见服务端上传
  • 上传任务成功提交后,您可通过素材上传完成事件通知获取任务结果。配置方式请见事件通知概述
  • 上传文件时必须携带文件后缀。例如,如需上传 MP4 文件,携带 .mp4.MP4
  • 以下示例为 2022-01-01 版本的 ApplyUploadInfo 和 CommitUploadInfo 接口。若您使用 2020-08-01 版本,示例代码请见历史版本
package upload

import (
        "encoding/json"
        "fmt"
        "github.com/volcengine/volc-sdk-golang/service/vod/upload/model"
        "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("\nresp = %s", bts)
        }

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

}

func TestVod_UploadMediaMaterialStreamWithCallback(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"

        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 := &model.VodStreamUploadRequest{
                SpaceName:        spaceName,
                Content:          nil, // 上传内容
                Size:             0,   // 内容大小
                ChunkSize:        0,   // 期望分片大小,默认20MB
                CallbackArgs:     "my callback",
                Functions:        string(fbts),
                FileType:         consts.FileTypeMedia,
                FileName:         "",
                FileExtension:    ".mp4",
                UploadHostPrefer: "",
        }

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

        fmt.Println(resp.GetResponseMetadata().GetRequestId())
        fmt.Println(resp.GetResult().GetData().GetMid())
        fmt.Printf("%d\n", 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("\nresp = %s", bts)
        }

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

}

func TestVod_UploadImageMaterialStreamWithCallback(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"

        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 := &model.VodStreamUploadRequest{
                SpaceName:        spaceName,
                Content:          nil, // 上传内容
                Size:             0,   // 内容大小
                ChunkSize:        0,   // 期望分片大小,默认20MB
                CallbackArgs:     "my callback",
                Functions:        string(fbts),
                FileType:         consts.FileTypeImage,
                FileName:         "",
                FileExtension:    ".jpg",
                UploadHostPrefer: "",
        }

        resp, _, err := instance.UploadMaterialStreamWithCallback(vodUploadMaterialRequest)
        if err != nil {
                fmt.Printf("error %v", err)
        } else {
                bts, _ := json.Marshal(resp)
                fmt.Printf("\nresp = %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("\nresp = %s", bts)
        }

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

}

func TestVod_UploadObjectMaterialStreamWithCallback(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"

        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 := &model.VodStreamUploadRequest{
                SpaceName:        spaceName,
                Content:          nil, // 上传内容
                Size:             0,   // 内容大小
                ChunkSize:        0,   // 期望分片大小,默认20MB
                CallbackArgs:     "my callback",
                Functions:        string(fbts),
                FileType:         consts.FileTypeObject,
                FileName:         "",
                FileExtension:    ".vtt",
                UploadHostPrefer: "",
        }

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

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

}

其它示例

签发临时上传 Token

如果您使用客户端上传 SDK 进行上传,您需要在应用服务端通过 AK 和 SK 在本地签出临时上传 Token,不依赖外网。如希望同时生成多个临时上传 Token,您可以循环调用生成方法。临时上传 Token 用于客户端上传,详见客户端上传

说明

Go SDK 支持签入上传策略,详见上传策略

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())
}

查询 URL 批量上传任务状态

接口请求参数和返回参数说明详见 QueryUploadTaskInfo

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())
}

历史版本

2020-08-01 上传音视频

接口请求参数和返回参数说明详见 ApplyUploadInfoCommitUploadInfo

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/request"
        "github.com/volcengine/volc-sdk-golang/service/vod/upload/functions"
)

func TestVod_UploadMediaWithCallback(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",
        //})

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

        snapShotFunc := functions.SnapshotFunc(1.3)
        getMetaFunc := functions.GetMetaFunc()
        vodFunctions := []vod.Function{snapShotFunc, getMetaFunc}
        fbts, _ := json.Marshal(vodFunctions)

        vodUploadMediaRequset := &request.VodUploadMediaRequest{
                SpaceName:    spaceName,
                FilePath:     filePath,
                CallbackArgs: "my callback",
                Functions:    string(fbts),
                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())

}

2020-08-01 上传素材

接口请求参数和返回参数说明详见 ApplyUploadInfoCommitUploadInfo

package upload

import (
        "encoding/json"
        "fmt"
        "github.com/volcengine/volc-sdk-golang/service/vod/models/request"
        "testing"

        "github.com/volcengine/volc-sdk-golang/base"
        "github.com/volcengine/volc-sdk-golang/service/vod"
        "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) {
        // 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",
        //})

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

        snapShotFunc := functions.SnapshotFunc(0.0)
        getMetaFunc := functions.GetMetaFunc()
        addOptionFunc := functions.AddOptionInfoFunc(vod.OptionInfo{
                Title:       "素材测试视频",
                Tags:        "test",
                Description: "素材测试,视频文件",
                Category:    consts.CategoryVideo,
                RecordType:  2,
                Format:      "mp4",
        })

        vodFunctions := []vod.Function{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) {
        // 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",
        //})

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

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

        vodFunctions := []vod.Function{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) {
        // 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",
        //})

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

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

        vodFunctions := []vod.Function{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())

}