點播服務端提供豐富的上傳接口,支持開發者采用不同的上傳方式實現媒體上傳。本文介紹Python SDK調用服務端上傳接口的使用場景和調用示例。
使用場景
本文僅提供API調用示例,即在點播服務中獲取上傳憑證和地址等操作示例。要實現完整的媒體文件上傳邏輯,請參考下表跳轉至對應的上傳方式和使用場景。
接口 | 使用場景 |
| |
|
前提條件
以下示例均通過AccessKey初始化客戶端實例。
接口調用說明
本文提供的接口調用示例均通過AccessKey初始化客戶端實例。
接口的參數解釋和返回字段的詳細說明請訪問阿里云OpenAPI門戶,在各接口右側的文檔頁簽查看。
本文僅提供部分復雜接口的代碼示例,其余接口的SDK代碼示例,可以通過阿里云OpenAPI門戶獲取。訪問阿里云OpenAPI門戶,在接口的左側參數配置頁簽,填寫需要的參數信息并發起調用后,在右側的SDK示例頁簽,選擇SDK版本,選擇目標語言,查看并下載示例代碼。
本文均以V1.0版本的SDK為例進行接口調用,如需獲取V2.0版本的SDK示例,請在通過阿里云OpenAPI門戶獲取SDK示例時,指定到對應的SDK版本。
接口調用示例
獲取音/視頻上傳地址和憑證
調用CreateUploadVideo接口,完成獲取音/視頻上傳地址和憑證功能。
阿里云OpenAPI門戶地址:CreateUploadVideo。
調用示例如下:
from aliyunsdkvod.request.v20170321 import CreateUploadVideoRequest
def create_upload_video(clt):
request = CreateUploadVideoRequest.CreateUploadVideoRequest()
request.set_Title('Video Title')
request.set_FileName('/opt/video/sample/video_file.mp4')
request.set_Description('Video Description')
# CoverURL示例:"http://192.168.0.1/16/tps/TB1qnJ1PVXXXXXCXXXXXXXXXXXX-700-700.png"
request.set_CoverURL('<your Cover URL>')
request.set_Tags('tag1,tag2')
request.set_CateId(0)
request.set_accept_format('JSON')
response = json.loads(clt.do_action_with_exception(request))
return response
try:
clt = init_vod_client()
uploadInfo = create_upload_video(clt)
print(uploadInfo['UploadAuth'])
print(json.dumps(uploadInfo, ensure_ascii=False, indent=4))
except Exception as e:
print(e)
print(traceback.format_exc())
刷新音/視頻上傳憑證
調用RefreshUploadVideo接口,完成刷新音/視頻上傳憑證功能。
阿里云OpenAPI門戶地址:RefreshUploadVideo。
調用示例如下:
from aliyunsdkvod.request.v20170321 import RefreshUploadVideoRequest
def refresh_upload_video(clt, videoId):
request = RefreshUploadVideoRequest.RefreshUploadVideoRequest()
request.set_VideoId(videoId)
request.set_accept_format('JSON')
return json.loads(clt.do_action_with_exception(request))
try:
clt = init_vod_client()
uploadInfo = refresh_upload_video(clt, 'VideoId')
print(json.dumps(uploadInfo, ensure_ascii=False, indent=4))
except Exception as e:
print(e)
print(traceback.format_exc())
獲取圖片上傳地址和憑證
調用CreateUploadImage接口,完成獲取圖片上傳地址和憑證功能。
阿里云OpenAPI門戶地址:CreateUploadImage。
調用示例如下:
from aliyunsdkvod.request.v20170321 import CreateUploadImageRequest
def create_upload_image(clt, imageType, imageExt):
request = CreateUploadImageRequest.CreateUploadImageRequest()
request.set_ImageType(imageType)
request.set_ImageExt(imageExt)
request.set_accept_format('JSON')
return json.loads(clt.do_action_with_exception(request))
try:
clt = init_vod_client()
imageInfo = create_upload_image(clt, 'cover', 'jpg')
print(json.dumps(imageInfo, ensure_ascii=False, indent=4))
except Exception as e:
print(e)
print(traceback.format_exc())
獲取輔助媒資上傳地址和憑證
調用CreateUploadAttachedMedia接口,完成獲取輔助媒資上傳地址和憑證功能。
阿里云OpenAPI門戶地址:CreateUploadAttachedMedia。
調用示例如下:
from aliyunsdkvod.request.v20170321 import CreateUploadAttachedMediaRequest
def create_upload_attached_media(clt):
request = CreateUploadAttachedMediaRequest.CreateUploadAttachedMediaRequest()
request.set_BusinessType("watermark");
request.set_MediaExt("gif");
request.set_Title("this is a sample");
request.set_accept_format('JSON')
return json.loads(clt.do_action_with_exception(request))
try:
clt = init_vod_client()
uploadInfo = create_upload_attached_media(clt)
print(uploadInfo['UploadAuth'])
print(json.dumps(uploadInfo, ensure_ascii=False, indent=4))
except Exception as e:
print(e)
print(traceback.format_exc())
URL批量拉取上傳
調用UploadMediaByURL接口,完成URL批量拉取上傳功能。
阿里云OpenAPI門戶地址:UploadMediaByURL。
調用示例如下:
from aliyunsdkvod.request.v20170321 import UploadMediaByURLRequest
import sys,urllib
def upload_media_by_url(clt):
request = UploadMediaByURLRequest.UploadMediaByURLRequest()
# URL示例:"http://example-bucket-****.cn-shanghai.aliyuncs.com/sample1-****.mp4","http://example-bucket-****.cn-shanghai.aliyuncs.com/sample2-****.flv"
urls = ["<your URL1>",
"your URL2"]
# Encode the urls and add metadata
uploadUrls = []
uploadMetadatas = []
for url in urls:
if sys.version_info[0] == 3:
encodeUrl = urllib.parse.quote(url)
else:
encodeUrl = urllib.quote(url)
uploadUrls.append(encodeUrl)
uploadMetadatas.append({'SourceURL': encodeUrl, 'Title': 'Sample Title'})
request.set_UploadURLs(','.join(uploadUrls))
request.set_UploadMetadatas(json.dumps(uploadMetadatas))
request.set_accept_format('JSON')
return json.loads(clt.do_action_with_exception(request))
try:
clt = init_vod_client()
uploadInfo = upload_media_by_url(clt)
print(uploadInfo['UploadJobs'])
print(json.dumps(uploadInfo, ensure_ascii=False, indent=4))
except Exception as e:
print(e)
print(traceback.format_exc())
獲取URL上傳信息
調用GetURLUploadInfos接口,完成獲取URL上傳信息功能。
調用示例如下:
from aliyunsdkvod.request.v20170321 import GetURLUploadInfosRequest
import sys,urllib
def get_url_upload_infos(clt):
request = GetURLUploadInfosRequest.GetURLUploadInfosRequest()
# URL示例:"http://192.168.0.1/16/sample1-****.mp4","http://192.168.0.1/16/sample2-****.flv"
urls = ["<your URL1>",
"<your URL2>"]
# 對URL進行編碼
uploadUrls = []
for url in urls:
if sys.version_info[0] == 3:
encodeUrl = urllib.parse.quote(url)
else:
encodeUrl = urllib.quote(url)
uploadUrls.append(encodeUrl)
# 設置上傳的URL列表,用逗號分隔
request.set_UploadURLs(','.join(uploadUrls))
# 也可以傳入jobId查詢
#request.set_JobIds("jobId1,jobId2")
request.set_accept_format('JSON')
return json.loads(clt.do_action_with_exception(request))
try:
clt = init_vod_client()
res = get_url_upload_infos(clt)
print(res['NonExists'])
print(json.dumps(res, ensure_ascii=False, indent=4))
except Exception as e:
print(e)
print(traceback.format_exc())
注冊媒資信息
調用RegisterMedia接口,完成注冊媒資信息功能。
阿里云OpenAPI門戶地址:RegisterMedia。
調用示例如下:
from aliyunsdkvod.request.v20170321 import RegisterMediaRequest
def register_media(clt):
request = RegisterMediaRequest.RegisterMediaRequest()
metadatas = []
# FlieURL示例:https://192.168.0.1/16/vod_sample_****.mp4
metadatas.append({'FileURL':"<your File URL>", "Title":"RegisterMedia sample Title"})
request.set_RegisterMetadatas(json.dumps(metadatas))
request.set_accept_format('JSON')
return json.loads(clt.do_action_with_exception(request))
try:
clt = init_vod_client()
registerInfo = register_media(clt)
print(registerInfo['RegisteredMediaList'])
print(registerInfo['FailedFileURLs'])
print(json.dumps(registerInfo, ensure_ascii=False, indent=4))
except Exception as e:
print(e)
print(traceback.format_exc())