媒体上传

点播服务端提供丰富的上传接口,支持开发者采用不同的上传方式实现媒体上传。本文介绍Go SDK调用服务端上传接口的使用场景和调用示例。

使用场景

注意

本文仅提供API调用示例,即在点播服务中获取上传凭证和地址等操作示例。要实现完整的媒体文件上传逻辑,请参考下表跳转至对应的上传方式和使用场景。

接口

使用场景

获取音视频上传地址和凭证

  • OSS原生SDK上传场景下,用户应用服务器集成点播服务端SDK并调用相关接口获取或刷新上传地址和凭证。获取到的上传地址和凭证需要Base64解码后作为初始化OSS上传客户端实例的参数。详情请参见基于OSS原生SDK上传

  • 点播客户端上传SDK场景下,用户应用服务器集成点播服务端SDK并调用相关接口获取或刷新上传地址和凭证。获取到的上传地址和凭证无需解码即可下发给客户端。详情请参见客户端上传SDK指引

刷新音视频上传凭证

获取图片上传地址和凭证

获取辅助媒资上传地址和凭证

URL批量拉取上传

  • 调用URL批量拉取上传接口实现离线拉取上传。这种方式可以免去先将文件下载到本地服务器或终端再使用上传SDK上传到点播的过程。

  • 调用获取URL上传信息获取上传进度。

获取URL上传信息

前提条件

  • 调用接口前请先安装SDK。操作指引请参见安装

  • 调用接口前请先初始化客户端。操作指引请参见初始化

    说明

    以下示例均通过AccessKey初始化客户端。

接口调用示例

获取音/视频上传地址和凭证

调用CreateUploadVideo接口,完成获取音/视频上传地址和凭证功能。

接口参数和返回字段请参见CreateUploadVideo。调用示例如下:

package main

import (
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
    "fmt"
)

func MyCreateUploadVideo(client *vod.Client) (response *vod.CreateUploadVideoResponse, err error) {
    request := vod.CreateCreateUploadVideoRequest()

    request.Title = "Sample Video Title"
    request.Description = "Sample Description"
    request.FileName = "/opt/video/sample/video_file.mp4"
    //request.CateId = "-1"
    request.CoverURL = "http://192.168.0.0/16/tps/TB1qnJ1PVXXXXXCXXXXXXXXXXXX-700-700.png"
    request.Tags = "tag1,tag2"

    request.AcceptFormat = "JSON"
    return client.CreateUploadVideo(request)
}


func main() {
    client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
    if err != nil {
        panic(err)
    }

    response, err := MyCreateUploadVideo(client)
    if err != nil {
        panic(err)
    }

    fmt.Println(response.GetHttpContentString())
    fmt.Printf("VideoId: %s\n UploadAddress: %s\n UploadAuth: %s",
        response.VideoId, response.UploadAddress, response.UploadAuth)
}

刷新音/视频上传凭证

调用RefreshUploadVideo接口,完成刷新音/视频上传凭证功能。

接口参数和返回字段请参见RefreshUploadVideo。调用示例如下:

package main

import (
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
    "fmt"
)

func MyRefreshUploadVideo(client *vod.Client) (response *vod.RefreshUploadVideoResponse, err error) {
    request := vod.CreateRefreshUploadVideoRequest()
    request.VideoId = "6657f89a86fa4f76a295ae95636e****"
    request.AcceptFormat = "JSON"

    return client.RefreshUploadVideo(request)
}

func main() {
    client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
    if err != nil {
        panic(err)
    }

    response, err := MyRefreshUploadVideo(client)
    if err != nil {
        panic(err)
    }

    fmt.Println(response.GetHttpContentString())
    fmt.Printf("UploadAddress: %s\n UploadAuth: %s", response.UploadAddress, response.UploadAuth)
}

获取图片上传地址和凭证

调用CreateUploadImage接口,完成获取图片上传地址和凭证功能。

接口参数和返回字段请参见CreateUploadImage。调用示例如下:

package main

import (
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
    "fmt"
)

func MyCreateUploadImage(client *vod.Client) (response *vod.CreateUploadImageResponse, err error) {
    request := vod.CreateCreateUploadImageRequest()

    request.ImageType = "cover"
    request.ImageExt = "jpg"
    request.Title = "Sample Image Title"
    //request.CateId = "-1"
    request.Tags = "tag1,tag2"

    request.AcceptFormat = "JSON"
    return client.CreateUploadImage(request)
}


func main() {
    client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
    if err != nil {
        panic(err)
    }

    response, err := MyCreateUploadImage(client)
    if err != nil {
        panic(err)
    }

    fmt.Println(response.GetHttpContentString())
    fmt.Printf("ImageId: %s\n ImageURL: %s\n UploadAddress: %s\n UploadAuth: %s",
        response.ImageId, response.ImageURL, response.UploadAddress, response.UploadAuth)
}

获取辅助媒资上传地址和凭证

调用CreateUploadAttachedMedia接口,完成获取辅助媒资上传地址和凭证功能。

接口参数和返回字段请参见CreateUploadAttachedMedia。调用示例如下:

package main

import (
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
    "fmt"
)

func MyCreateUploadAttachedMedia(client *vod.Client) (response *vod.CreateUploadAttachedMediaResponse, err error) {
    request := vod.CreateCreateUploadAttachedMediaRequest()

    request.BusinessType = "watermark"
    request.MediaExt = "gif"
    request.Title = "Sample watermark Title"
    //request.CateId = "-1"
    request.Tags = "tag1,tag2"

    request.AcceptFormat = "JSON"
    return client.CreateUploadAttachedMedia(request)
}

func main() {
    client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
    if err != nil {
        panic(err)
    }

    response, err := MyCreateUploadAttachedMedia(client)
    if err != nil {
        panic(err)
    }

    fmt.Println(response.GetHttpContentString())
    fmt.Printf("MediaId: %s\n MediaURL: %s\n FileURL: %s\n UploadAddress: %s\n UploadAuth: %s",
        response.MediaId, response.MediaURL, response.FileURL, response.UploadAddress, response.UploadAuth)
}

URL批量拉取上传

调用UploadMediaByURL接口,完成URL批量拉取上传功能。

接口参数和返回字段请参见UploadMediaByURL。调用示例如下:

package main

import (
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
    "fmt"
    "encoding/json"
    "net/url"
    "strings"
)

func MyUploadMediaByURL(client *vod.Client) (response *vod.UploadMediaByURLResponse, err error) {
    request := vod.CreateUploadMediaByURLRequest()

    // 对URL进行编码
    sourceUrls := []string{"https://exampleBucket****.oss-cn-shanghai.aliyuncs.com/vod_video****.mp4",
        "https://exampleBucket****.oss-cn-shanghai.aliyuncs.com/vod_video****.flv"}
    uploadUrls := []string{}
    metadatas := []map[string]interface{}{}
    for _, surl := range sourceUrls {
        encodeUrl := url.QueryEscape(surl)
        uploadUrls = append(uploadUrls, encodeUrl)
        metadata := map[string]interface{}{"SourceURL": encodeUrl, "Title":"UploadMediaByURL Sample Title"}
        metadatas = append(metadatas, metadata)
    }

    // 设置上传的URL列表,用逗号分隔
    request.UploadURLs = strings.Join(uploadUrls, ",")

    // 可选项,设置URL对应的Meta信息
    jsonMetas, err := json.Marshal(metadatas)
    if err != nil {
        fmt.Println("json.Marshal failed:", err)
        return
    }
    request.UploadMetadatas = string(jsonMetas)

    // 可选项,设置转码模板组
    //request.TemplateGroupId = "<TemplateGroupId>"

    request.AcceptFormat = "JSON"
    return client.UploadMediaByURL(request)
}

func main() {
    client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
    if err != nil {
        panic(err)
    }

    response, err := MyUploadMediaByURL(client)
    if err != nil {
        panic(err)
    }

    fmt.Println(response.GetHttpContentString())
    for _, job := range response.UploadJobs {
        fmt.Printf("%s: %s\n", job.JobId, job.SourceURL)
    }
}

获取URL上传信息

调用GetURLUploadInfos接口,完成获取URL上传信息功能。

接口参数和返回字段请参见GetURLUploadInfos。调用示例如下:

package main

import (
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
    "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
    "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
    "fmt"
    "strings"
)

func MyGetURLUploadInfos(client *vod.Client) (response *vod.GetURLUploadInfosResponse, err error) {
    request := vod.CreateGetURLUploadInfosRequest()

    // 对URL进行编码
    sourceUrls := []string{"https://192.168.0.0/16/vod_sample1.mp4",
        "https://exampleBucket****.oss-cn-shanghai.aliyuncs.com/vod_video****.flv"}
    uploadUrls := []string{}
    for _, surl := range sourceUrls {
        encodeUrl := url.QueryEscape(surl)
        uploadUrls = append(uploadUrls, encodeUrl)
    }

    // 设置上传的URL列表,用逗号分隔
    request.UploadURLs = strings.Join(uploadUrls, ",")

    // 也可以传入jobId查询
    //request.JobIds = "jobId1,jobId2"

    request.AcceptFormat = "JSON"
    return client.GetURLUploadInfos(request)
}

func main() {
    client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
    if err != nil {
        panic(err)
    }

    response, err := MyGetURLUploadInfos(client)
    if err != nil {
        panic(err)
    }

    fmt.Println(response.GetHttpContentString())
    fmt.Println(response.NonExists)
    for _, uploadInfo := range response.URLUploadInfoList {
        fmt.Printf("%s: %s %s\n", uploadInfo.UploadURL, uploadInfo.Status, uploadInfo.MediaId)
    }
}
阿里云首页 视频点播 相关技术圈