This topic describes how to use the upload SDK for Python to upload media files to ApsaraVideo VOD.
Upload process
The upload SDK for Python follows the general upload process of ApsaraVideo VOD SDKs. For more information, see Upload process. The following content describes the basic process of using the server upload SDK for Python:
Complete the configurations as described in the Prerequisites section of this topic.
Integrate the upload SDK for Python. For more information, see Integrate the upload SDK for Python.
Configure upload information to implement the upload logic.
For more information about how to upload audio or video files, see Scenario 1: Upload audio and video files.
For more information about how to upload images, see Scenario 2: Upload images.
For more information about how to upload auxiliary media assets, see Scenario 3: Upload auxiliary media assets.
Specify
ApiRegion
inAliyunVodUploader.py
if you want to upload media files to a region other than the China (Shanghai) region. By default, the upload SDK for Python uploads media files tocn-shanghai
. For more information about the supported endpoints, see Endpoints.The following sample code provides an example on how to specify the
China (Beijing)
region:self.setApiRegion('cn-beijing')
Prerequisites
ApsaraVideo VOD is activated. For more information, see Activate ApsaraVideo VOD.
The system settings required for the upload, including the storage path in the specified region and the callback settings, are configured. For more information, see Manage storage buckets and Configure callbacks.
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 Use STS to upload videos.
NoteFor more information about the scenarios in which STS can be used, see Comparison between credentials and STS.
Integrate the upload SDK for Python
For more information about the directories of the upload SDK for Python, see Directories.
The following section describes how to integrate the upload SDK for Python as a non-root user.
Run the following commands to install dependencies:
sudo pip install aliyun-python-sdk-core
sudo pip install aliyun-python-sdk-vod
sudo pip install oss2
NoteIf you use Python 3.x, replace sudo pip install aliyun-python-sdk-core with sudo 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, troubleshoot the errors by following the instructions provided in FAQ.
Download the VodUploadSDK-Python_1.3.*.zip package, which contains the upload SDK for Python and sample code. For more information, see Upload SDK.
NoteIn this topic, the SDK V1.3.1 is used as an example. You can use other versions based on your business requirements.
Decompress the VodUploadSDK-Python_1.3.*.zip package and copy voduploadsdk from the VodUploadSDK-Python_1.3.* directory to the directory of your local project or the site-packages directory. We recommend that you copy voduploadsdk to the directory of your local project.
Update the upload SDK for Python
If new operations or new features of existing operations are unavailable in the current SDK, update the SDK to the latest version.
Run the following commands to install dependencies:
sudo pip install --upgrade aliyun-python-sdk-vod
sudo pip install --upgrade oss2
Download the latest version of the upload SDK for Python to overwrite the existing SDK. For more information, see SDK overview and download.
NoteYou can obtain the version number and release date of the current SDK in the first line of the ChangeLog.txt file in the voduploadsdk directory.
Directories
voduploadsdk
AliyunVodUtils.py
Directory
Description
AliyunVodLog
The log class of the upload SDK. The class 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 video upload. For more information about the parameters, see CreateUploadVideo.
UploadImageRequest.py
Directory
Description
UploadImageRequest
The request class for image upload. For more information about the parameters, see CreateUploadImage.
UploadAttachedMediaRequest.py
Directory
Description
UploadAttachedMediaRequest
The request class for the upload of auxiliary media assets. For more information about the parameters, see CreateUploadAttachedMedia.
AliyunVodUploader.py
Directory
Description
uploadLocalVideo
The API operation that is used to upload local videos.
uploadWebVideo
The API operation that is used to upload online videos.
uploadLocalM3u8
The API operation that is used to upload local M3U8 videos.
uploadWebM3u8
The API operation that is used to upload online M3U8 videos.
uploadImage
The API operation that is used to upload local or online images.
uploadAttachedMedia
The API operation that is used to upload local or online auxiliary media assets.
parseWebM3u8
The API operation that is used to parse information about the file parts of online M3U8 videos.
parseLocalM3u8
The API operation that is used to parse information about the file parts of local M3U8 videos.
setApiRegion
The API operation that is used to specify the access region of ApsaraVideo VOD. Default value: cn-shanghai. You can specify a region outside the Chinese mainland, such as ap-southeast-1 for Singapore. For more information, see Region IDs of ApsaraVideo VOD.
setMultipartUpload
The API operation that is used to set the threshold of multipart upload and part size.
uploadProgressCallback
The API operation that is used to set the callback for the upload progress. This results can be overridden.
setEnableCrc
The API operation that is used to specify whether to enable cyclic redundancy check (CRC) during the upload. By default, CRC is enabled.
ChangeLog.txt: The version history. You can obtain the version number and release date of the SDK from the first line.
samples directory
uploadVideo.py: sample code for video upload.
uploadImage.py: sample code for image upload.
uploadAttachedMedia.py: sample code for the upload of auxiliary media assets.
Scenario 1: Upload audio and video files
Regular audio and video files
ApsaraVideo VOD allows you to upload audio or video files by using one of the following methods:
Use multipart upload to upload a local file. You can upload a file of up to 48.8 TB in size. Resumable upload is not supported. For more information, see the testUploadLocalVideo function in the sample code.
Use the file URL to upload an online file. You can upload a file of 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 sample code.
M3U8 files
Scenario 2: Upload images
Scenario 3: Upload auxiliary media assets
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 the dlfiles folder in the directory at the same level as voduploadsdk to fix the error.