全部产品
云市场

媒体上传

更新时间:2019-01-31 17:54:31

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

初始化客户端

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

获取视频上传地址和凭证

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

  1. from aliyunsdkvod.request.v20170321 import CreateUploadVideoRequest
  2. def create_upload_video(clt):
  3. request = CreateUploadVideoRequest.CreateUploadVideoRequest()
  4. request.set_Title('Video Title')
  5. request.set_FileName('/opt/video/sample/video_file.mp4')
  6. request.set_Description('Video Description')
  7. request.set_CoverURL('http://img.alicdn.com/tps/TB1qnJ1PVXXXXXCXXXXXXXXXXXX-700-700.png')
  8. request.set_Tags('tag1,tag2')
  9. request.set_CateId(0)
  10. request.set_accept_format('JSON')
  11. response = json.loads(clt.do_action_with_exception(request))
  12. return response
  13. try:
  14. clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
  15. uploadInfo = create_upload_video(clt)
  16. print(uploadInfo['UploadAuth'])
  17. print(json.dumps(uploadInfo, ensure_ascii=False, indent=4))
  18. except Exception as e:
  19. print(e)
  20. print(traceback.format_exc())

刷新视频上传凭证

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

  1. from aliyunsdkvod.request.v20170321 import RefreshUploadVideoRequest
  2. def refresh_upload_video(clt, videoId):
  3. request = RefreshUploadVideoRequest.RefreshUploadVideoRequest()
  4. request.set_VideoId(videoId)
  5. request.set_accept_format('JSON')
  6. return json.loads(clt.do_action_with_exception(request))
  7. try:
  8. clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
  9. uploadInfo = refresh_upload_video(clt, 'VideoId')
  10. print(json.dumps(uploadInfo, ensure_ascii=False, indent=4))
  11. except Exception as e:
  12. print(e)
  13. print(traceback.format_exc())

获取图片上传地址和凭证

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

  1. from aliyunsdkvod.request.v20170321 import CreateUploadImageRequest
  2. def create_upload_image(clt, imageType, imageExt):
  3. request = CreateUploadImageRequest.CreateUploadImageRequest()
  4. request.set_ImageType(imageType)
  5. request.set_ImageExt(imageExt)
  6. request.set_accept_format('JSON')
  7. return json.loads(clt.do_action_with_exception(request))
  8. try:
  9. clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
  10. imageInfo = create_upload_image(clt, 'cover', 'jpg')
  11. print(json.dumps(imageInfo, ensure_ascii=False, indent=4))
  12. except Exception as e:
  13. print(e)
  14. print(traceback.format_exc())

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

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

  1. from aliyunsdkvod.request.v20170321 import CreateUploadAttachedMediaRequest
  2. def create_upload_attached_media(clt):
  3. request = CreateUploadAttachedMediaRequest.CreateUploadAttachedMediaRequest()
  4. request.set_BusinessType("watermark");
  5. request.set_MediaExt("gif");
  6. request.set_Title("this is a sample");
  7. request.set_accept_format('JSON')
  8. return json.loads(clt.do_action_with_exception(request))
  9. try:
  10. clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
  11. uploadInfo = create_upload_attached_media(clt)
  12. print(uploadInfo['UploadAuth'])
  13. print(json.dumps(uploadInfo, ensure_ascii=False, indent=4))
  14. except Exception as e:
  15. print(e)
  16. print(traceback.format_exc())

URL批量拉取上传

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

  1. from aliyunsdkvod.request.v20170321 import UploadMediaByURLRequest
  2. import sys,urllib
  3. def upload_media_by_url(clt):
  4. request = UploadMediaByURLRequest.UploadMediaByURLRequest()
  5. urls = ["http://xxx.cn-shanghai.aliyuncs.com/sample1.mp4",
  6. "http://xxx.cn-shanghai.aliyuncs.com/sample2.flv"]
  7. # Encode the urls and add metadata
  8. uploadUrls = []
  9. uploadMetadatas = []
  10. for url in urls:
  11. if sys.version_info[0] == 3:
  12. encodeUrl = urllib.parse.quote(url)
  13. else:
  14. encodeUrl = urllib.quote(url)
  15. uploadUrls.append(encodeUrl)
  16. uploadMetadatas.append({'SourceURL': encodeUrl, 'Title': 'Sample Title'})
  17. request.set_UploadURLs(','.join(uploadUrls))
  18. request.set_UploadMetadatas(json.dumps(uploadMetadatas))
  19. request.set_accept_format('JSON')
  20. return json.loads(clt.do_action_with_exception(request))
  21. try:
  22. clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
  23. uploadInfo = upload_media_by_url(clt)
  24. print(uploadInfo['UploadJobs'])
  25. print(json.dumps(uploadInfo, ensure_ascii=False, indent=4))
  26. except Exception as e:
  27. print(e)
  28. print(traceback.format_exc())

注册媒资信息

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

  1. from aliyunsdkvod.request.v20170321 import RegisterMediaRequest
  2. def register_media(clt):
  3. request = RegisterMediaRequest.RegisterMediaRequest()
  4. metadatas = []
  5. metadatas.append({'FileURL':"https://xxxxxx.oss-cn-shanghai.aliyuncs.com/vod_sample.mp4", "Title":"RegisterMedia sample Title"})
  6. request.set_RegisterMetadatas(json.dumps(metadatas))
  7. request.set_accept_format('JSON')
  8. return json.loads(clt.do_action_with_exception(request))
  9. try:
  10. clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
  11. registerInfo = register_media(clt)
  12. print(registerInfo['RegisteredMediaList'])
  13. print(registerInfo['FailedFileURLs'])
  14. print(json.dumps(registerInfo, ensure_ascii=False, indent=4))
  15. except Exception as e:
  16. print(e)
  17. print(traceback.format_exc())

获取URL上传信息

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

  1. from aliyunsdkvod.request.v20170321 import GetURLUploadInfosRequest
  2. import sys,urllib
  3. def get_url_upload_infos(clt):
  4. request = GetURLUploadInfosRequest.GetURLUploadInfosRequest()
  5. urls = ["http://xxx.cn-shanghai.aliyuncs.com/sample1.mp4",
  6. "http://xxx.cn-shanghai.aliyuncs.com/sample2.flv"]
  7. # 对URL进行编码
  8. uploadUrls = []
  9. for url in urls:
  10. if sys.version_info[0] == 3:
  11. encodeUrl = urllib.parse.quote(url)
  12. else:
  13. encodeUrl = urllib.quote(url)
  14. uploadUrls.append(encodeUrl)
  15. # 设置上传的URL列表,用逗号分隔
  16. request.set_UploadURLs(','.join(uploadUrls))
  17. # 也可以传入jobId查询
  18. #request.set_JobIds("jobId1,jobId2")
  19. request.set_accept_format('JSON')
  20. return json.loads(clt.do_action_with_exception(request))
  21. try:
  22. clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
  23. res = get_url_upload_infos(clt)
  24. print(res['NonExists'])
  25. print(json.dumps(res, ensure_ascii=False, indent=4))
  26. except Exception as e:
  27. print(e)
  28. print(traceback.format_exc())