All Products
Search
Document Center

Media upload

Last Updated: Jul 11, 2019

This topic describes how to use the Python server SDK to call server operations, for example, to obtain the upload URL and credential and register media asset information. To upload complete media files, you can use the client upload SDK together with the upload URL and credential or directly use the Python server upload SDK.

Initialize a client

Initialize a client before using the SDK. For more information, see init_vod_client.

Obtain the video upload URL and credential

For more information about the request and response parameters, see 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')
 request.set_CoverURL('http://img.alicdn.com/tps/TB1qnJ1PVXXXXXCXXXXXXXXXXXX-700-700.png')
    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('<AccessKeyId>', '<AccessKeySecret>') 
    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())

Refresh the video upload credential

For more information about the request and response parameters, see 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('<AccessKeyId>', '<AccessKeySecret>') 
    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())

Obtain the image upload URL and credential

For more information about the request and response parameters, see 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('<AccessKeyId>', '<AccessKeySecret>') 
    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())

Obtain the upload URL and credential for an attached media asset

For more information about the request and response parameters, see 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('<AccessKeyId>', '<AccessKeySecret>') 
    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())

Upload multiple videos based on mezzanine file URLs

For more information about the request and response parameters, see UploadMediaByURL.

from aliyunsdkvod.request.v20170321 import UploadMediaByURLRequest
import sys,urllib
def upload_media_by_url(clt):
    request = UploadMediaByURLRequest.UploadMediaByURLRequest()

    urls = ["http://xxx.cn-shanghai.aliyuncs.com/sample1.mp4",
            "http://xxx.cn-shanghai.aliyuncs.com/sample2.flv"]
    # Encodes the URLs and adds 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('<AccessKeyId>', '<AccessKeySecret>') 
    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())

Register media asset information

For more information about the request and response parameters, see RegisterMedia.

from aliyunsdkvod.request.v20170321 import RegisterMediaRequest
def register_media(clt):
    request = RegisterMediaRequest.RegisterMediaRequest()
    metadatas = []
    metadatas.append({'FileURL':"https://xxxxxx.oss-cn-shanghai.aliyuncs.com/vod_sample.mp4", "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('<AccessKeyId>', '<AccessKeySecret>')
    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())

Obtain the information about upload based on mezzanine file URLs

For more information about the request and response parameters, see GetURLUploadInfos.

from aliyunsdkvod.request.v20170321 import GetURLUploadInfosRequest
import sys,urllib
def get_url_upload_infos(clt):
    request = GetURLUploadInfosRequest.GetURLUploadInfosRequest()
    urls = ["http://xxx.cn-shanghai.aliyuncs.com/sample1.mp4",
            "http://xxx.cn-shanghai.aliyuncs.com/sample2.flv"]

    # Encodes mezzanine file URLs.
    uploadUrls = []
    for url in urls:
        if sys.version_info[0] == 3:
            encodeUrl = urllib.parse.quote(url)
        else:
            encodeUrl = urllib.quote(url)
        uploadUrls.append(encodeUrl)

    # Specifies a list of mezzanine file URLs. Separate multiple URLs with a comma (,).
    request.set_UploadURLs(','.join(uploadUrls))

    # Specifies the IDs of upload jobs, alternatively.
    #request.set_JobIds("jobId1,jobId2")

    request.set_accept_format('JSON')
    return json.loads(clt.do_action_with_exception(request))

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    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())