[Best Practices for Video On Demand] Uploading Videos to On Demand Using the OSS SDK

Abstract: The scene-on-demand upload SDK lacks the required language version (such as C/C++, Go, etc.) or corresponding functions (such as network stream upload, additional upload), and can directly use the OSS SDK to upload. Preparations Confirm that the on-demand service has been activated and the relevant configuration has been completed. Confirm that the Alibaba Cloud account AK has been prepared and the upload permission has been granted.

Scenes

The on-demand upload SDK lacks the required language version (such as C/C++, Go, etc.) or corresponding functions (such as network stream upload, additional upload), and can directly use the OSS SDK to upload.

Ready to work

Confirm that the on-demand service has been activated and the related configuration has been completed. 
Confirm that the Alibaba Cloud account AK has been prepared and the upload permission has been granted.

Upload steps

image description

1. Access the VOD service to obtain the upload address and upload certificate .

In this step, a video media asset record will also be created, and the video ID will be returned. Please save it properly. Later, video playback, management, and AI processing can be performed according to the video ID.

2. Perform Base64 analysis on the upload address (UploadAddress) and upload certificate (UploadAuth) respectively to obtain the upload address and authorization information of the OSS.

After Base64 decoding of the UploadAddress field, a JSON format string is obtained, including the following fields:

image description

After Base64 decoding of the UploadAuth field, a JSON format string is obtained, which contains the following fields:

image description

3. Call the OSS SDK to upload the video file to the specified bucket. Be careful to use the STS Auth method, and use UploadAddress and UploadAuth for initialization. Do not use your own AK and other information.

Code

The core code implementation is divided into 4 steps:

1. Use AK to initialize the VOD client 
2. Obtain the video upload address and certificate 
3. Use the upload certificate and address to initialize the OSS client 
4. Upload local files

You can use the interface SDK ( Java , PHP , Python , .NET ) of the VOD service, or the API ( interface description , calling example ) to obtain the upload address and credentials.

Use OSS SDK to upload on the server, currently supports the following versions: 
OSS-Java-SDK , OSS-PHP-SDK , OSS-Python-SDK , OSS-C-SDK , OSS-Go-SDK , OSS-Ruby-SDK , OSS -.NET-SDK

It is recommended to use VOD upload SDKs for client upload: VOD-Android-SDK, VOD-iOS-SDK, VOD-JavaScript-SDK ; when uploading on the client side, you need to obtain the upload address and certificate from the server and send it to the client to ensure data Safety.

PHP upload example

Environmental preparation

PHP 5.3+, you can check the current PHP version with the php -v command. 
cURL extension, you can run the php -m command to check whether the curl extension has been installed.

Install

1. Add empty folder aliyun-php-sdk in your PHP project.

2. Download the entire source code from  aliyun-openapi-php-sdk  , decompress it, and copy the aliyun-php-sdk-core and aliyun-php-sdk-vod folders to the aliyun-php-sdk directory.

3. Download the source code of the latest OSS PHP SDK from  aliyun-oss-php-sdk  , decompress the ZIP file, and add a folder to the aliyun-php-sdk directory. Take downloading  v.2.2.4 Source code (zip)  as an example, the folder after decompression is aliyun-oss-php-sdk-2.2.4.

4. Open the aliyun-php-sdk/aliyun-php-sdk-core/Config.php file, find "//config sdk auto load path.", and add below this line:

Autoloader::addAutoloadPath("aliyun-php-sdk-vod");

5. Reference the VOD and OSS files in the code:

require_once './aliyun-php-sdk/aliyun-php-sdk-core/Config.php';   // 假定您的源码文件和aliyun-php-sdk处于同一目录。
require_once './aliyun-php-sdk/aliyun-oss-php-sdk-2.2.4/autoload.php';
use vod\Request\V20170321 as vod;
use OSS\OssClient;
use OSS\Core\OssException;

For more information, refer to VOD PHP SDK installation  and  OSS PHP SDK installation .

Reference Code

Functions that define core steps:

Initialize VOD client with AK

function init_vod_client($accessKeyId, $accessKeySecret) {
    $regionId = 'cn-shanghai';     // 点播服务所在的Region,国内请填cn-shanghai,不要填写别的区域
    $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
    return new DefaultAcsClient($profile);
}

Get video upload address and credentials

function create_upload_video($vodClient) {
    $request = new vod\CreateUploadVideoRequest();
    $request->setTitle("视频标题");        // 视频标题(必填参数)
    $request->setFileName("文件名称.mov"); // 视频源文件名称,必须包含扩展名(必填参数)
    $request->setDescription("视频描述");  // 视频源文件描述(可选)
    $request->setCoverURL("http://img.alicdn.com/tps/TB1qnJ1PVXXXXXCXXXXXXXXXXXX-700-700.png"); // 自定义视频封面(可选)
    $request->setTags("标签1,标签2"); // 视频标签,多个用逗号分隔(可选)
    return $vodClient->getAcsResponse($request);
}

Initialize the OSS client using the upload credentials and address (note that you need to decode Base64 and Json Decode before passing in)

function init_oss_client($uploadAuth, $uploadAddress) {
    $ossClient = new OssClient($uploadAuth['AccessKeyId'], $uploadAuth['AccessKeySecret'], $uploadAddress['Endpoint'], 
        false, $uploadAuth['SecurityToken']);
    $ossClient->setTimeout(86400*7);    // 设置请求超时时间,单位秒,默认是5184000秒, 建议不要设置太小,如果上传文件很大,消耗的时间会比较长
    $ossClient->setConnectTimeout(10);  // 设置连接超时时间,单位秒,默认是10秒
    return $ossClient;
}

Upload local files

function upload_local_file($ossClient, $uploadAddress, $localFile) {
    return $ossClient->uploadFile($uploadAddress['Bucket'], $uploadAddress['FileName'], $localFile);
}

Refresh upload credentials

function refresh_upload_video($vodClient, $videoId) {
    $request = new vod\RefreshUploadVideoRequest();
    $request->setVideoId($videoId);
    return $vodClient->getAcsResponse($request);
}
执行完整流程(注意捕获异常):
$accessKeyId = '<AccessKeyId>';                    // 您的AccessKeyId
$accessKeySecret = '<AccessKeySecret>';            // 您的AccessKeySecret
$localFile = '/Users/yours/Video/testVideo.flv';   // 需要上传到VOD的本地视频文件的完整路径
try {
    // 初始化VOD客户端并获取上传地址和凭证
    $vodClient = init_vod_client($accessKeyId, $accessKeySecret);
    $createRes = create_upload_video($vodClient);
    // 执行成功会返回VideoId、UploadAddress和UploadAuth
    $videoId = $createRes->VideoId;
    $uploadAddress = json_decode(base64_decode($createRes->UploadAddress), true);
    $uploadAuth = json_decode(base64_decode($createRes->UploadAuth), true);
    // 使用UploadAuth和UploadAddress初始化OSS客户端
    $ossClient = init_oss_client($uploadAuth, $uploadAddress);
    // 上传文件,注意是同步上传会阻塞等待,耗时与文件大小和网络上行带宽有关
    //$result = upload_local_file($ossClient, $uploadAddress, $localFile);
    $result = multipart_upload_file($ossClient, $uploadAddress, $localFile);
    printf("Succeed, VideoId: %s", $videoId);
} catch (Exception $e) {
    // var_dump($e);
    printf("Failed, ErrorMessage: %s", $e->getMessage());
}

Demo download

You can download the complete  PHP version to upload the demo source code . For more usage information, please refer to the  OSS-PHP-SDK upload file.

In order to support more entrepreneurs and lower the entry threshold, VOD has launched a premium experience package based on the five original VOD service packages.

For only 9.9 yuan, you can get 10GB of traffic, 50GB of storage, and 100 minutes of transcoding, which can be used by individuals and small and micro enterprise portals.

image description

Click to enter the event page, purchase immediately, and complete the payment

image description

Enter the VOD product page , click Activate Now, activate the VOD service, and perform initial configuration. Users can perform video uploading, transcoding settings, video management, and video preview operations on the console. 
image description

In addition, after the experience is completed, Alibaba Cloud also provides five on-demand packages for users to choose from.

image description

Click to view purchase details

To read more good articles, please scan the following QR code:

image description

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325175284&siteId=291194637