You need to enable JavaScript to run this app.
导航
媒资上传
最近更新时间:2024.11.05 20:30:50首次发布时间:2021.02.23 10:42:38

本文为您提供了服务端 PHP SDK 的媒资上传模块的接口调用示例。

使用说明

  • 开始前,请阅读媒资上传概述了解媒资类型和支持格式。
  • 异步上传成功是指提交任务成功,并不代表媒资上传任务执行成功。

前提条件

调用接口前,请先完成 SDK 的安装初始化

调用示例

签发临时上传 Token

可在应用服务端通过 AK 和 SK 在本地签出临时上传 Token,不依赖外网。如希望同时生成多个临时上传 Token,您可以循环调用生成方法。临时上传 Token 用于客户端上传,详见客户端上传

说明

支持签入上传策略,详见上传策略

<?php
require('../../vendor/autoload.php');

use Volc\Service\Vod\Vod;
use Volc\Service\Vod\Upload\UploadPolicy;

// Create a VOD instance in the specified region.
// $client = Vod::getInstance('cn-north-1');
$client = Vod::getInstance();

// Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/4408.
// The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed.
// During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account.
// $client->setAccessKey('your ak');
// $client->setSecretKey('your sk');

$expire = 60*60; // 请求的签名有效期

echo "\nSTS2鉴权签名\n";
// 使用 $spaces 来限制限制允许上传的空间
//$spaces = [''];
// 使用 $keyPtn 来限制上传的FileName路径
//     如: "test/*" 表示上传的文件必须包含 "test/" 前缀
//$keyPtn = '';
// 使用 $uploadPolicy来设置上传策略
//$uploadPolicy = new UploadPolicy();
//$uploadPolicy->ContentTypeBlackList = ['content-type1','content-type2'];
//$uploadPolicy->ContentTypeWhiteList = ['content-type1','content-type2'];
//$uploadPolicy->FileSizeUpLimit = 'file size';
//$uploadPolicy->FileSizeBottomLimit = 'file size';
//$response = $client->getUploadVideoAuthWithExpiredTime($expire, $spaces, $keyPtn, $uploadPolicy);
$response = $client->getUploadVideoAuthWithExpiredTime($expire);
echo json_encode($response);

echo "\nSTS2鉴权签名,过期时间默认1小时\n";
$response = $client->getUploadVideoAuth();
echo json_encode($response);

媒资上传

视频点播目前支持以下两个版本的获取上传地址和凭证和确认上传接口:

2022-01-01

接口请求参数和返回参数详见 OpenAPI:获取上传地址和凭证确认上传

注意

版本号为 2022-01-01 时,上传文件必须携带文件后缀。例如,如需上传 MP4 文件,携带 .mp4.MP4

<?php
require('../../vendor/autoload.php');

use Volc\Service\Vod\Models\Request\VodUploadMediaRequest;
use Volc\Service\Vod\Models\Response\VodCommitUploadInfoResponse;
use Volc\Service\Vod\Upload\Functions;
use Volc\Service\Vod\Upload\VodUpload;
use Volc\Service\Vod\Upload\OptionInfo;
use Volc\Service\Vod\Upload\Template;

// Create a VOD instance in the specified region.
// $client = Volc\Service\Vod\Vod::getInstance('cn-north-1');
$client = VodUpload::getInstance();

// Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/4408.
// The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed.
// During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account.
// $client->setAccessKey('your ak');
// $client->setSecretKey('your sk');

$space = 'your space';
$filePath = "file path";

$functions = new Functions();
$functions->addGetMetaFunc();
$functions->addSnapshotTimeFunc(2.1);
$impTemplate = new Template();
$impTemplate->TemplateIds = ["imp template id"];
$impTemplate->TemplateType = "imp";
$transcodeTemplate = new Template();
$transcodeTemplate->TemplateIds = ["transcode template id"];
$transcodeTemplate->TemplateType = "transcode";
$functions->addStartWorkflowTemplateFunc([$impTemplate, $transcodeTemplate]);
$optionInfo = new OptionInfo();
$optionInfo->IsHlsIndexOnly = true;
$functions->addOptionInfoFunc($optionInfo);
$functions = $functions->getFunctionsString();

$request = new VodUploadMediaRequest();
$request->setSpaceName($space);
$request->setFilePath($filePath);
$request->setFunctions($functions);
$request->setCallbackArgs("my callback");
$request->setFileName("hello/vod.mp4");
$request->setStorageClass(1);
$request->setUploadHostPrefer("");

$response = new VodCommitUploadInfoResponse();
try {
    $response = $client->uploadMedia($request);
} catch (Exception $e) {
    echo $e, "\n";
} catch (Throwable $e) {
    echo $e, "\n";
}
if ($response->getResponseMetadata() != null && $response->getResponseMetadata()->getError() != null) {
    echo $response->getResponseMetadata()->getError()->serializeToJsonString(), "\n";
}
echo $response->serializeToJsonString();
echo "\n";

if ($response->getResult() != null) {
    echo $response->getResult()->getData()->getVid(), "\n";
    echo $response->getResult()->getData()->getPosterUri(), "\n";
    echo $response->getResult()->getData()->getSourceInfo()->getWidth(), "\n";
    echo $response->getResult()->getData()->getSourceInfo()->getHeight(), "\n";
}

2020-08-01

接口请求参数和返回参数详见 OpenAPI:获取上传地址和凭证确认上传

<?php
require('../vendor/autoload.php');

use Volc\Service\Vod\Models\Request\VodUploadMediaRequest;
use Volc\Service\Vod\Models\Response\VodCommitUploadInfoResponse;
use Volc\Service\Vod\Upload\Functions;
use Volc\Service\Vod\Upload\VodUpload;

// Create a VOD instance in the specified region.
// $client = Vod::getInstance('cn-north-1');
$client = VodUpload::getInstance();

// Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/4408.
// The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed.
// During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account.
// $client->setAccessKey('your ak');
// $client->setSecretKey('your sk');

$space = 'your space';
$filePath = "file path";

Functions::addGetMetaFunc();
Functions::addSnapshotTimeFunc(2.1);
$functions = Functions::getFunctionsString();

$request = new VodUploadMediaRequest();
$request->setSpaceName($space);
$request->setFilePath($filePath);
$request->setFunctions($functions);
$request->setCallbackArgs("my callback");

$response = new VodCommitUploadInfoResponse();
try {
    $response = $client->uploadMedia($request);
} catch (Exception $e) {
    echo $e, "\n";
} catch (Throwable $e) {
    echo $e, "\n";
}
if ($response->getResponseMetadata()->getError() != null) {
    print_r($response->getResponseMetadata()->getError());
}
echo $response->serializeToJsonString();
echo "\n";

echo $response->getResult()->getData()->getVid(), "\n";
echo $response->getResult()->getData()->getPosterUri(), "\n";
echo $response->getResult()->getData()->getSourceInfo()->getWidth(), "\n";
echo $response->getResult()->getData()->getSourceInfo()->getHeight(), "\n";

素材上传

视频点播目前支持以下两个版本的获取上传地址和凭证和确认上传接口:

2022-01-01

接口请求参数和返回参数详见 OpenAPI:获取上传地址和凭证确认上传

注意

版本号为 2022-01-01 时,上传文件必须携带文件后缀。例如,如需上传 MP4 文件,携带 .mp4.MP4

<?php
require('../../vendor/autoload.php');

use Volc\Service\Vod\Models\Request\VodUploadMaterialRequest;
use Volc\Service\Vod\Models\Response\VodCommitUploadInfoResponse;
use Volc\Service\Vod\Upload\Functions;
use Volc\Service\Vod\Upload\VodUpload;
use Volc\Service\Vod\Upload\OptionInfo;

// Create a VOD instance in the specified region.
// $client = Volc\Service\Vod\Vod::getInstance('cn-north-1');
$client = VodUpload::getInstance();

// Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/4408.
// The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed.
// During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account.
// $client->setAccessKey('your ak');
// $client->setSecretKey('your sk');

$space = 'your space';
$filePath = "file path";

//音视频素材上传
$functions = new Functions();
$functions->addGetMetaFunc();
$functions->addSnapshotTimeFunc(2.1);

$optionInfo = new OptionInfo();
$optionInfo->Title = '素材测试视频';
$optionInfo->Tags = 'test';
$optionInfo->Description = '素材测试,视频文件';
$optionInfo->Category = 'video';
$optionInfo->RecordType = 2;
$optionInfo->Format = 'mp4';
$functions->addOptionInfoFunc($optionInfo);

$functions = $functions->getFunctionsString();

$request = new VodUploadMaterialRequest();
$request->setSpaceName($space);
$request->setFilePath($filePath);
$request->setFunctions($functions);
$request->setFileType("media");

$request->setCallbackArgs("my callback");
$request->setUploadHostPrefer("");

$response = new VodCommitUploadInfoResponse();
try {
    $response = $client->uploadMaterial($request);
} catch (Exception $e) {
    echo $e, "\n";
} catch (Throwable $e) {
    echo $e, "\n";
}
if ($response->getResponseMetadata() != null && $response->getResponseMetadata()->getError() != null) {
    echo $response->getResponseMetadata()->getError(), "\n";
}
echo $response->serializeToJsonString();
echo "\n";

if ($response->getResult() != null) {
    echo $response->getResult()->getData()->getMid(), "\n";
    echo $response->getResult()->getData()->getSourceInfo()->getWidth(), "\n";
    echo $response->getResult()->getData()->getSourceInfo()->getHeight(), "\n";
}

////图片素材上传
//$functions = new Functions();
//$functions->addGetMetaFunc();
//
//$optionInfo = new OptionInfo();
//$optionInfo->Title = '素材测试图片';
//$optionInfo->Tags = 'test';
//$optionInfo->Description = '素材测试,图片文件';
//$optionInfo->Category = 'image';
//$optionInfo->RecordType = 2;
//$optionInfo->Format = 'jpg';
//$functions->addOptionInfoFunc($optionInfo);
//
//$functions = $functions->getFunctionsString();
//
//$request = new VodUploadMaterialRequest();
//$request->setSpaceName($space);
//$request->setFilePath($filePath);
//$request->setFunctions($functions);
//$request->setFileType("image");
//$request->setFileName("hello/vod/image");
//
//$request->setCallbackArgs("my callback");
//
//$response = new VodCommitUploadInfoResponse();
//try {
//    $response = $client->uploadMaterial($request);
//} catch (Exception $e) {
//    echo $e, "\n";
//} catch (Throwable $e) {
//    echo $e, "\n";
//}
//if ($response->getResponseMetadata() != null && $response->getResponseMetadata()->getError() != null) {
//    echo $response->getResponseMetadata()->getError(), "\n";
//}
//echo $response->serializeToJsonString();
//echo "\n";
//
//if ($response->getResult() != null) {
//    echo $response->getResult()->getData()->getMid(), "\n";
//    echo $response->getResult()->getData()->getSourceInfo()->getWidth(), "\n";
//    echo $response->getResult()->getData()->getSourceInfo()->getHeight(), "\n";
//}

////字幕素材上传
//$functions = new Functions();
//$functions->addGetMetaFunc();
//
//$optionInfo = new OptionInfo();
//$optionInfo->Title = '素材测试字幕';
//$optionInfo->Tags = 'test';
//$optionInfo->Description = '素材测试,字幕文件';
//$optionInfo->Category = 'subtitle';
//$optionInfo->RecordType = 2;
//$optionInfo->Format = 'vtt';
//$functions->addOptionInfoFunc($optionInfo);
//
//$functions = $functions->getFunctionsString();
//
//$request = new VodUploadMaterialRequest();
//$request->setSpaceName($space);
//$request->setFilePath($filePath);
//$request->setFunctions($functions);
//$request->setFileType("object");
//$request->setFileName("hello/vod/object");
//
//$request->setCallbackArgs("my callback");
//
//$response = new VodCommitUploadInfoResponse();
//try {
//    $response = $client->uploadMaterial($request);
//} catch (Exception $e) {
//    echo $e, "\n";
//} catch (Throwable $e) {
//    echo $e, "\n";
//}
//if ($response->getResponseMetadata() != null && $response->getResponseMetadata()->getError() != null) {
//    echo $response->getResponseMetadata()->getError(), "\n";
//}
//echo $response->serializeToJsonString();
//echo "\n";
//
//if ($response->getResult() != null) {
//    echo $response->getResult()->getData()->getMid(), "\n";
//    echo $response->getResult()->getData()->getSourceInfo()->getSize(), "\n";
//}

2020-08-01

接口请求参数和返回参数详见 OpenAPI:获取上传地址和凭证确认上传

<?php
require('../../vendor/autoload.php');

use Volc\Service\Vod\Models\Request\VodUploadMaterialRequest;
use Volc\Service\Vod\Models\Response\VodCommitUploadInfoResponse;
use Volc\Service\Vod\Upload\Functions;
use Volc\Service\Vod\Upload\VodUpload;
use Volc\Service\Vod\Upload\OptionInfo;

// Create a VOD instance in the specified region.
// $client = Vod::getInstance('cn-north-1');
$client = VodUpload::getInstance();

// Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/4408.
// The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed.
// During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account.
// $client->setAccessKey('your ak');
// $client->setSecretKey('your sk');

$space = 'your space';
$filePath = "file path";

//音视频素材上传
Functions::addGetMetaFunc();
Functions::addSnapshotTimeFunc(2.1);

$optionInfo = new OptionInfo();
$optionInfo->Title = '素材测试视频';
$optionInfo->Tags = 'test';
$optionInfo->Description = '素材测试,视频文件';
$optionInfo->Category = 'video';
$optionInfo->RecordType = 2;
$optionInfo->Format = 'mp4';
Functions::addOptionInfoFunc($optionInfo);

$functions = Functions::getFunctionsString();

$request = new VodUploadMaterialRequest();
$request->setSpaceName($space);
$request->setFilePath($filePath);
$request->setFunctions($functions);
$request->setFileType("media");

$request->setCallbackArgs("my callback");

$response = new VodCommitUploadInfoResponse();
try {
    $response = $client->uploadMaterial($request);
} catch (Exception $e) {
    echo $e, "\n";
} catch (Throwable $e) {
    echo $e, "\n";
}
if ($response->getResponseMetadata() != null && $response->getResponseMetadata()->getError() != null) {
    echo $response->getResponseMetadata()->getError(), "\n";
}
echo $response->serializeToJsonString();
echo "\n";

if ($response->getResult() != null) {
    echo $response->getResult()->getData()->getMid(), "\n";
    echo $response->getResult()->getData()->getSourceInfo()->getWidth(), "\n";
    echo $response->getResult()->getData()->getSourceInfo()->getHeight(), "\n";
}

//图片素材上传
Functions::addGetMetaFunc();

$optionInfo = new OptionInfo();
$optionInfo->Title = '素材测试图片';
$optionInfo->Tags = 'test';
$optionInfo->Description = '素材测试,图片文件';
$optionInfo->Category = 'image';
$optionInfo->RecordType = 2;
$optionInfo->Format = 'jpg';
Functions::addOptionInfoFunc($optionInfo);

$functions = Functions::getFunctionsString();

$request = new VodUploadMaterialRequest();
$request->setSpaceName($space);
$request->setFilePath($filePath);
$request->setFunctions($functions);
$request->setFileType("image");

$request->setCallbackArgs("my callback");

$response = new VodCommitUploadInfoResponse();
try {
    $response = $client->uploadMaterial($request);
} catch (Exception $e) {
    echo $e, "\n";
} catch (Throwable $e) {
    echo $e, "\n";
}
if ($response->getResponseMetadata() != null && $response->getResponseMetadata()->getError() != null) {
    echo $response->getResponseMetadata()->getError(), "\n";
}
echo $response->serializeToJsonString();
echo "\n";

if ($response->getResult() != null) {
    echo $response->getResult()->getData()->getMid(), "\n";
    echo $response->getResult()->getData()->getSourceInfo()->getWidth(), "\n";
    echo $response->getResult()->getData()->getSourceInfo()->getHeight(), "\n";
}

//字幕素材上传
Functions::addGetMetaFunc();

$optionInfo = new OptionInfo();
$optionInfo->Title = '素材测试字幕';
$optionInfo->Tags = 'test';
$optionInfo->Description = '素材测试,字幕文件';
$optionInfo->Category = 'subtitle';
$optionInfo->RecordType = 2;
$optionInfo->Format = 'vtt';
Functions::addOptionInfoFunc($optionInfo);

$functions = Functions::getFunctionsString();

$request = new VodUploadMaterialRequest();
$request->setSpaceName($space);
$request->setFilePath($filePath);
$request->setFunctions($functions);
$request->setFileType("object");

$request->setCallbackArgs("my callback");

$response = new VodCommitUploadInfoResponse();
try {
    $response = $client->uploadMaterial($request);
} catch (Exception $e) {
    echo $e, "\n";
} catch (Throwable $e) {
    echo $e, "\n";
}
if ($response->getResponseMetadata() != null && $response->getResponseMetadata()->getError() != null) {
    echo $response->getResponseMetadata()->getError(), "\n";
}
echo $response->serializeToJsonString();
echo "\n";

if ($response->getResult() != null) {
    echo $response->getResult()->getData()->getMid(), "\n";
    echo $response->getResult()->getData()->getSourceInfo()->getSize(), "\n";
}

URL 批量拉取上传

接口请求参数和返回参数详见 OpenAPI:URL 批量拉取上传

<?php
require('../../vendor/autoload.php');

// Create a VOD instance in the specified region.
// $client = Vod::getInstance('cn-north-1');
$client = Volc\Service\Vod\Vod::getInstance();

// Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/4408.
// The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed.
// During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account.
// $client->setAccessKey('your ak');
// $client->setSecretKey('your sk');

$request = new Volc\Service\Vod\Models\Request\VodUrlUploadRequest();
$request->setSpaceName("your SpaceName");

$urlSet = new Volc\Service\Vod\Models\Business\VodUrlUploadURLSet();
$urlSet->setSourceUrl("");
$urlSet->setStorageClass(1);
$urlSet->setFileExtension(".mp4");
$urlSet->setCallbackArgs("");
$customHeaders = ['your header key' => "your header value"]; // 自定义 Header,业务希望访问源视频 URL 携带的 Header(例如User-Agent)可以通过该参数传入,非必须字段
$urlSet->setCustomURLHeaders($customHeaders);
$request->setURLSets([$urlSet]);


$response = new Volc\Service\Vod\Models\Response\VodUrlUploadResponse();
try {
    $response = $client->uploadMediaByUrl($request);
} catch (Exception $e) {
    echo $e, "\n";
} catch (Throwable $e) {
    echo $e, "\n";
}
if ($response != null && $response->getResponseMetadata() != null && $response->getResponseMetadata()->getError() != null) {
    echo $response->getResponseMetadata()->getError()->serializeToJsonString(), "\n";
} else {
    echo $response->serializeToJsonString(), "\n";
}

获取上传地址和凭证

接口请求参数和返回参数说明详见 ApplyUploadInfo

<?php
require('../../vendor/autoload.php');

// Create a VOD instance in the specified region.
// $client = Volc\Service\Vod\Vod::getInstance('cn-north-1');
$client = Volc\Service\Vod\Vod::getInstance();

// Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/4408.
// The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed.
// During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account.
// $client->setAccessKey('your ak');
// $client->setSecretKey('your sk');

$request = new Volc\Service\Vod\Models\Request\VodApplyUploadInfoRequest();
$request->setSpaceName("your SpaceName");
$request->setSessionKey("your SessionKey");
$request->setFileSize(0);
$request->setFileType("your FileType");
$request->setFileName("your FileName");
$request->setStorageClass(1);
$request->setFileExtension("your FileExtension");
$request->setClientNetWorkMode("your ClientNetWorkMode");
$request->setClientIDCMode("your ClientIDCMode");
$request->setNeedFallback(false);
$request->setUploadHostPrefer("your UploadHostPrefer");

$response = new Volc\Service\Vod\Models\Response\VodApplyUploadInfoResponse();
try {
    $response = $client->applyUploadInfo($request);
} catch (Exception $e) {
    echo $e, "\n";
} catch (Throwable $e) {
    echo $e, "\n";
}
if ($response != null && $response->getResponseMetadata() != null && $response->getResponseMetadata()->getError() != null) {
    echo $response->getResponseMetadata()->getError()->serializeToJsonString(), "\n";
} else {
    echo $response->serializeToJsonString(), "\n";
}

查询 URL 批量上传任务状态

接口请求参数和返回参数详见 OpenAPI:查询 URL 批量上传任务状态

<?php
require('../vendor/autoload.php');

use Volc\Service\Vod\Models\Business\VodURLSet;
use Volc\Service\Vod\Models\Request\VodQueryUploadTaskInfoRequest;
use Volc\Service\Vod\Models\Response\VodQueryUploadTaskInfoResponse;
use Volc\Service\Vod\Vod;

// Create a VOD instance in the specified region.
// $client = Vod::getInstance('cn-north-1');
$client = Vod::getInstance();

// Configure your Access Key ID (AK) and Secret Access Key (SK) in the environment variables or in the local ~/.volc/config file. For detailed instructions, see https://www.volcengine.com/docs/4/4408.
// The SDK will automatically fetch the AK and SK from the environment variables or the ~/.volc/config file as needed.
// During testing, you may use the following code snippet. However, do not store the AK and SK directly in your project code to prevent potential leakage and safeguard the security of all resources associated with your account.
// $client->setAccessKey('your ak');
// $client->setSecretKey('your sk');

$jobId = 'url jobId';
$jobIds = [$jobId];


$request = new VodQueryUploadTaskInfoRequest();
$request->setJobIds(implode(",", $jobIds));

$response = new VodQueryUploadTaskInfoResponse();
try {
    $response = $client->queryUploadTaskInfo($request);
} catch (Exception $e) {
    echo $e, "\n";
} catch (Throwable $e) {
    echo $e, "\n";
}
if ($response->getResponseMetadata()->getError() != null) {
    print_r($response->getResponseMetadata()->getError());
}
echo $response->serializeToJsonString();
echo "\n";

$mediaInfo = new VodURLSet();
$mediaInfo = $response->getResult()->getData()->getMediaInfoList()[0];
echo $mediaInfo->getRequestId(), "\n";
echo $mediaInfo->getState(), "\n";