點播服務端提供豐富的上傳介面,支援開發人員採用不同的上傳方式實現媒體上傳。本文介紹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())