Abstract: The scene on demand upload SDK lacks required language versions (such as C/C++, Go, etc.) or corresponding functions (such as network stream upload, add-on upload), and can be directly uploaded using the OSS SDK. Preparations The VOD service has been enabled and related configurations have been completed. Confirm that you have prepared the Aliyun account AK and granted the upload permission.

scenario

The ON-DEMAND upload SDK lacks required language versions (such as C/C++, Go, etc.) or corresponding functions (such as network stream upload and add-on upload), so you can directly upload using the OSS SDK.

The preparatory work

Ensure that the VOD service is enabled and related configurations are complete. Confirm that you have prepared the Aliyun account AK and granted the upload permission.

Upload step

Access the voD service to obtain the upload address and upload credentials.

In this step, a video media asset record is created and the video ID is returned. Keep the video ID safe. You can play, manage, and process videos based on the VIDEO ID.

UploadAddress and UploadAuth are Base64 resolved to obtain the UploadAddress and authorization information of OSS.

When the UploadAddress field Base64 is decoded, a string in JSON format is obtained, including the following fields:

When the UploadAuth field Base64 is decoded, a string in JSON format is obtained, including the following fields:

3. Call the OSS SDK to upload the video file to the specified bucket, pay attention to the STS Auth mode, and use UploadAddress and UploadAuth to initialize, do not use your own AK information.

Code implementation

Core code implementation is divided into 4 steps:

1. Initialize the VOD client using the AK. 2. Initialize the OSS client using the upload credentials and address. 4. Upload the local file

You can use the INTERFACE SDK of voD service (Java, PHP, Python,.net) or API (interface description, invocation example) to obtain the upload address and credentials.

Upload the OSS SDK on the server. Currently, OSS- java-SDK, OSS- php-sdk, OSS- python-sdk, OSS-C-SDK, OSS- GO-SDK, OSS-Ruby-SDK, oss-.net-sdk are supported

It is recommended to use the VOD upload SDK: vod-Android-SDK, vod-ios-sdk, and vod-javascript-sdk. To ensure data security, the client must obtain the upload address and certificate from the server and send the certificate to the client.

PHP upload example

Environment to prepare

PHP 5.3+, you can view the current VERSION of PHP by using the PHP -v command. To check whether the cURL extension is installed, run the PHP -m command.

The installation

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

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

3. Download the latest oss PHP SDK source code from aliyun-oss-php-sdk, decompress the ZIP file, and add the folder to aliyun-php-sdk. For example, download V. 2.2.4 Source Code (zip). The aliyun-oss-php-SDK-2.2.4 folder is decompressed.

4. Open the aliyun-php-sdk/aliyun-php-sdk-core/ config. PHP file, find // Config SDK auto load path.

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

5. Reference VOD and OSS files in code:

require_once './aliyun-php-sdk/aliyun-php-sdk-core/Config.php'; // Assume that your source files are in the same directory as 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;Copy the code

See VOD PHP SDK Installation and OSS PHP SDK Installation for more information.

Reference code

Functions that define core steps:

Initialize the VOD client using AK

function init_vod_client($accessKeyId.$accessKeySecret) {
    $regionId = 'cn-shanghai'; // Region where the vod service is located, please fill in CN-Shanghai, do not fill in other Region$profile = DefaultProfile::getProfile($regionId.$accessKeyId.$accessKeySecret);
    return new DefaultAcsClient($profile);
}Copy the code

Obtain the video uploading address and credentials

function create_upload_video($vodClient) {
    $request = new vod\CreateUploadVideoRequest();
    $request->setTitle("Video Title"); // Video title (mandatory)$request->setFileName("File name.mov"); // The name of the video source file, which must include the extension (mandatory)$request->setDescription("Video Description"); // Video source file description (optional)$request->setCoverURL("http://img.alicdn.com/tps/TB1qnJ1PVXXXXXCXXXXXXXXXXXX-700-700.png"); // Custom video cover (optional)$request->setTags("Tag 1, tag 2"); // Video labels, multiple comma separated (optional)return $vodClient->getAcsResponse($request);
}Copy the code

Initialize OSS client with upload credentials and address (note Base64 Decode and Json Decode are required)

function init_oss_client($uploadAuth.$uploadAddress) {
    $ossClient = new OssClient($uploadAuth['AccessKeyId'].$uploadAuth['AccessKeySecret'].$uploadAddress['Endpoint'].false.$uploadAuth['SecurityToken']);
    $ossClient->setTimeout(86400*7); The default value is 5184000 seconds. Do not set the timeout value too small. If a large file is uploaded, it takes a long time$ossClient->setConnectTimeout(10); // Set connection timeout in seconds. Default is 10 secondsreturn $ossClient;
}Copy the code

Uploading a Local File

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

Update upload credentials

function refresh_upload_video($vodClient.$videoId) {
    $request = new vod\RefreshUploadVideoRequest();
    $request->setVideoId($videoId);
    return $vodClient->getAcsResponse($request); } Execute the full process (note catching exceptions) :$accessKeyId = '<AccessKeyId>'; / / your AccessKeyId$accessKeySecret = '<AccessKeySecret>'; / / your AccessKeySecret$localFile = '/Users/yours/Video/testVideo.flv'; // Initialize the VOD client and get the upload address and credentials$vodClient = init_vod_client($accessKeyId.$accessKeySecret);
    $createRes = create_upload_video($vodClient); // VideoId, UploadAddress, and UploadAuth are returned after successful execution$videoId = $createRes->VideoId;
    $uploadAddress = json_decode(base64_decode($createRes->UploadAddress), true);
    $uploadAuth = json_decode(base64_decode($createRes->UploadAuth), true); // Initialize the OSS client with UploadAuth and UploadAddress$ossClient = init_oss_client($uploadAuth.$uploadAddress); // Upload a file. Note that synchronous uploading blocks waiting. The time required depends on the file size and uplink bandwidth$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());
}Copy the code

Download the Demo

You can download the complete PHP version of the Demo source code, for more information about the use of OSS- php-SDK upload file.

In order to support more entrepreneurs and lower the entry barrier, VOD has launched a premium experience package on top of the original five packages of voD services.

Only 9.9 yuan can get 10GB of data, 50GB of storage, 100 minutes of transcoding, for individuals, small and micro enterprise portal experience use.

Click to enter the activity page, buy now, complete the payment

Enter theVideo on demand product pageClick Open now to open the VOD service and perform initial configuration. Users can upload videos, set transcodes, manage videos and preview videos on the console.

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

Click for purchase details

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