全部产品
云市场

媒体上传

更新时间:2019-01-31 17:36:42

本文档为Go版获取上传地址和凭证、注册媒资信息等服务端API的调用示例,要实现完整的媒体文件上传,可配合 客户端上传

初始化客户端

使用前请先初始化客户端,参考 InitVodClient 接口。

获取视频上传地址和凭证

接口参数和返回字段请参考 CreateUploadVideo

  1. package main
  2. import (
  3. "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
  4. "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
  5. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
  6. "fmt"
  7. )
  8. func MyCreateUploadVideo(client *vod.Client) (response *vod.CreateUploadVideoResponse, err error) {
  9. request := vod.CreateCreateUploadVideoRequest()
  10. request.Title = "Sample Video Title"
  11. request.Description = "Sample Description"
  12. request.FileName = "/opt/video/sample/video_file.mp4"
  13. //request.CateId = "-1"
  14. request.CoverURL = "http://img.alicdn.com/tps/TB1qnJ1PVXXXXXCXXXXXXXXXXXX-700-700.png"
  15. request.Tags = "tag1,tag2"
  16. request.AcceptFormat = "JSON"
  17. return client.CreateUploadVideo(request)
  18. }
  19. func main() {
  20. client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
  21. if err != nil {
  22. panic(err)
  23. }
  24. response, err := MyCreateUploadVideo(client)
  25. if err != nil {
  26. panic(err)
  27. }
  28. fmt.Println(response.GetHttpContentString())
  29. fmt.Printf("VideoId: %s\n UploadAddress: %s\n UploadAuth: %s",
  30. response.VideoId, response.UploadAddress, response.UploadAuth)
  31. }

刷新视频上传凭证

接口参数和返回字段请参考 RefreshUploadVideo

  1. package main
  2. import (
  3. "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
  4. "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
  5. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
  6. "fmt"
  7. )
  8. func MyRefreshUploadVideo(client *vod.Client) (response *vod.RefreshUploadVideoResponse, err error) {
  9. request := vod.CreateRefreshUploadVideoRequest()
  10. request.VideoId = "6657f89a86fa4f76a295ae95636e5aed"
  11. request.AcceptFormat = "JSON"
  12. return client.RefreshUploadVideo(request)
  13. }
  14. func main() {
  15. client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
  16. if err != nil {
  17. panic(err)
  18. }
  19. response, err := MyRefreshUploadVideo(client)
  20. if err != nil {
  21. panic(err)
  22. }
  23. fmt.Println(response.GetHttpContentString())
  24. fmt.Printf("UploadAddress: %s\n UploadAuth: %s", response.UploadAddress, response.UploadAuth)
  25. }

获取图片上传地址和凭证

接口参数和返回字段请参考 CreateUploadImage

  1. package main
  2. import (
  3. "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
  4. "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
  5. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
  6. "fmt"
  7. )
  8. func MyCreateUploadImage(client *vod.Client) (response *vod.CreateUploadImageResponse, err error) {
  9. request := vod.CreateCreateUploadImageRequest()
  10. request.ImageType = "cover"
  11. request.ImageExt = "jpg"
  12. request.Title = "Sample Image Title"
  13. //request.CateId = "-1"
  14. request.Tags = "tag1,tag2"
  15. request.AcceptFormat = "JSON"
  16. return client.CreateUploadImage(request)
  17. }
  18. func main() {
  19. client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
  20. if err != nil {
  21. panic(err)
  22. }
  23. response, err := MyCreateUploadImage(client)
  24. if err != nil {
  25. panic(err)
  26. }
  27. fmt.Println(response.GetHttpContentString())
  28. fmt.Printf("ImageId: %s\n ImageURL: %s\n UploadAddress: %s\n UploadAuth: %s",
  29. response.ImageId, response.ImageURL, response.UploadAddress, response.UploadAuth)
  30. }

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

接口参数和返回字段请参考 CreateUploadAttachedMedia

  1. package main
  2. import (
  3. "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
  4. "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
  5. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
  6. "fmt"
  7. )
  8. func MyCreateUploadAttachedMedia(client *vod.Client) (response *vod.CreateUploadAttachedMediaResponse, err error) {
  9. request := vod.CreateCreateUploadAttachedMediaRequest()
  10. request.BusinessType = "watermark"
  11. request.MediaExt = "gif"
  12. request.Title = "Sample watermark Title"
  13. //request.CateId = "-1"
  14. request.Tags = "tag1,tag2"
  15. request.AcceptFormat = "JSON"
  16. return client.CreateUploadAttachedMedia(request)
  17. }
  18. func main() {
  19. client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
  20. if err != nil {
  21. panic(err)
  22. }
  23. response, err := MyCreateUploadAttachedMedia(client)
  24. if err != nil {
  25. panic(err)
  26. }
  27. fmt.Println(response.GetHttpContentString())
  28. fmt.Printf("MediaId: %s\n MediaURL: %s\n FileURL: %s\n UploadAddress: %s\n UploadAuth: %s",
  29. response.MediaId, response.MediaURL, response.FileURL, response.UploadAddress, response.UploadAuth)
  30. }

URL批量拉取上传

接口参数和返回字段请参考 UploadMediaByURL

  1. package main
  2. import (
  3. "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
  4. "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
  5. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
  6. "fmt"
  7. "encoding/json"
  8. "net/url"
  9. "strings"
  10. )
  11. func MyUploadMediaByURL(client *vod.Client) (response *vod.UploadMediaByURLResponse, err error) {
  12. request := vod.CreateUploadMediaByURLRequest()
  13. // 对URL进行编码
  14. sourceUrls := []string{"https://sample.oss-cn-shanghai.aliyuncs.com/vod_sample1.mp4",
  15. "https://sample.oss-cn-shanghai.aliyuncs.com/vod_sample2.flv"}
  16. uploadUrls := []string{}
  17. metadatas := []map[string]interface{}{}
  18. for _, surl := range sourceUrls {
  19. encodeUrl := url.QueryEscape(surl)
  20. uploadUrls = append(uploadUrls, encodeUrl)
  21. metadata := map[string]interface{}{"SourceURL": encodeUrl, "Title":"UploadMediaByURL Sample Title"}
  22. metadatas = append(metadatas, metadata)
  23. }
  24. // 设置上传的URL列表,用逗号分隔
  25. request.UploadURLs = strings.Join(uploadUrls, ",")
  26. // 可选项,设置URL对应的Meta信息
  27. jsonMetas, err := json.Marshal(metadatas)
  28. if err != nil {
  29. fmt.Println("json.Marshal failed:", err)
  30. return
  31. }
  32. request.UploadMetadatas = string(jsonMetas)
  33. // 可选项,设置转码模板组
  34. //request.TemplateGroupId = "<TemplateGroupId>"
  35. request.AcceptFormat = "JSON"
  36. return client.UploadMediaByURL(request)
  37. }
  38. func main() {
  39. client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
  40. if err != nil {
  41. panic(err)
  42. }
  43. response, err := MyUploadMediaByURL(client)
  44. if err != nil {
  45. panic(err)
  46. }
  47. fmt.Println(response.GetHttpContentString())
  48. for _, job := range response.UploadJobs {
  49. fmt.Printf("%s: %s\n", job.JobId, job.SourceURL)
  50. }
  51. }

注册媒资信息

接口参数和返回字段请参考 RegisterMedia

  1. package main
  2. import (
  3. "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
  4. "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
  5. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
  6. "fmt"
  7. "encoding/json"
  8. )
  9. func MyRegisterMedia(client *vod.Client) (response *vod.RegisterMediaResponse, err error) {
  10. request := vod.CreateRegisterMediaRequest()
  11. metadatas := []map[string]interface{}{}
  12. metadata1 := map[string]interface{}{"FileURL":"https://sample.oss-cn-shanghai.aliyuncs.com/vod_sample1.mp4",
  13. "Title":"RegisterMedia sample Title 1"}
  14. metadata2 := map[string]interface{}{"FileURL":"https://sample.oss-cn-shanghai.aliyuncs.com/vod_sample2.mp4",
  15. "Title":"RegisterMedia sample Title 2"}
  16. metadatas = append(metadatas, metadata1, metadata2)
  17. jsonMetas, err := json.Marshal(metadatas)
  18. if err != nil {
  19. fmt.Println("json.Marshal failed:", err)
  20. return
  21. }
  22. request.RegisterMetadatas = string(jsonMetas)
  23. //request.TemplateGroupId = "<TemplateGroupId>"
  24. request.AcceptFormat = "JSON"
  25. return client.RegisterMedia(request)
  26. }
  27. func main() {
  28. client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
  29. if err != nil {
  30. panic(err)
  31. }
  32. response, err := MyRegisterMedia(client)
  33. if err != nil {
  34. panic(err)
  35. }
  36. fmt.Println(response.GetHttpContentString())
  37. fmt.Println(response.FailedFileURLs)
  38. fmt.Println(response.RegisteredMediaList)
  39. }

获取URL上传信息

接口参数和返回字段请参考 GetURLUploadInfos

  1. package main
  2. import (
  3. "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
  4. "github.com/aliyun/alibaba-cloud-sdk-go/services/vod"
  5. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
  6. "fmt"
  7. "strings"
  8. )
  9. func MyGetURLUploadInfos(client *vod.Client) (response *vod.GetURLUploadInfosResponse, err error) {
  10. request := vod.CreateGetURLUploadInfosRequest()
  11. // 对URL进行编码
  12. sourceUrls := []string{"https://sample.oss-cn-shanghai.aliyuncs.com/vod_sample1.mp4",
  13. "https://sample.oss-cn-shanghai.aliyuncs.com/vod_sample2.flv"}
  14. uploadUrls := []string{}
  15. for _, surl := range sourceUrls {
  16. encodeUrl := url.QueryEscape(surl)
  17. uploadUrls = append(uploadUrls, encodeUrl)
  18. }
  19. // 设置上传的URL列表,用逗号分隔
  20. request.UploadURLs = strings.Join(uploadUrls, ",")
  21. // 也可以传入jobId查询
  22. //request.JobIds = "jobId1,jobId2"
  23. request.AcceptFormat = "JSON"
  24. return client.GetURLUploadInfos(request)
  25. }
  26. func main() {
  27. client, err := InitVodClient("<accessKeyId>", "<accessKeySecret>")
  28. if err != nil {
  29. panic(err)
  30. }
  31. response, err := MyGetURLUploadInfos(client)
  32. if err != nil {
  33. panic(err)
  34. }
  35. fmt.Println(response.GetHttpContentString())
  36. fmt.Println(response.NonExists)
  37. for _, uploadInfo := range response.URLUploadInfoList {
  38. fmt.Printf("%s: %s %s\n", uploadInfo.UploadURL, uploadInfo.Status, uploadInfo.MediaId)
  39. }
  40. }