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

上传流程

从内部逻辑角度看,PHP上传SDK遵循点播服务端SDK的通用流程。详情请参见上传流程。从操作角度看,使用PHP上传SDK的基础流程如下:
  1. 完成前提条件。请参见前提条件
  2. 集成PHP上传SDK。请参见集成PHP上传SDK
  3. 实现上传逻辑(主要是上传信息配置)。

前提条件

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

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

集成PHP上传SDK

  1. 下载PHP上传SDK及示例代码VodUploadSDK-PHP_1.0.3.zip,请参见服务端上传SDK
    说明
    • 此处以SDK1.0.3版本举例说明。其他版本请根据实际情况操作。
    • SDK及示例代码解压后的详细目录说明,请参见下文目录说明
  2. 解压VodUploadSDK-PHP_1.0.3.zip,将VodUploadSDK-PHP_1.0.3目录下的voduploadsdk复制到您的项目中。

更新PHP上传SDK

若发现新的接口或已有接口新的功能在当前SDK没有,请下载最新的PHP上传SDK覆盖到本地SDK文件。更多信息,请参见服务端上传SDK

说明 您可以打开voduploadsdk目录下的ChangeLog.txt文件查看当前SDK的版本号和发布日期。

音视频上传

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

部分代码示例如下所示:

<?php
/**
 * Created by Aliyun ApsaraVideo VOD.
 */
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'voduploadsdk' . DIRECTORY_SEPARATOR . 'Autoloader.php';
date_default_timezone_set('PRC');
//测试上传本地视频
function testUploadLocalVideo($accessKeyId, $accessKeySecret, $filePath)
{
    try {
        $uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
        $uploadVideoRequest = new UploadVideoRequest($filePath, 'testUploadLocalVideo via PHP-SDK');
        //$uploadVideoRequest->setCateId(1);
        //CoverURL示例:http://example.com/example****.jpg
        //$uploadVideoRequest->setCoverURL("<your CoverURL>");
        //$uploadVideoRequest->setTags('test1,test2');
        //$uploadVideoRequest->setStorageLocation('outin-xx.oss-cn-beijing.aliyuncs.com');
        //$uploadVideoRequest->setTemplateGroupId('6ae347b0140181ad371d197ebe289****');
        $userData = array(
            //Callback示例:https://demo.aliyundoc.com/ProcessMessageCallback
            "MessageCallback"=>array("CallbackURL"=>"<your callback URL>"),
            "Extend"=>array("localId"=>"xxx", "test"=>"www")
        );
        $uploadVideoRequest->setUserData(json_encode($userData));
        $res = $uploader->uploadLocalVideo($uploadVideoRequest);
        print_r($res);
    } catch (Exception $e) {
        printf("testUploadLocalVideo Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
            $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
    }
}
// 测试上传网络视频
function testUploadWebVideo($accessKeyId, $accessKeySecret, $fileURL)
{
    try {
        $uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
        $uploadVideoRequest = new UploadVideoRequest($fileURL, 'testUploadWebVideo via PHP-SDK');
        $res = $uploader->uploadWebVideo($uploadVideoRequest);
        print_r($res);
    } catch (Exception $e) {
        printf("testUploadWebVideo Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
            $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
    }
}

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

//$localFilePath = 'C:\test\sample.mp4';
$localFilePath = '/opt/video/sample.mp4';
//testUploadLocalVideo($accessKeyId, $accessKeySecret, $localFilePath);

//webFileURL示例:http://example-bucket-****.cn-shanghai.aliyuncs.com/b55b904bc612463b812990b7c8cc****/daa30814c0c340cf8199926f78aa****-a0bc05ba62c3e95cc672e88b8281****-ld.mp4?auth_key=1608774986-0-0-c56acd302bea0c331370d8ed6865****
$webFileURL = '<your webFileURL>';
testUploadWebVideo($accessKeyId, $accessKeySecret, $webFileURL);
M3U8视频文件

部分代码示例如下所示:

<?php
/**
 * Created by Aliyun ApsaraVideo VOD.
 */
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'voduploadsdk' . DIRECTORY_SEPARATOR . 'Autoloader.php';
date_default_timezone_set('PRC');
// 测试上传本地m3u8视频
function testUploadLocalM3u8($accessKeyId, $accessKeySecret, $m3u8FilePath)
{
    try {
        $uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
        $uploadVideoRequest = new UploadVideoRequest($m3u8FilePath, 'testUploadLocalM3u8 via PHP-SDK');
        // 调用接口解析m3u8的分片地址列表,如果解析结果不准确,请自行拼接地址列表(默认分片文件和m3u8文件位于同一目录)
        $sliceFiles = $uploader->parseM3u8File($m3u8FilePath);
        //print_r($sliceFiles);
        $res = $uploader->uploadLocalM3u8($uploadVideoRequest, $sliceFiles);
        print_r($res);
    } catch (Exception $e) {
        printf("testUploadLocalM3u8 Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
            $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
    }
}

// 测试上传网络m3u8视频
function testUploadWebM3u8($accessKeyId, $accessKeySecret, $m3u8FileUrl)
{
    try {
        $uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
        $uploadVideoRequest = new UploadVideoRequest($m3u8FileUrl, 'testUploadWebM3u8 via PHP-SDK');
        // 调用接口解析m3u8的分片地址列表,如果解析结果不准确,请自行拼接地址列表(默认分片文件和m3u8文件位于同一目录)
        $sliceFileUrls = $uploader->parseM3u8File($m3u8FileUrl);
        //print_r($sliceFileUrls);
        $res = $uploader->uploadWebM3u8($uploadVideoRequest, $sliceFileUrls);
        print_r($res);
    } catch (Exception $e) {
        printf("testUploadWebM3u8 Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
            $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
    }
}
####  执行测试代码   ####
$accessKeyId = '<AccessKeyId>';
$accessKeySecret = '<AccessKeySecret>';
$localM3u8FilePath = '/opt/video/m3u8/sample.m3u8';
//testUploadLocalM3u8($accessKeyId, $accessKeySecret, $localM3u8FilePath);

//webM3u8FileURL示例:http://example-bucket-****.cn-shanghai.aliyuncs.com/b55b904bc612463b812990b7c8cc****/daa30814c0c340cf8199926f78aa****-195a25af366b5edae324c47e99a0****-ld.m3u8?auth_key=1608775606-0-0-9fb038deaecd009dadd86721c585****
$webM3u8FileURL = '<your webM3u8FileURL>';
//testUploadWebM3u8($accessKeyId, $accessKeySecret, $webM3u8FileURL);

图片上传

部分代码示例如下所示:

<?php
/**
 * Created by Aliyun ApsaraVideo VoD.
 * User: https://www.aliyun.com/product/vod
 * API document: https://help.aliyun.com/document_detail/55619.html
 */

require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'voduploadsdk' . DIRECTORY_SEPARATOR . 'Autoloader.php';

date_default_timezone_set('PRC');

// 测试上传本地图片
function testUploadLocalImage($accessKeyId, $accessKeySecret, $filePath)
{
    try {
        $uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
        $uploadImageRequest = new UploadImageRequest($filePath, 'testUploadLocalImage via PHP-SDK');
        $uploadImageRequest->setCateId(1000009458);
        $res = $uploader->uploadLocalImage($uploadImageRequest);
        print_r($res);
    } catch (Exception $e) {
        printf("testUploadLocalImage Failed, ErrorMessage: %s\n", $e->getMessage());
    }

}

// 测试上传网络图片
function testUploadWebImage($accessKeyId, $accessKeySecret, $fileURL)
{
    try {
        $uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
        $uploadImageRequest = new UploadImageRequest($fileURL, 'testUploadWebImage via PHP-SDK');
        $uploadImageRequest->setCateId(1000009458);
        $res = $uploader->uploadWebImage($uploadImageRequest);
        print_r($res);
    } catch (Exception $e) {
        printf("testUploadWebImage Failed, ErrorMessage: %s\n", $e->getMessage());
    }

}



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

$localFilePath = '/opt/image/test-image.jpg';
//testUploadLocalImage($accessKeyId, $accessKeySecret, $localFilePath);

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

辅助媒资文件上传

部分代码示例如下所示:

<?php
/**
 * Created by Aliyun ApsaraVideo VoD.
 * User: https://www.aliyun.com/product/vod
 * API document: https://help.aliyun.com/document_detail/98467.html
 */

require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'voduploadsdk' . DIRECTORY_SEPARATOR . 'Autoloader.php';

date_default_timezone_set('PRC');

// 测试上传本地辅助媒资文件
function testUploadLocalAttachedMedia($accessKeyId, $accessKeySecret, $filePath)
{
    try {
        $uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
        $uploadAttachedRequest = new UploadAttachedMediaRequest($filePath, 'watermark',
            'testUploadLocalAttachedMedia via PHP-SDK');
        //$uploadAttachedRequest->setCateId(100000****);
        $res = $uploader->uploadLocalAttachedMedia($uploadAttachedRequest);
        print_r($res);
    } catch (Exception $e) {
        printf("testUploadLocalAttachedMedia Failed, ErrorMessage: %s\n", $e->getMessage());
    }

}

// 测试上传网络辅助媒资文件
function testUploadWebAttachedMedia($accessKeyId, $accessKeySecret, $fileURL)
{
    try {
        $uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
        $uploadAttachedRequest = new UploadAttachedMediaRequest($fileURL, 'watermark',
            'testUploadWebAttachedMedia via PHP-SDK');
        //$uploadAttachedRequest->setCateId(100000****);
        $res = $uploader->uploadWebAttachedMedia($uploadAttachedRequest);
        print_r($res);
    } catch (Exception $e) {
        printf("testUploadWebAttachedMedia Failed, ErrorMessage: %s\n", $e->getMessage());
    }

}


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

$localFilePath = '/opt/image/test.png';
//testUploadLocalAttachedMedia($accessKeyId, $accessKeySecret, $localFilePath);

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

PHP上传SDK目录说明

/VodUploadSDK-PHP_1.0.3.zip解压目录/VodUploadSDK-PHP_1.0.3/voduploadsdk/uploader
  • UploadVideoRequest.php
    目录 说明
    UploadVideoRequest 上传视频的请求类,字段请参见获取音视频上传地址和凭证
  • UploadImageRequest.php
    目录 说明
    UploadImageRequest 上传图片的请求类,字段请参见获取图片上传地址和凭证
  • UploadAttachedMediaRequest.php
    目录 说明
    UploadAttachedMediaRequest 上传辅助媒资的请求类,字段请参见获取辅助媒资上传地址和凭证
  • AliyunVodUploader.php
    目录 说明
    __construct 可设置上传的AccessKey以及视频点播中心和访问域名。请参见AccessKey点播地域标识
    uploadLocalVideo 上传本地视频的接口。
    uploadWebVideo 上传网络视频的接口。
    uploadLocalImage 上传本地图片。
    uploadWebImage 上传网络图片。
    uploadLocalAttachedMedia 上传本地辅助媒资文件。
    uploadWebAttachedMedia 上传网络辅助媒资文件。
    uploadLocalM3u8 上传本地m3u8文件。
    uploadWebM3u8 上传网络m3u8文件。
    parseM3u8File 解析m3u8索引文件得到分片地址列表。
    setEcsRegionId 设置上传脚本部署的ECS区域(如果有),如果与视频点播存储同一区域会自动启用内网上传。
    setEnableSSL 是否启用SSL(网络请求使用HTTPS),默认不启用,以避免相关扩展未安装或配置异常时无法使用。
    uploadProgressCallback 上传进度回调函数,可重写。
  • AliyunVodUtils.php
    目录 说明
    AliyunVodUtils 工具类,提供截取字符串、获取扩展名、获取文件名等静态函数。
    AliyunVodLog 实现简单打印的日志类,logSwitch为日志开关。
    AliyunVodDownloader 实现下载网络文件。
    AliyunVodReportUpload 实现上传进度汇报。
    AliyunVodError 定义错误码。
/VodUploadSDK-PHP_1.0.3.zip解压目录/VodUploadSDK-PHP_1.0.3/voduploadsdk
  • aliyun-php-sdk-core:上传SDK依赖的基础类,封装了阿里云API签名和HTTP请求等。
  • aliyun-php-sdk-vod:视频点播的服务端接口SDK,封装了视频点播API的请求。
  • aliyun-php-sdk-oss:上传SDK依赖的OSS类,封装了OSS上传等操作。
/VodUploadSDK-PHP_1.0.3.zip解压目录/VodUploadSDK-PHP_1.0.3/samples
  • uploadVideo.php:上传视频的示例代码。
  • uploadImage.php:上传图片的示例代码。
  • uploadAttachedMedia.php:上传辅助媒资的示例代码。