This topic describes how to use the upload SDK for PHP to upload media files to ApsaraVideo VOD for storage.
Upload process
The internal upload logic used by the server upload SDK for PHP is the same as that
of the general upload process of server upload SDKs. For more information, see the
"Upload process" section of the Upload process topic. The following content describes the basic process of using the server upload
SDK for PHP:
- Complete the prerequisites. For more information, see the Prerequisites section.
- Integrate the upload SDK for PHP. For more information, see the Integrate the upload SDK for PHP section.
- Implement the upload logic. This involves upload information configuration.
- For more information about the sample code for uploading audio and video files, see the Upload audio and video files section.
- For more information about the sample code for uploading images, see the Upload images section.
- For more information about the sample code for uploading auxiliary media assets, see the Upload auxiliary media assets section.
Prerequisites
-
ApsaraVideo VOD is activated. For more information, see Activate ApsaraVideo VOD.
- The system settings required for uploading media files, including the OSS bucket for storing the media files in the specified region and the callback settings, are configured. For more information, see Manage VOD storage 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 Create a role and grant temporary access permissions to the role by using STS.
Note For more information about the scenarios in which STS can be used, see Comparison between credentials and STS.
Integrate the upload SDK for PHP
Update the upload SDK for PHP
If new methods or new features of existing methods are unavailable in the current SDK, update the SDK to the latest version. For more information, see SDK download.
Note You can check the version number and release date of the current SDK in the first
line of the ChangeLog.txt file in the voduploadsdk directory.
Upload audio and video files
Ordinary audio and video filesThe upload SDK for PHP supports the following types of audio and video files:
- Local files. Multipart upload is used. A single file to be uploaded can be up to 48.8 TB in size. Resumable upload is not supported. For more information, see the testUploadLocalVideo function in the following sample code.
- Online files. You can specify upload URLs to upload online files. A single file to be uploaded can be 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 following sample code.
Part of the sample code for uploading ordinary videos:
<?php
/**
* Created by Aliyun ApsaraVideo VOD.
*/
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'voduploadsdk' . DIRECTORY_SEPARATOR . 'Autoloader.php';
date_default_timezone_set('PRC');
// Test the upload of a local video.
function testUploadLocalVideo($accessKeyId, $accessKeySecret, $filePath)
{
try {
$uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
$uploadVideoRequest = new UploadVideoRequest($filePath, 'testUploadLocalVideo via PHP-SDK');
//$uploadVideoRequest->setCateId(1);
// The URL of the video thumbnail. Example: 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(
// The callback URL. Example: 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());
}
}
// Test the upload of an online video.
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());
}
}
#### Run the test code. ####
$accessKeyId = '<AccessKeyId>';
$accessKeySecret = '<AccessKeySecret>';
//$localFilePath = 'C:\test\sample.mp4';
$localFilePath = '/opt/video/sample.mp4';
//testUploadLocalVideo($accessKeyId, $accessKeySecret, $localFilePath);
// The URL of the online video. Example: 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 videosPart of the sample code for uploading M3U8 videos:
<?php
/**
* Created by Aliyun ApsaraVideo VOD.
*/
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'voduploadsdk' . DIRECTORY_SEPARATOR . 'Autoloader.php';
date_default_timezone_set('PRC');
// Test the upload of a local M3U8 video.
function testUploadLocalM3u8($accessKeyId, $accessKeySecret, $m3u8FilePath)
{
try {
$uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
$uploadVideoRequest = new UploadVideoRequest($m3u8FilePath, 'testUploadLocalM3u8 via PHP-SDK');
// Call the method for parsing the M3U8 playlist to obtain the URLs of parts. If the parsing result is invalid, manually assemble the URLs of parts. By default, the part files and M3U8 file are stored in the same directory.
$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());
}
}
// Test the upload of an online M3U8 video.
function testUploadWebM3u8($accessKeyId, $accessKeySecret, $m3u8FileUrl)
{
try {
$uploader = new AliyunVodUploader($accessKeyId, $accessKeySecret);
$uploadVideoRequest = new UploadVideoRequest($m3u8FileUrl, 'testUploadWebM3u8 via PHP-SDK');
// Call the method for parsing the M3U8 playlist to obtain the URLs of parts. If the parsing result is invalid, manually assemble the URLs of parts. By default, the part files and M3U8 file are stored in the same directory.
$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());
}
}
#### Run the test code. ####
$accessKeyId = '<AccessKeyId>';
$accessKeySecret = '<AccessKeySecret>';
$localM3u8FilePath = '/opt/video/m3u8/sample.m3u8';
//testUploadLocalM3u8($accessKeyId, $accessKeySecret, $localM3u8FilePath);
// The URL of the online M3U8 video. Example: 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);
Upload images
Part of the sample code for uploading images:
<?php
/**
* Created by Aliyun ApsaraVideo VoD.
* User: https://www.aliyun.com/product/vod
*/
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'voduploadsdk' . DIRECTORY_SEPARATOR . 'Autoloader.php';
date_default_timezone_set('PRC');
# Test the upload of a local image.
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());
}
}
// Test the upload of an online image.
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());
}
}
#### Run the test code. ####
$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);
Upload auxiliary media assets
Part of the sample code for uploading auxiliary media assets:
<?php
/**
* Created by Aliyun ApsaraVideo VoD.
* User: https://www.aliyun.com/product/vod
*/
require_once dirname(__DIR__) . DIRECTORY_SEPARATOR . 'voduploadsdk' . DIRECTORY_SEPARATOR . 'Autoloader.php';
date_default_timezone_set('PRC');
// Test the upload of a local auxiliary media asset.
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());
}
}
// Test the upload of an online auxiliary media asset.
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());
}
}
#### Run the test code. ####
$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);
Directories of the upload SDK for PHP
/VodUploadSDK-PHP_1.0.3.zip decompress directory/VodUploadSDK-PHP_1.0.3/voduploadsdk/uploader- UploadVideoRequest.php
Directory Description UploadVideoRequest The request class for uploading videos. For more information about the parameters, see CreateUploadVideo. - UploadImageRequest.php
Directory Description UploadImageRequest The request class for uploading images. For more information about the parameters, see CreateUploadImage. - UploadAttachedMediaRequest.php
Directory Description UploadAttachedMediaRequest The request class for uploading auxiliary media assets. For more information about the parameters, see CreateUploadAttachedMedia. - AliyunVodUploader.php
Directory Description __construct The constructor used to set the AccessKey pair and ApsaraVideo VOD endpoint for the upload. For more information, see Create and grant permissions to a RAM user and Region IDs of ApsaraVideo VOD. uploadLocalVideo The method used to upload local videos. uploadWebVideo The method used to upload online videos. uploadLocalImage The method used to upload local images. uploadWebImage The method used to upload online images. uploadLocalAttachedMedia The method used to upload local auxiliary media assets. uploadWebAttachedMedia The method used to upload online auxiliary media assets. uploadLocalM3u8 The method used to upload local M3U8 videos. uploadWebM3u8 The method used to upload online M3U8 videos. parseM3u8File The method used to parse the M3U8 playlist to obtain the URLs of parts. setEcsRegionId The method used to specify the region of the Elastic Compute Service (ECS) instance on which the upload script is to be deployed. If the region of the ECS instance is the same as the storage region in ApsaraVideo VOD, the file is automatically uploaded over the internal network. setEnableSSL The method used to specify whether to enable SSL for HTTPS requests. By default, SSL is disabled to prevent the failure to use the SDK when related extensions are not installed or a configuration exception occurs. uploadProgressCallback The method used to set the callback for upload progress. This method can be overridden. - AliyunVodUtils.php
Directory Description AliyunVodUtils The utility class, which provides static functions for truncating strings and obtaining extensions and file names. AliyunVodLog The log class, which provides a simple logging feature. You can set the logSwitch parameter to determine whether to enable logging. AliyunVodDownloader Downloads online files. AliyunVodReportUpload Reports the upload progress. AliyunVodError Defines error codes.
- aliyun-php-sdk-core: provides the base class on which the upload SDK depends. This package encapsulates Alibaba Cloud API signatures and HTTP requests.
- aliyun-php-sdk-vod: provides the server SDKs of ApsaraVideo VOD. This package encapsulates API requests of ApsaraVideo VOD.
- aliyun-php-sdk-oss: provides the Object Storage Service (OSS) class on which the upload SDK depends. This package encapsulates operations such as OSS uploads.
- uploadVideo.php: provides the sample code for uploading videos.
- uploadImage.php: provides the sample code for uploading images.
- uploadAttachedMedia.php: provides the sample code for uploading auxiliary media assets.