本文通过示例详细介绍如何使用服务端上传SDK(Python语言)将各类媒体文件上传至点播存储。

上传流程

从内部逻辑角度看,Python上传SDK遵循点播服务端SDK的通用流程。详情请参见上传流程。从操作角度看,使用Python上传SDK的基础流程如下:

  1. 完成前提条件。具体操作请参见前提条件
  2. 集成Python上传SDK。具体操作请参见集成Python上传SDK
  3. 实现上传逻辑(主要是上传信息配置)。

前提条件

  • 您已经开通了视频点播服务。开通步骤请参见开通视频点播服务

  • 您已经完成上传相关的系统配置,包括启用目标存储地域的存储地址和配置回调。操作指引请参见存储管理回调设置
  • 您已准备好用于调用点播服务的账号。为避免阿里云账号AccessKey泄露带来的安全风险,推荐您创建RAM用户并授予其VOD相关权限。然后使用RAM用户的AK对(AccessKey ID和AccessKey Secret)访问点播服务。操作指引请参见创建RAM用户并授权
  • (可选)如需使用STS临时授权方式(阿里云Security Token Service)访问点播服务,请为RAM用户创建角色并授予角色VOD相关权限。操作指引请参见创建角色并进行STS临时授权
    说明 STS临时授权方式的适用场景请参见凭证方式与STS方式对比

集成Python上传SDK

说明 上传SDK目录详细介绍请参见下文目录说明
  1. 执行以下命令安装依赖包。

    pip install aliyun-python-sdk-core

    pip install aliyun-python-sdk-vod

    pip install oss2

    说明 如果使用的是 Python 3.x,请将pip install aliyun-python-sdk-core修改为pip install aliyun-python-sdk-core-v3。如果同时安装了不同版本,可使用pip3命令。如果集成遇到问题,请参见常见问题排查。
  2. 下载Python上传SDK及示例代码VodUploadSDK-Python_1.3.1.zip,更多信息,请参见SDK下载
    说明 此处以SDK1.3.1版本举例说明。其他版本请根据实际情况操作。
  3. 解压VodUploadSDK-Python_1.3.1.zip,将VodUploadSDK-Python_1.3.1目录下的voduploadsdk复制到本地项目工程目录下(推荐),或复制到site-packages目录下。

更新Python上传SDK

若发现新的接口或已有接口新的功能在当前SDK没有,可将SDK更新到最新版。

  1. 执行以下命令更新依赖包。

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

    pip install --upgrade oss2

  2. 下载最新的Python上传SDK覆盖到本地SDK文件。更多信息,请参见SDK下载
    说明 您可以打开voduploadsdk目录下的ChangeLog.txt文件查看当前SDK的版本号和发布日期。

音视频上传

普通文件上传
音视频上传目前支持以下类型文件上传:
  • 上传本地文件,使用分片上传,最大支持48.8 TB的单个文件,不支持断点续传。请参见示例代码中的testUploadLocalVideo函数。
  • 上传网络文件,可指定文件URL进行上传,最大支持48.8 TB的单个文件。该上传方式需要先将网络文件下载到本地磁盘,再进行上传,所以要保证本地磁盘有充足的空间。请参见testUploadWebVideo函数。
示例代码
# -*- coding: UTF-8 -*-
from voduploadsdk.AliyunVodUtils import *
from voduploadsdk.AliyunVodUploader import AliyunVodUploader
from voduploadsdk.UploadVideoRequest import UploadVideoRequest 

# 测试上传本地音视频
def testUploadLocalVideo(accessKeyId, accessKeySecret, filePath, storageLocation=None):
    try:
        # 可以指定上传脚本部署的ECS区域。如果ECS区域和视频点播存储区域相同,则自动使用内网上传,上传更快且更省公网流量。
        # ecsRegionId ="cn-shanghai"
        # uploader = AliyunVodUploader(accessKeyId, accessKeySecret, ecsRegionId)
        # 不指定上传脚本部署的ECS区域。
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadVideoRequest = UploadVideoRequest(filePath, 'exampleTitle')
        # 可以设置视频封面,如果是本地或网络图片可使用UploadImageRequest上传图片到视频点播,获取到ImageURL
        #ImageURL示例:https://example.com/sample-****.jpg
        #uploadVideoRequest.setCoverURL('<your Image URL>')  
        # 标签
        #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)
 
# 测试上传网络音视频
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)

####  执行测试代码   ####   
accessKeyId = '<AccessKeyId>'
accessKeySecret = '<AccessKeySecret>'

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

# fileURL示例:http://video.developer.aliyundoc.com/video/sample-****.mp4
fileUrl = '<your file URL>'
#testUploadWebVideo(accessKeyId, accessKeySecret, fileUrl)
M3U8文件上传
示例代码
# -*- coding: UTF-8 -*-
from voduploadsdk.AliyunVodUtils import *
from voduploadsdk.AliyunVodUploader import AliyunVodUploader
from voduploadsdk.UploadVideoRequest import UploadVideoRequest

# 测试上传m3u8本地视频
def testUploadLocalM3u8(accessKeyId, accessKeySecret, m3u8LocalFile):
    try:
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadVideoRequest = UploadVideoRequest(m3u8LocalFile, 'exampleTitle')
        # uploadVideoRequest.setTemplateGroupId('<TemplateGroupId>')
        # 分片文件和m3u8文件位于同一目录,SDK会自动解析上传
        videoId = uploader.uploadLocalM3u8(uploadVideoRequest)
        print("file: %s, videoId: %s" % (uploadVideoRequest.filePath, videoId))

    except AliyunVodException as e:
        print(e)

# 测试上传m3u8网络视频
def testUploadWebM3u8(accessKeyId, accessKeySecret, m3u8FileUrl):
    try:
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadVideoRequest = UploadVideoRequest(m3u8FileUrl, 'exampleTitle')
        # 解析分片文件地址(适用于分片地址和m3u8文件签名相同或无签名的情况,其它情况需要您自行解析)
        sliceFileUrls = uploader.parseWebM3u8(m3u8FileUrl)
        videoId = uploader.uploadWebM3u8(uploadVideoRequest, sliceFileUrls)
        print("file: %s, videoId: %s" % (uploadVideoRequest.filePath, videoId))

    except AliyunVodException as e:
        print(e)

####  执行测试代码   ####
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)
            

图片上传

示例代码
# -*- coding: UTF-8 -*-
from voduploadsdk.AliyunVodUtils import *
from voduploadsdk.AliyunVodUploader import AliyunVodUploader
from voduploadsdk.UploadImageRequest import UploadImageRequest

# 测试上传本地图片
def testUploadLocalImage(accessKeyId, accessKeySecret, filePath):
    try:
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadImageRequest = UploadImageRequest(filePath)
        uploadImageRequest.setTitle('exampleTitle')  # 设置图片标题,默认为空
        imageId, imageUrl = uploader.uploadImage(uploadImageRequest, True)
        print("file: %s, imageId: %s, imageUrl: %s" % (uploadImageRequest.filePath, imageId, imageUrl))

    except AliyunVodException as e:
        print(e)

# 测试上传网络图片
def testUploadWebImage(accessKeyId, accessKeySecret, fileUrl):
    try:
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadImageRequest = UploadImageRequest(fileUrl)
        uploadImageRequest.setTitle('exampleTitle')  # 设置图片标题,默认为空
        imageId, imageUrl = uploader.uploadImage(uploadImageRequest, False)
        print("file: %s, imageId: %s, imageUrl: %s" % (uploadImageRequest.filePath, imageId, imageUrl))

    except AliyunVodException as e:
        print(e)


####  执行测试代码   ####
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)
            

辅助媒资上传

示例代码
# -*- coding: UTF-8 -*-
from voduploadsdk.AliyunVodUtils import *
from voduploadsdk.AliyunVodUploader import AliyunVodUploader
from voduploadsdk.UploadAttachedMediaRequest import UploadAttachedMediaRequest


# 测试上传本地辅助媒资(水印、字幕等文件)
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)


# 测试上传网络辅助媒资(水印、字幕等文件)
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)


####  执行测试代码   ####
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)
            

常见问题

使用示例代码上传网络视频流时,如果报ERROR:root:Download fail: [Errno 2] No such file or directory错误,解决办法为:在voduploadsdk同级目录下手动创建dlfiles文件夹。错误

Python上传SDK目录说明

voduploadsdk目录
  • AliyunVodUtils.py
    目录 说明
    AliyunVodLog 上传SDK的日志类,基于logging实现。
    AliyunVodUtils 上传SDK的工具类。
    AliyunVodException 上传SDK的异常类,做统一的异常处理,外部捕获此异常即可。
  • UploadVideoRequest.py
    目录 说明
    UploadVideoRequest 上传视频的请求类,字段请参见获取音视频上传地址和凭证
  • UploadImageRequest.py
    目录 说明
    UploadImageRequest 上传图片的请求类,字段请参见获取图片上传地址和凭证
  • UploadAttachedMediaRequest.py
    目录 说明
    UploadAttachedMediaRequest 上传辅助媒资的请求类,字段请参见获取辅助媒资上传地址和凭证
  • AliyunVodUploader.py
    目录 说明
    uploadLocalVideo 上传本地视频的接口。
    uploadWebVideo 上传网络视频的接口。
    uploadLocalM3u8 上传本地m3u8视频。
    uploadWebM3u8 上传网络m3u8视频。
    uploadImage 上传本地或网络图片文件。
    uploadAttachedMedia 上传本地或网络辅助媒资文件。
    parseWebM3u8 解析网络m3u8文件的分片信息。
    parseLocalM3u8 解析本地m3u8文件的分片信息。
    setApiRegion 设置VoD的接入地址。默认为cn-shanghai(上海),海外支持ap-southeast-1(新加坡)等区域。详情请参见点播中心和访问域名
    setMultipartUpload 设置分片上传的阈值、分片大小。
    uploadProgressCallback 上传进度回调函数,可重写。
    setEnableCrc 上传时是否启用CRC校验,默认开启。
  • ChangeLog.txt:版本发布记录,首行即为当前SDK的版本号和发布日期。
samples目录
  • uploadVideo.py:上传视频的示例代码。
  • uploadImage.py:上传图片的示例代码。
  • uploadAttachedMedia.py:上传辅助媒资文件的示例代码。