Upload files using the Python SDK
This topic provides detailed examples of how to use the server-side upload SDK for Python to upload various media files to ApsaraVideo VOD.
Overview
The Python upload SDK follows the general process of the ApsaraVideo VOD server-side SDK. For more information, see Upload process. The basic process of using the Python upload SDK is as follows:
-
Complete the prerequisites. For more information, see Prerequisites.
-
Integrate the Python upload SDK. For more information, see Integrate the Python upload SDK.
-
Implement the upload logic. This step involves configuring upload information.
-
For audio and video uploads, see Scenario 1: Upload audio and video files.
-
For image uploads, see Scenario 2: Upload images.
-
For auxiliary media asset uploads, see Scenario 3: Upload auxiliary media assets.
-
-
The default endpoint for the Python upload SDK is
cn-shanghai(Shanghai). To upload media files to other regions, you can specify the region usingApiRegioninAliyunVodUploader.py. For more information about the endpoints that ApsaraVideo VOD supports, see Endpoints.The following example shows how to set the upload region to
cn-beijing(Beijing):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 a RAM user and grant permissions.
-
Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables. For more information, see Configure environment variables in Linux, macOS, and Windows.
Important-
An Alibaba Cloud account AccessKey pair has permissions on all API operations. Use a RAM user's AccessKey pair to call API operations or perform routine O&M.
-
Do not hard-code your AccessKey ID and AccessKey secret in project code. If leaked, all resources in your account are compromised.
-
-
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 Obtain an STS token.
Integrate the Python upload SDK
-
For a detailed description of the upload SDK directories, see Directory structure.
-
This topic uses a regular user as an example to describe the operations.
-
Run the following commands to install dependency packages.
sudo pip install aliyun-python-sdk-core
sudo pip install aliyun-python-sdk-vod
sudo pip install oss2
NoteIf you use Python 3, change sudo pip install aliyun-python-sdk-core to sudo pip install aliyun-python-sdk-core-v3. If you have multiple Python versions installed, you can use the `pip3` command. If you encounter integration issues, see FAQ to troubleshoot the issues.
-
Download the Python upload SDK and sample code.
-
SDK version: 1.3.2.2
-
Update time: 2022-02-24
-
Download address: V1.3.2.2 SDK and demo source code
-
-
Decompress VodUploadSDK-Python_1.3.*.zip and copy the voduploadsdk folder from the VodUploadSDK-Python_1.3.* directory to your local project directory (recommended) or the site-packages directory.
Update the Python upload SDK
If you find that new APIs or new features of existing APIs are not available in your current SDK, update the SDK to the latest version.
-
Run the following commands to update the dependency packages.
sudo pip install --upgrade aliyun-python-sdk-vod
sudo pip install --upgrade oss2
-
Download the latest Python upload SDK and overwrite the local SDK files. For more information, see SDK download.
NoteYou can open the ChangeLog.txt file in the voduploadsdk directory to view the current SDK version number and release date.
Python upload SDK directory structure
The voduploadsdk directory
-
AliyunVodUtils.py
Directory
Description
AliyunVodLog
The log class of the upload SDK. It is implemented based on logging.
AliyunVodUtils
The utility class of the upload SDK.
AliyunVodException
The exception class of the upload SDK. It provides unified exception handling. You can catch this exception externally.
-
UploadVideoRequest.py
Directory
Description
UploadVideoRequest
The request class for uploading videos. For more information about the fields, see CreateUploadVideo.
-
UploadImageRequest.py
Directory
Description
UploadImageRequest
The request class for uploading images. For more information about the fields, see CreateUploadImage.
-
UploadAttachedMediaRequest.py
Directory
Description
UploadAttachedMediaRequest
The request class for uploading auxiliary media assets. For more information about the fields, see CreateUploadAttachedMedia.
-
AliyunVodUploader.py
Directory
Description
uploadLocalVideo
The API for uploading local videos.
uploadWebVideo
The API for uploading web videos.
uploadLocalM3u8
Uploads local M3U8 videos.
uploadWebM3u8
Uploads web M3U8 videos.
uploadImage
Uploads local or web image files.
uploadAttachedMedia
Uploads local or web auxiliary media asset files.
parseWebM3u8
Parses the shard information of a web M3U8 file.
parseLocalM3u8
Parses the shard information of a local M3U8 file.
setApiRegion
Sets the endpoint for VOD. The default value is cn-shanghai (Shanghai). Regions outside China, such as ap-southeast-1 (Singapore), are also supported. For more information, see VOD region IDs.
setMultipartUpload
Sets the threshold and part size for multipart upload.
uploadProgressCallback
The callback function for upload progress. You can rewrite this function.
setEnableCrc
Specifies whether to enable cyclic redundancy check (CRC) during upload. It is enabled by default.
-
ChangeLog.txt: Contains the release history. The first line indicates the current SDK version number and release date.
The samples directory
-
uploadVideo.py: Sample code for uploading videos.
-
uploadImage.py: Sample code for uploading images.
-
uploadAttachedMedia.py: Sample code for uploading auxiliary media asset files.
Scenario 1: Upload audio and video files
Upload regular files
You can upload the following types of audio and video files:
-
Local files: Multipart upload is used. The maximum size of a single file is 48.8 TB. Resumable upload is not supported. For more information, see the `testUploadLocalVideo` function in the sample code.
-
Web files: You can specify a file URL for the upload. The maximum size of a single file is 48.8 TB. This method requires downloading the web file to a local disk before uploading. Make sure that your local disk has sufficient space. For more information, see the `testUploadWebVideo` function in the sample code.
Upload M3U8 files
Upload acceleration
To upload large files (at the GB or TB level) or perform cross-region uploads, such as uploading a video from the Chinese mainland to a storage address in the Singapore storage region, you can enable the upload acceleration feature. For more information, see Enable upload acceleration. After you enable this feature, you must add the AccelerateConfig key-value pair to the UserData object in the upload configuration. If the `UserData` object does not exist, you must create it. The following is an example:
uploadVideoRequest.setUserData("{\"AccelerateConfig\":{\"Type\":\"oss\",\"Domain\":\"****Bucket.oss-accelerate.aliyuncs.com\"}}");
Parameter description
|
Name |
Type |
Description |
|
Type |
string |
The type of upload acceleration to enable. Only `oss` is supported. |
|
Domain |
string |
The accelerated domain name of your bucket. The default protocol is HTTPS. Note
Use an accelerated domain name assigned after activation, for example, `vod-*******.oss-accelerate.aliyuncs.com`. |
Scenario 2: Upload images
Scenario 3: Upload auxiliary media assets
FAQ
When you use the sample code to upload a web video stream, if the ERROR:root:Download fail: [Errno 2] No such file or directory error is reported, you can resolve the issue by manually creating a dlfiles folder in the same directory as the voduploadsdk folder.
/Users/fulin/Documents/Code/PythonProject/VodPythonUploadSdkTest/venv/bin/python /Users/fulin/Documents/Code/PythonProject/VodPythonUploadSdkTest/uploadVideo.py
[2022-02-16 20:54:59]Download https://img.a... To /Users/fulin/Documents/Code/PythonProject/
/Users/fulin... : InsecureRequestWarning: Unverifi...
warnings.warn(...)
[2022-02-16 20:54:59]Download fail: [Errno 2] No such file or directory: '/Users/fulin/Documents/Code/PythonProject/VodPythonUploadSdkTest/dlfiles/115...
Type: FileDownloadError, Code: Download File Error, Message: , HTTPStatus: NULL, RequestId: NULL
ERROR:root:Download fail: [Errno 2] No such file or directory: '/Users/fulin/Documents/Code/PythonProject/VodPythonUploadSdkTest/dlfiles/115310cd4d...
Process finished with exit code 0