All Products
Search
Document Center

Python upload SDK

Last Updated: Dec 04, 2018

 Overview

ApsaraVideo VOD is built based on OSS. After you activate ApsaraVideo VOD, a dedicated input bucket and a dedicated output bucket are allocated. The input bucket is used to store video or audio source files. The output bucket is used to store transcoded files, screenshots, and album arts, and is used to serve as the CDN origin site for VOD. You can upload images and other media resources to VOD. You can use ApsaraVideo Upload SDK to quickly upload media files.

 Functions

ApsaraVideo Upload SDK has the following functions:

  1. Upload a local video to VOD. Multi-fragment upload is used by default. The maximum size of a single file that can be uploaded is 48.8 TB. Resumable upload is not supported currently.
  2. Upload a network video to VOD. You can specify a URL and the system automatically downloads the video from the URL and uploads it to VOD. The maximum size of a single file that can be uploaded is 5 GB.
  3. Upload a local m3u8 video (including all the fragment files) to VOD. You just need to specify the address of the local m3u8 index file.
  4. Upload a network m3u8 video (including all the fragment files) to VOD. You must specify the URLs of the m3u8 index file and fragment files.
  5. Upload a local image to VOD. The output bucket is used by default to facilitate distribution acceleration.
  6. Upload a network image to VOD. You can specify a URL and the system automatically downloads the image from the URL and uploads it to VOD.

 Features

  1. Automatically upload the file through the optimal route: You need to specify the region of the ECS instance on which the upload script is deployed. If the ECS instance and the storage are in the same region, typically in China (Shanghai), the file is automatically uploaded through the Intranet to accelerate the upload speed and save the public traffic.
  2. Unified exception handling. For external calls, the system only needs to detect AliyunVodException exceptions (in AliyunVodUtils.py).
  3. Easy log output. For more information, see the AliyunVodLog class (in AliyunVodUtils.py).

2. SDK installation

 Environment requirements

 Install the SDK

  1. Install dependency packages

    1. pip install aliyun-python-sdk-core
    2. pip install aliyun-python-sdk-vod
    3. pip install oss2

    If some operations failed due to permission issues, add sudo before the command.

  2. Download the SDKDownload address: Download ApsaraVideo Upload SDK.Download a Python version. Decompress the package and copy voduploadsdk in the VodUploadSDK-Python directory. Then, you can use the SDK. The samples directory contains the sample code.

 Update the SDK

If the current SDK does not support the new operations or the new operations do not support the new functions, you can update it to the latest version.

  1. Update the dependency packages
    1. pip install --upgrade aliyun-python-sdk-vod
    2. pip install --upgrade oss2
  2. Download the latest ApsaraVideo Upload SDK Python edition.

3. How to use the SDK

 SDK structure

voduploadsdk directory

  • AliyunVodUtils.py file
    • AliyunVodLog: the log class. It is implemented based on logging.
    • AliyunVodUtils: the tool class. It provides static functions for truncating strings and obtaining extensions and file names.
    • AliyunVodException: the exception class. It supports unified exception handling. You only need to detect this exception for external calls.
  • UploadVideoRequest.py file.
    • UploadVideoRequest: the upload video request class. For more information about the request and response parameters, see the CreateUploadVideo operation.
  • UploadImageRequest.py file
    • UploadImageRequest: the upload image request class. For more information about the request and response parameters, see the CreateUploadImage operation.
  • AliyunVodUploader.py file, mainly includes operations in the AliyunVodUploader class.
    • uploadLocalVideo: local video upload operation.
    • uploadWebVideo: network video upload operation.
    • uploadLocalM3u8: local m3u8 video upload operation.
    • uploadWebM3u8: network m3u8 video upload operation.
    • uploadImage: local or network image upload operation.
    • parseWebM3u8: operation for parsing the fragmentation information of m3u8 network files.
    • setMultipartUpload: operation for setting the threshold and fragment size in multi-fragment mode.

samples directory

  • uploadVideo.py: sample code for video upload.
  • uploadImage.py: sample code for image upload.

 sample code

The following uses uploading a local video and a network video file as an example:

  1. # -*- coding: UTF-8 -*-
  2. from voduploadsdk.AliyunVodUtils import *
  3. from voduploadsdk.AliyunVodUploader import AliyunVodUploader
  4. from voduploadsdk.UploadVideoRequest import UploadVideoRequest
  5. # Test uploading of a local video
  6. def testUploadLocalVideo(accessKeyId, accessKeySecret, filePath):
  7. try:
  8. uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
  9. uploadVideoRequest = UploadVideoRequest(filePath, 'test upload local video')
  10. # Sets the video album art. For a local or network image, you can call UploadImageRequest to upload the image to VOD and obtain the ImageURL.
  11. #uploadVideoRequest.setCoverURL('https://sample.com/sample.jpg')
  12. #uploadVideoRequest.setTags('test1,test2')
  13. videoId = uploader.uploadLocalVideo(uploadVideoRequest)
  14. print "file: %s, videoId: %s" % (filePath, videoId)
  15. except AliyunVodException,e:
  16. print e
  17. # Test uploading of a network video
  18. def testUploadWebVideo(accessKeyId, accessKeySecret, fileUrl):
  19. try:
  20. uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
  21. uploadVideoRequest = UploadVideoRequest(fileUrl, 'test upload web video')
  22. uploadVideoRequest.setTags('test1,test2')
  23. videoId = uploader.uploadWebVideo(uploadVideoRequest)
  24. print "file: %s, videoId: %s" % (fileUrl, videoId)
  25. except AliyunVodException,e:
  26. print e
  27. #### Execute the test code ####
  28. accessKeyId = '<your AccessKeyId>'
  29. accessKeySecret = '<your AccessKeySecret>'
  30. localFilePath = '/Users/yours/Desktop/Video/sample.mov'
  31. testUploadLocalVideo(accessKeyId, accessKeySecret, localFilePath)
  32. fileUrl = 'http://vod-test2.cn-shanghai.aliyuncs.com/72f8b5e970024318b672f2e9c7f610f2/0e9fb231b82b41ef8703a5c1882a7478-5287d2089db37e62345123a1be272f8b.mp4'
  33. testUploadWebVideo(accessKeyId, accessKeySecret, fileUrl)

For more sample code, see the samples directory in the VodUploadSDK-Python directory after decompression.