All Products
Search
Document Center

ApsaraVideo VOD:Media upload

Last Updated:May 19, 2022

ApsaraVideo VOD servers provide a variety of API operations for media upload. You can upload media files to ApsaraVideo VOD by calling one of these API operations. This topic describes the scenarios in which you can use the server operation SDK for PHP to call these operations and provides sample code.

Scenarios

Notice

This topic provides only the sample code of calling API operations, such as the operation for obtaining a URL and a credential for uploading a media file. For more information about the scenarios in which the operations can be used, see the following table.

Operation

Scenario

CreateUploadVideo

  • If you upload media files by using Object Storage Service (OSS) SDKs, you need to use the server operation SDK and call an operation to obtain a URL and a credential for uploading a media file. The obtained URL and credential are Base64-encoded and must be decoded before they are used to initialize an OSS client. For more information, see Upload media files by using OSS SDKs.

  • If you upload media file by using ApsaraVideo VOD clients, you need to use the server operation SDK and call an operation to obtain a URL and a credential for uploading a media file. You can deliver the URL and credential to clients without the need to decode the URL or credential. For more information, see Upload from clients.

RefreshUploadVideo

CreateUploadImage

CreateUploadAttachedMedia

UploadMediaByURL

  • You can call the UploadMediaByURL operation to upload media files by using the URLs of source files. This way, you do not need to download media files to your servers or terminals before you use the server operation SDK to upload the media files to ApsaraVideo VOD.

  • You can call the GetURLUploadInfos operation to query the information about URL-based upload jobs.

GetURLUploadInfos

RegisterMedia

  • After you upload media files to a user-created OSS bucket, you can call the RegisterMedia operation to register the media files and obtain the media IDs of the files. After you register the media files, you can submit transcoding or snapshot jobs for the media files. For more information, see Add a user-created OSS bucket.

Prerequisites

  • The server operation SDK for PHP is installed. For more information, see Installation.

  • The server operation SDK for PHP is initialized. For more information, see Initialization.

    Note

    In the following examples, an AccessKey pair is used to initialize the server operation SDK.

Sample code

Obtain a URL and a credential for uploading an audio file or a video

You can call the CreateUploadVideo operation to obtain a URL and a credential for uploading an audio file or a video.

For more information about the request and response parameters of this operation, see CreateUploadVideo. The following sample code provides an example:

<?php
require_once './aliyun-php-sdk/aliyun-php-sdk-core/Config.php';   // In this example, the source code file is stored in the same folder as aliyun-php-sdk.
use vod\Request\V20170321 as vod;

function initVodClient($accessKeyId, $accessKeySecret) {
    $regionId = 'cn-shanghai';  // Specify the region from which you want to access ApsaraVideo VOD.
    $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
    return new DefaultAcsClient($profile);
}

/**
 * Obtain a URL and a credential for uploading an audio file or a video.
 * @param client The client that sends a request.
 * @return CreateUploadVideoResponse The fields that must be contained in the response.
 */
function createUploadVideo($client) {
    $request = new vod\CreateUploadVideoRequest();
    $request->setTitle("Sample Title");
    $request->setFileName("videoFile.mov");
    $request->setDescription("Video Description");
    $request->setCoverURL("http://192.168.0.0/16/tps/TB1qnJ1PVXXXXXCXXXXXXXXXXXX-700-700.png");
    $request->setTags("tag1,tag2");

    $request->setAcceptFormat('JSON');
    return $client->getAcsResponse($request);
}

try {
    $client = initVodClient('<AccessKeyId>', '<AccessKeySecret>');
    $uploadInfo = createUploadVideo($client);
    var_dump($uploadInfo);
} catch (Exception $e) {
    print $e->getMessage()."\n";
}

Refresh the credential for uploading an audio file or a video

You can call the RefreshUploadVideo operation to refresh the credential for uploading an audio file or a video.

For more information about the request and response parameters of this operation, see RefreshUploadVideo. The following sample code provides an example:

<?php
require_once './aliyun-php-sdk/aliyun-php-sdk-core/Config.php';   // In this example, the source code file is stored in the same folder as aliyun-php-sdk.
use vod\Request\V20170321 as vod;

function initVodClient($accessKeyId, $accessKeySecret) {
    $regionId = 'cn-shanghai';  // Specify the region from which you want to access ApsaraVideo VOD.
    $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
    return new DefaultAcsClient($profile);
}

/**
 * Refresh the credential for uploading an audio file or a video.
 * @param client The client that sends a request.
 * @return RefreshUploadVideoResponse The fields that must be contained in the response.
 */
function refreshUploadVideo($client, $videoId) {
    $request = new vod\RefreshUploadVideoRequest();
    $request->setVideoId($videoId);
    $request->setAcceptFormat('JSON');
    return $client->getAcsResponse($request);
}

try {
    $client = initVodClient('<AccessKeyId>', '<AccessKeySecret>');
    $refreshInfo = refreshUploadVideo($client, '<MediaId>');
    var_dump($refreshInfo);
} catch (Exception $e) {
    print $e->getMessage()."\n";
}

Obtain a URL and a credential for uploading an image

You can call the CreateUploadImage operation to obtain a URL and a credential for uploading an image.

For more information about the request and response parameters of this operation, see CreateUploadImage. The following sample code provides an example:

<?php
require_once './aliyun-php-sdk/aliyun-php-sdk-core/Config.php';   // In this example, the source code file is stored in the same folder as aliyun-php-sdk.
use vod\Request\V20170321 as vod;

function initVodClient($accessKeyId, $accessKeySecret) {
    $regionId = 'cn-shanghai';  // Specify the region from which you want to access ApsaraVideo VOD.
    $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
    return new DefaultAcsClient($profile);
}

/**
 * Obtain a URL and a credential for uploading an image.
 * @param client The client that sends a request.
 * @return CreateUploadImageResponse The fields that must be contained in the response.
 */
function createUploadImage($client, $imageType, $imageExt) {
    $request = new vod\CreateUploadImageRequest();
    $request->setImageType($imageType);
    $request->setImageExt($imageExt);
    $request->setAcceptFormat('JSON');
    return $client->getAcsResponse($request);
}

try {
    $client = initVodClient('<AccessKeyId>', '<AccessKeySecret>');

    $imageInfo = createUploadImage($client, 'cover', 'jpg');
    var_dump($imageInfo);
} catch (Exception $e) {
    print $e->getMessage()."\n";
}

Obtain a URL and a credential for uploading an auxiliary media asset

You can call the CreateUploadAttachedMedia operation to obtain a URL and a credential for uploading an auxiliary media asset.

For more information about the request and response parameters of this operation, see CreateUploadAttachedMedia. The following sample code provides an example:

<?php
require_once './aliyun-php-sdk/aliyun-php-sdk-core/Config.php';   // In this example, the source code file is stored in the same folder as aliyun-php-sdk.
use vod\Request\V20170321 as vod;

function initVodClient($accessKeyId, $accessKeySecret) {
    $regionId = 'cn-shanghai';  // Specify the region from which you want to access ApsaraVideo VOD.
    $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
    return new DefaultAcsClient($profile);
}

/**
 * Obtain a URL and a credential for uploading an auxiliary media asset, such as a watermark or subtitle file.
 * @param client The client that sends a request.
 * @return CreateUploadAttachedMediaResponse The fields that must be contained in the response.
 */
function createUploadAttachedMedia($client) {
    $request = new vod\CreateUploadAttachedMediaRequest();
    $request->setBusinessType("watermark");
    $request->setMediaExt("gif");
    $request->setTitle("this is a sample");

    return $client->getAcsResponse($request);
}


try {
    $client = initVodClient('<AccessKeyId>', '<AccessKeySecret>');

    $result = createUploadAttachedMedia($client);
    var_dump($result);
} catch (Exception $e) {
    print $e->getMessage()."\n";
}

Upload multiple media files by using the URLs of source files at a time

You can call the UploadMediaByURL operation to upload multiple media files by using the URLs of source files at a time.

For more information about the request and response parameters of this operation, see UploadMediaByURL. The following sample code provides an example:

<?php
require_once './aliyun-php-sdk/aliyun-php-sdk-core/Config.php';   // In this example, the source code file is stored in the same folder as aliyun-php-sdk.
use vod\Request\V20170321 as vod;

function initVodClient($accessKeyId, $accessKeySecret) {
    $regionId = 'cn-shanghai';  // Specify the region from which you want to access ApsaraVideo VOD.
    $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
    return new DefaultAcsClient($profile);
}

/**
 * Upload multiple media files by using the URLs of source files at a time.
 * @param client The client that sends a request.
 * @return UploadMediaByURLResponse The fields that must be contained in the response.
 */
function uploadMediaByURL($client) {
    $request = new vod\UploadMediaByURLRequest();
    $url = "http://192.168.0.0/16/***.mp4";
    $request->setUploadURLs($url);

    $uploadMetadataList = array();
    $uploadMetadata = array();
    $uploadMetadata["SourceUrl"] = $url;
    $uploadMetadata["Title"] = "upload by url sample";
    $uploadMetadataList[] = $uploadMetadata;
    $request->setUploadMetadatas(json_encode($uploadMetadataList));

    return $client->getAcsResponse($request);
}

try {
    $client = initVodClient('<AccessKeyId>', '<AccessKeySecret>');

    $result = uploadMediaByURL($client);
    var_dump($result);
} catch (Exception $e) {
    print $e->getMessage()."\n";
}

Register media assets

You can call the RegisterMedia operation to register media assets.

For more information about the request and response parameters of this operation, see RegisterMedia. The following sample code provides an example:

<?php
require_once './aliyun-php-sdk/aliyun-php-sdk-core/Config.php';   // In this example, the source code file is stored in the same folder as aliyun-php-sdk.
use vod\Request\V20170321 as vod;

function initVodClient($accessKeyId, $accessKeySecret) {
    $regionId = 'cn-shanghai';  // Specify the region from which you want to access ApsaraVideo VOD.
    $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
    return new DefaultAcsClient($profile);
}

/**
 * Register media assets.
 * @param client The client that sends a request.
 * @return RegisterMediaResponse The fields that must be contained in the response.
 */
function registerMedia($client) {
    $request = new vod\RegisterMediaRequest();

    $metaDataArray= array();
    $metaData= array();
    $metaData["Title"] = "registerMedia by url sample";
    $metaData["FileURL"] = "https://192.168.0.0/16/vod_sample.mp4";
    $metaDataArray[] = $metaData;
    $request->setRegisterMetadatas(json_encode($metaDataArray));

    return $client->getAcsResponse($request);
}


try {
    $client = initVodClient('<AccessKeyId>', '<AccessKeySecret>');

    $result = registerMedia($client);
    var_dump($result);
} catch (Exception $e) {
    print $e->getMessage()."\n";
}

Query the information about URL-based upload jobs

You can call the GetURLUploadInfos operation to query the information about URL-based upload jobs.

For more information about the request and response parameters of this operation, see GetURLUploadInfos. The following sample code provides an example:

<?php
require_once './aliyun-php-sdk/aliyun-php-sdk-core/Config.php';   // In this example, the source code file is stored in the same folder as aliyun-php-sdk.
use vod\Request\V20170321 as vod;

function initVodClient($accessKeyId, $accessKeySecret) {
    $regionId = 'cn-shanghai';  // Specify the region from which you want to access ApsaraVideo VOD.
    $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
    return new DefaultAcsClient($profile);
}

/**
 * Query the information about URL-based upload jobs.
 * @param client The client that sends a request.
 * @return GetURLUploadInfosResponse The fields that must be contained in the response.
 */
function getURLUploadInfos($client) {
    $request = new vod\GetURLUploadInfosRequest();

    // The URL list.
    $urls = array();
    $urls[] = "http://192.168.0.0/16/sample1.mp4";
    $urls[] = "http://192.168.0.0/16/sample2.mp4";

    // Encode URLs.
    $uploadURLs = array();
    foreach ($urls as $url) {
        $uploadURLs[] = urlencode($url);
    }

    // The URLs of source files. Separate multiple URLs with commas (,).
    $request->setUploadURLs(implode(",", $uploadURLs));

    // The IDs of upload jobs.
    //$request->setJobIds("jobId1****,jobId2****")

    return $client->getAcsResponse($request);
}


try {
    $client = initVodClient('<AccessKeyId>', '<AccessKeySecret>');

    $result = getURLUploadInfos($client);
    var_dump($result);
} catch (Exception $e) {
    print $e->getMessage()."\n";
}