ApsaraVideo VOD servers provide a variety of API operations for media upload. You can upload media files to ApsaraVideo VOD by calling one of these API operations. This topic describes the scenarios in which you can use the server operation SDK for Go to call these operations and provides sample code.
Scenarios
This topic provides only the sample code of calling API operations, such as the operation for obtaining a URL and a credential for uploading a media file. For more information about the scenarios in which the operations can be used, see the following table.
Operation | Scenario |
---|---|
| |
| |
|
Prerequisites
The server operation SDK for Go is installed. For more information, see Installation.
The server operation SDK for Go is initialized. For more information, see Initialization.
NoteIn the following examples, an AccessKey pair is used to initialize the server operation SDK.
Sample code
Obtain a URL and a credential for uploading an audio file or a video
You can call the CreateUploadVideo operation to obtain a URL and a credential for uploading an audio file or a video.
For more information about the request and response parameters of this operation, see CreateUploadVideo. The following sample code provides an example:
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)
}
Refresh the credential for uploading an audio file or a video
You can call the RefreshUploadVideo operation to refresh the credential for uploading an audio file or a video.
For more information about the request and response parameters of this operation, see RefreshUploadVideo. The following sample code provides an example:
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)
}
Obtain a URL and a credential for uploading an image
You can call the CreateUploadImage operation to obtain a URL and a credential for uploading an image.
For more information about the request and response parameters of this operation, see CreateUploadImage. The following sample code provides an example:
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)
}
Obtain a URL and a credential for uploading an auxiliary media asset
You can call the CreateUploadAttachedMedia operation to obtain a URL and a credential for uploading an auxiliary media asset.
For more information about the request and response parameters of this operation, see CreateUploadAttachedMedia. The following sample code provides an example:
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)
}
Upload multiple media files by using the URLs of source files at a time
You can call the UploadMediaByURL operation to upload multiple media files by using the URLs of source files at a time.
For more information about the request and response parameters of this operation, see UploadMediaByURL. The following sample code provides an example:
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()
// Encode URLs.
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)
}
// The URLs of source files. Separate multiple URLs with commas (,).
request.UploadURLs = strings.Join(uploadUrls, ",")
// Optional. The metadata that corresponds to the URLs.
jsonMetas, err := json.Marshal(metadatas)
if err != nil {
fmt.Println("json.Marshal failed:", err)
return
}
request.UploadMetadatas = string(jsonMetas)
// Optional. The transcoding template group.
//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)
}
}
Register media assets
You can call the RegisterMedia operation to register media assets.
For more information about the request and response parameters of this operation, see RegisterMedia. The following sample code provides an example:
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"
)
func MyRegisterMedia(client *vod.Client) (response *vod.RegisterMediaResponse, err error) {
request := vod.CreateRegisterMediaRequest()
metadatas := []map[string]interface{}{}
metadata1 := map[string]interface{}{"FileURL":"https://exampleBucket****.oss-cn-shanghai.aliyuncs.com/vod_video****.mp4",
"Title":"RegisterMedia sample Title 1"}
metadata2 := map[string]interface{}{"FileURL":"https://exampleBucket****.oss-cn-shanghai.aliyuncs.com/vod_video****.mp4",
"Title":"RegisterMedia sample Title 2"}
metadatas = append(metadatas, metadata1, metadata2)
jsonMetas, err := json.Marshal(metadatas)
if err != nil {
fmt.Println("json.Marshal failed:", err)
return
}
request.RegisterMetadatas = string(jsonMetas)
//request.TemplateGroupId = "<TemplateGroupId>"
request.AcceptFormat = "JSON"
return client.RegisterMedia(request)
}
func main() {
client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
if err != nil {
panic(err)
}
response, err := MyRegisterMedia(client)
if err != nil {
panic(err)
}
fmt.Println(response.GetHttpContentString())
fmt.Println(response.FailedFileURLs)
fmt.Println(response.RegisteredMediaList)
}
Query the information about URL-based upload jobs
You can call the GetURLUploadInfos operation to query the information about URL-based upload jobs.
For more information about the request and response parameters of this operation, see GetURLUploadInfos. The following sample code provides an example:
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()
// Encode URLs.
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)
}
// The URLs of source files. Separate multiple URLs with commas (,).
request.UploadURLs = strings.Join(uploadUrls, ",")
// The IDs of upload jobs.
//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)
}
}