This topic describes how to use the server upload SDK for Python to upload media files to ApsaraVideo VOD.

Upload process

The internal upload logic used by the server upload SDK for Python is the same as that of the general upload process of server upload SDKs. For more information, see the "Upload process" section of the Upload process topic. The following content describes the basic process of using the server upload SDK for Python:

  1. Complete the prerequisites. For more information, see the Prerequisites section.
  2. Integrate the upload SDK for Python. For more information, see the Integrate the upload SDK for Python section.
  3. Implement the upload logic. This involves upload information configuration.
    • For more information about the sample code for uploading audio and video files, see the Upload audio and video files section.
    • For more information about the sample code for uploading images, see the Upload images section.
    • For more information about the sample code for uploading auxiliary media assets, see the Upload auxiliary media assets section.

Prerequisites

  • ApsaraVideo VOD is activated. For more information, see Activate ApsaraVideo VOD.

  • The system settings required for uploading media files, including the OSS bucket for storing the media files in the specified region and the callback settings, are configured. For more information, see Manage VOD resources and Configure callback settings.
  • A RAM user is created and used to access ApsaraVideo VOD. To prevent security risks caused by the leakage of the AccessKey pair of your Alibaba Cloud account, we recommend that you create a RAM user and grant the RAM user the permissions required to access ApsaraVideo VOD. Then, you can use the AccessKey pair of the RAM user to access ApsaraVideo VOD. For more information, see Create and grant permissions to a RAM user.
  • Optional. A role is created for the RAM user and the role is granted the permissions required to access ApsaraVideo VOD if you want to access ApsaraVideo VOD by using Security Token Service (STS). For more information, see Create a role and grant temporary access permissions to the role by using STS.
    Note For more information about the scenarios in which STS can be used, see Comparison between credentials and STS.

Integrate the upload SDK for Python

Note For more information about the directories of the upload SDK for Python, see the Directories of the upload SDK for Python section.
  1. Run the following commands to install dependencies:

    pip install aliyun-python-sdk-core

    pip install aliyun-python-sdk-vod

    pip install oss2

    Note If you are using Python 3.x, replace pip install aliyun-python-sdk-core with pip install aliyun-python-sdk-core-v3. If you have installed multiple versions of Python, you can run the pip3 command. If errors occur when you integrate the SDK, see the FAQ section of this topic for troubleshooting.
  2. Download the VodUploadSDK-Python_1.3.1.zip package, which contains the upload SDK for Python and sample code. For more information, see SDK download.
    Note In this example, the SDK V1.3.1 is used. You can use other versions based on your business requirements.
  3. Decompress the VodUploadSDK-Python_1.3.1.zip package and copy the voduploadsdk directory from the VodUploadSDK-Python_1.3.1 directory to your local project. We recommend that you use this method. Alternatively, you can copy the voduploadsdk directory to the site-packages directory.

Update the upload SDK for Python

If new methods or new features of existing methods are unavailable in the current SDK, update the SDK to the latest version.

  1. Run the following commands to update dependencies:

    pip install --upgrade aliyun-python-sdk-vod

    pip install --upgrade oss2

  2. Download the latest version of the upload SDK for Python to overwrite the current SDK. For more information, see SDK download.
    Note You can check the version number and release date of the current SDK in the first line of the ChangeLog.txt file in the voduploadsdk directory.

Upload audio and video files

Ordinary audio and video files
The upload SDK for Python supports the following types of audio and video files:
  • Local files. Multipart upload is used. A single file to be uploaded can be up to 48.8 TB in size. Resumable upload is not supported. For more information, see the testUploadLocalVideo function in the following sample code.
  • Online files. You can specify upload URLs to upload online files. A single file to be uploaded can be up to 48.8 TB in size. Before you upload online files, you must download the files to a local disk. Make sure that the local disk has sufficient space. For more information, see the testUploadWebVideo function in the following sample code.
Sample code
# -*- coding: UTF-8 -*-
from voduploadsdk.AliyunVodUtils import *
from voduploadsdk.AliyunVodUploader import AliyunVodUploader
from voduploadsdk.UploadVideoRequest import UploadVideoRequest 

# Test the upload of a local audio or video file.
def testUploadLocalVideo(accessKeyId, accessKeySecret, filePath, storageLocation=None):
    try:
        # Specify the region of the Elastic Compute Service (ECS) instance on which the upload script is to be deployed. If the region of the ECS instance is the same as the storage region in ApsaraVideo VOD, the file is automatically uploaded over the internal network. This speeds up the upload and reduces Internet traffic. 
        # ecsRegionId ="cn-shanghai"
        # uploader = AliyunVodUploader(accessKeyId, accessKeySecret, ecsRegionId)
        # You do not specify the ECS region where the upload script is deployed. 
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadVideoRequest = UploadVideoRequest(filePath, 'exampleTitle')
        # Specify the thumbnail. If the thumbnail is a local or online image, you can call the UploadImageRequest operation to upload the image to ApsaraVideo VOD and obtain the value of the ImageURL parameter.
        # Sample value of the ImageURL parameter: https://example.com/sample-****.jpg.
        #uploadVideoRequest.setCoverURL('<your Image URL>')  
        # Set tags.
        #uploadVideoRequest.setTags('tag1,tag2')
        if storageLocation:
            uploadVideoRequest.setStorageLocation(storageLocation)
        videoId = uploader.uploadLocalVideo(uploadVideoRequest)
        print("file: %s, videoId: %s" % (uploadVideoRequest.filePath, videoId))
        
    except AliyunVodException as e:
        print(e)
 
# Test the upload of an online audio or video file.
def testUploadWebVideo(accessKeyId, accessKeySecret, fileUrl, storageLocation=None):
    try:
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadVideoRequest = UploadVideoRequest(fileUrl, 'exampleTitle')
        uploadVideoRequest.setTags('tag1,tag2')
        if storageLocation:
            uploadVideoRequest.setStorageLocation(storageLocation)
        videoId = uploader.uploadWebVideo(uploadVideoRequest)
        print("file: %s, videoId: %s" % (uploadVideoRequest.filePath, videoId))
        
    except AliyunVodException as e:
        print(e)

####  Run the test code.   ####   
accessKeyId = '<AccessKeyId>'
accessKeySecret = '<AccessKeySecret>'

localFilePath = '/opt/video/sample-****.mp4'
testUploadLocalVideo(accessKeyId, accessKeySecret, localFilePath)

# Sample value of the FileURL parameter: example:http://video.developer.aliyundoc.com/video/sample-****.mp4.
fileUrl = '<your file URL>'
#testUploadWebVideo(accessKeyId, accessKeySecret, fileUrl)
M3U8 files
Sample code
# -*- coding: UTF-8 -*-
from voduploadsdk.AliyunVodUtils import *
from voduploadsdk.AliyunVodUploader import AliyunVodUploader
from voduploadsdk.UploadVideoRequest import UploadVideoRequest

# Test the upload of a local M3U8 video file.
def testUploadLocalM3u8(accessKeyId, accessKeySecret, m3u8LocalFile):
    try:
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadVideoRequest = UploadVideoRequest(m3u8LocalFile, 'exampleTitle')
        # uploadVideoRequest.setTemplateGroupId('<TemplateGroupId>')
        # The M3U8 file and file parts are stored in the same directory. The upload SDK automatically parses and uploads the M3U8 file.
        videoId = uploader.uploadLocalM3u8(uploadVideoRequest)
        print("file: %s, videoId: %s" % (uploadVideoRequest.filePath, videoId))

    except AliyunVodException as e:
        print(e)

# Test the upload of an online M3U8 video.
def testUploadWebM3u8(accessKeyId, accessKeySecret, m3u8FileUrl):
    try:
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadVideoRequest = UploadVideoRequest(m3u8FileUrl, 'exampleTitle')
        # ApsaraVideo VOD automatically parses the URLs of file parts if the URLs use the same signature as that of the M3U8 file or no signature is available. In other scenarios, you need to manually parse the URLs of part files.
        sliceFileUrls = uploader.parseWebM3u8(m3u8FileUrl)
        videoId = uploader.uploadWebM3u8(uploadVideoRequest, sliceFileUrls)
        print("file: %s, videoId: %s" % (uploadVideoRequest.filePath, videoId))

    except AliyunVodException as e:
        print(e)

####  Run the test code.   ####
accessKeyId = '<AccessKeyId>'
accessKeySecret = '<AccessKeySecret>'

m3u8LocalFile = '/opt/video/m3u8/example_01.m3u8'
#testUploadLocalM3u8(accessKeyId, accessKeySecret, m3u8LocalFile)

m3u8FileUrl = 'http://sample.oss.aliyuncs.com/video/m3u8/example_01.m3u8'
#testUploadWebM3u8(accessKeyId, accessKeySecret, m3u8FileUrl)
            

Upload images

Sample code
# -*- coding: UTF-8 -*-
from voduploadsdk.AliyunVodUtils import *
from voduploadsdk.AliyunVodUploader import AliyunVodUploader
from voduploadsdk.UploadImageRequest import UploadImageRequest

# Test the upload of a local image.
def testUploadLocalImage(accessKeyId, accessKeySecret, filePath):
    try:
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadImageRequest = UploadImageRequest(filePath)
        uploadImageRequest.setTitle('exampleTitle')  # Specify the image title, which is left empty by default.
        imageId, imageUrl = uploader.uploadImage(uploadImageRequest, True)
        print("file: %s, imageId: %s, imageUrl: %s" % (uploadImageRequest.filePath, imageId, imageUrl))

    except AliyunVodException as e:
        print(e)

# Test the upload of an online image.
def testUploadWebImage(accessKeyId, accessKeySecret, fileUrl):
    try:
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadImageRequest = UploadImageRequest(fileUrl)
        uploadImageRequest.setTitle('exampleTitle')  # Specify the image title, which is left empty by default.
        imageId, imageUrl = uploader.uploadImage(uploadImageRequest, False)
        print("file: %s, imageId: %s, imageUrl: %s" % (uploadImageRequest.filePath, imageId, imageUrl))

    except AliyunVodException as e:
        print(e)


####  Run the test code.   ####
accessKeyId = '<AccessKeyId>'
accessKeySecret = '<AccessKeySecret>'

localFilePath = '/opt/image/image01.png'
#testUploadLocalImage(accessKeyId, accessKeySecret, localFilePath)

fileUrl = 'http://vod-download.cn-shanghai.aliyuncs.com/retina/pic/20180208/496AE240-54AE-4CC8-8578-3EEC8F38****.gif'
testUploadWebImage(accessKeyId, accessKeySecret, fileUrl)
            

Upload auxiliary media assets

Sample code
# -*- coding: UTF-8 -*-
from voduploadsdk.AliyunVodUtils import *
from voduploadsdk.AliyunVodUploader import AliyunVodUploader
from voduploadsdk.UploadAttachedMediaRequest import UploadAttachedMediaRequest


# Test the upload of a local auxiliary media asset such as a watermark or subtitle.
def testUploadLocalAttachedMedia(accessKeyId, accessKeySecret, filePath):
    try:
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadAttachedRequest = UploadAttachedMediaRequest(filePath, 'watermark')
        uploadAttachedRequest.setTitle('exampleTitle')
        media = uploader.uploadAttachedMedia(uploadAttachedRequest, True)
        print(media)

    except AliyunVodException as e:
        print(e)


# Test the upload of an online auxiliary media asset such as a watermark or subtitle.
def testUploadWebAttachedMedia(accessKeyId, accessKeySecret, fileUrl):
    try:
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadAttachedRequest = UploadAttachedMediaRequest(fileUrl, 'watermark')
        uploadAttachedRequest.setTitle('exampletitle')
        media = uploader.uploadAttachedMedia(uploadAttachedRequest, False)
        print(media)

    except AliyunVodException as e:
        print(e)


####  Run the test code.   ####
accessKeyId = '<AccessKeyId>'
accessKeySecret = '<AccessKeySecret>'

localFilePath = '/opt/image/sample.png'
#testUploadLocalAttachedMedia(accessKeyId, accessKeySecret, localFilePath)

fileUrl = 'http://vod-download.cn-shanghai.aliyuncs.com/retina/pic/20180208/496AE240-54AE-4CC8-8578-3EEC8F386E0B.gif'
testUploadWebAttachedMedia(accessKeyId, accessKeySecret, fileUrl)
            

FAQ

If the ERROR:root:Download fail: [Errno 2] No such file or directory error message is returned when you use the sample code to upload an online video stream, you can create a dlfiles folder in the directory in which the voduploadsdk directory resides. 001

Directories of the upload SDK for Python

voduploadsdk directory
  • AliyunVodUtils.py
    Directory Description
    AliyunVodLog The log class of the upload SDK, which is implemented based on logging.
    AliyunVodUtils The utility class of the upload SDK.
    AliyunVodException The exception class of the upload SDK. Unified exception handling is supported. You need to only detect AliyunVodException exceptions for external calls.
  • UploadVideoRequest.py
    Directory Description
    UploadVideoRequest The request class for uploading videos. For more information about the parameters, see CreateUploadVideo.
  • UploadImageRequest.py
    Directory Description
    UploadImageRequest The request class for uploading images. For more information about the parameters, see CreateUploadImage.
  • UploadAttachedMediaRequest.py
    Directory Description
    UploadAttachedMediaRequest The request class for uploading auxiliary media assets. For more information about the parameters, see CreateUploadAttachedMedia.
  • AliyunVodUploader.py
    Directory Description
    uploadLocalVideo The method used to upload local videos.
    uploadWebVideo The method used to upload online videos.
    uploadLocalM3u8 The method used to upload local M3U8 videos.
    uploadWebM3u8 The method used to upload online M3U8 videos.
    uploadImage The method used to upload local or online images.
    uploadAttachedMedia The method used to upload local or online auxiliary media assets.
    parseWebM3u8 The method used to parse the information about file parts of online M3U8 videos.
    parseLocalM3u8 The method used to parse the information about file parts of local M3U8 videos.
    setApiRegion The method used to specify the access region of ApsaraVideo VOD. The default region is the China (Shanghai) region. You can also specify a region outside the Chinese mainland, such as the Singapore (Singapore) region. For more information, see VOD centers and endpoints.
    setMultipartUpload The method used to set the threshold of the file size based on which multipart upload starts and the size of each part in multipart upload.
    uploadProgressCallback The method used to set the callback for upload progress. This method can be overridden.
    setEnableCrc The method used to specify whether to enable cyclic redundancy check (CRC) during the upload. By default, CRC is enabled.
  • ChangeLog.txt: provides version history. You can obtain the version number and release date of the current SDK in the first line.
samples directory
  • uploadVideo.py: provides the sample code for uploading videos.
  • uploadImage.py: provides the sample code for uploading images.
  • uploadAttachedMedia.py: provides the sample code for uploading auxiliary media assets.