阿里云视频点播上传获取地址和凭证

📅 2026/7/19 16:39:22
阿里云视频点播上传获取地址和凭证
第一步下载sdk;composer require alibabacloud/vod-20170321 3.9.1 composer require alibabacloud/tea-utils第二步在html里上传视频时需要获取地址和上传凭证根据阿里云官方提供的源码示例这里我只贴出一部分function createUploader () { var uploader new AliyunUpload.Vod({ timeout: 60000, partSize: 1048576, parallel: 5, retryCount: 3, retryDuration: 2, region: cn-shanghai, userId: $(#userId).val(), localCheckpoint: true, //此参数是禁用服务端缓存 // 添加文件成功 addFileSuccess: function (uploadInfo) { console.log(addFileSuccess); $(#authUpload).attr(disabled, false); $(#resumeUpload).attr(disabled, false); $(#status).text(添加文件成功, 等待上传...); console.log(addFileSuccess: uploadInfo.file.name); }, // 开始上传 onUploadstarted: function (uploadInfo) { // 如果是 UploadAuth 上传方式, 需要调用 uploader.setUploadAuthAndAddress 方法 // 如果是 UploadAuth 上传方式, 需要根据 uploadInfo.videoId是否有值调用点播的不同接口获取uploadauth和uploadAddress // 如果 uploadInfo.videoId 有值调用刷新视频上传凭证接口否则调用创建视频上传凭证接口 // 注意: 这里是测试 demo 所以直接调用了获取 UploadAuth 的测试接口, 用户在使用时需要判断 uploadInfo.videoId 存在与否从而调用 openApi // 如果 uploadInfo.videoId 存在, 调用 刷新视频上传凭证接口(https://help.aliyun.com/document_detail/55408.html) // 如果 uploadInfo.videoId 不存在,调用 获取视频上传地址和凭证接口(https://help.aliyun.com/document_detail/55407.html) // 统一的获取凭证失败处理 var handleAuthRequestFail function (jqXHR, textStatus, errorThrown, scene) { console.error(scene 失败:, textStatus, errorThrown); var errMsg (jqXHR jqXHR.status) ? (HTTP jqXHR.status) : (textStatus || 网络错误); $(#status).text(scene 失败请稍后重试 errMsg ); if (uploader) { uploader.stopUpload(); } $(#authUpload).attr(disabled, false); $(#pauseUpload).attr(disabled, true); $(#resumeUpload).attr(disabled, false); } if (!uploadInfo.videoId) { var createUrl https://demo.xxx.cn/api/video/createdUploadVideo/; $.get(createUrl, function (data) { var uploadAuth data.uploadAuth; var uploadAddress data.uploadAddress; var videoId data.videoId; uploader.setUploadAuthAndAddress(uploadInfo, uploadAuth, uploadAddress,videoId); }, json).fail(function (jqXHR, textStatus, errorThrown) { handleAuthRequestFail(jqXHR, textStatus, errorThrown, 获取视频上传凭证) }); $(#status).text(文件开始上传...); console.log(onUploadStarted: uploadInfo.file.name , endpoint: uploadInfo.endpoint , bucket: uploadInfo.bucket , object: uploadInfo.object); } else { // 如果videoId有值根据videoId刷新上传凭证 // https://help.aliyun.com/document_detail/55408.html?spma2c4g.11186623.6.630.BoYYcY var refreshUrl https://demo.xxx.cn/api/video/refreshUploadVideo/?videoId uploadInfo.videoId; $.get(refreshUrl, function (data) { var uploadAuth data.uploadAuth; var uploadAddress data.uploadAddress; var videoId data.videoId; uploader.setUploadAuthAndAddress(uploadInfo, uploadAuth, uploadAddress,videoId); }, json).fail(function (jqXHR, textStatus, errorThrown) { handleAuthRequestFail(jqXHR, textStatus, errorThrown, 刷新视频上传凭证); }) } }, // 文件上传成功 onUploadSucceed: function (uploadInfo) { console.log(onUploadSucceed: uploadInfo.file.name , endpoint: uploadInfo.endpoint , bucket: uploadInfo.bucket , object: uploadInfo.object); $(#status).text(文件上传成功!); }, // 文件上传失败 onUploadFailed: function (uploadInfo, code, message) { console.log(onUploadFailed: file: uploadInfo.file.name ,code: code , message: message); $(#status).text(文件上传失败!); }, // 取消文件上传 onUploadCanceled: function (uploadInfo, code, message) { console.log(Canceled file: uploadInfo.file.name , code: code , message: message); $(#status).text(文件上传已暂停!); }, // 文件上传进度单位字节, 可以在这个函数中拿到上传进度并显示在页面上 onUploadProgress: function (uploadInfo, totalSize, progress) { console.log(onUploadProgress:file: uploadInfo.file.name , fileSize: totalSize , percent: Math.ceil(progress * 100) %); var progressPercent Math.ceil(progress * 100); $(#auth-progress).text(progressPercent); $(#status).text(文件上传中...); }, // 上传凭证超时 onUploadTokenExpired: function (uploadInfo) { // 上传大文件超时, 如果是上传方式一即根据 UploadAuth 上传时 // 需要根据 uploadInfo.videoId 调用刷新视频上传凭证接口(https://help.aliyun.com/document_detail/55408.html)重新获取 UploadAuth // 然后调用 resumeUploadWithAuth 方法, 这里是测试接口, 所以我直接获取了 UploadAuth $(#status).text(文件上传超时!); let refreshUrl https://demo.xxx.cn/api/video/refreshUploadVideo/?videoId uploadInfo.videoId; $.get(refreshUrl, function (data) { var uploadAuth data.uploadAuth; // 源文档是UploadAuth,是错误的; uploader.resumeUploadWithAuth(uploadAuth); console.log(upload expired and resume upload with uploadauth uploadAuth); }, json).fail(function (jqXHR, textStatus, errorThrown) { console.error(凭证过期后刷新 UploadAuth 失败:, textStatus, errorThrown); var errMsg (jqXHR jqXHR.status) ? (HTTP jqXHR.status) : (textStatus || 网络错误); $(#status).text(上传凭证已过期刷新凭证失败请点击恢复上传重试 errMsg ); // 刷新凭证失败停止当前上传并恢复按钮状态等待用户手动恢复; if (uploader) { uploader.stopUpload(); } $(#pauseUpload).attr(disabled, true); $(#resumeUpload).attr(disabled, false); }) }, // 全部文件上传结束 onUploadEnd: function (uploadInfo) { $(#status).text(文件上传完毕!); console.log(onUploadEnd: uploaded all the files); } }) return uploader; }第三步在thinkphp8框架下use AlibabaCloud\SDK\Vod\V20170321\Vod; use AlibabaCloud\Credentials\Credential; use \Exception; use AlibabaCloud\Tea\Exception\TeaError; use AlibabaCloud\Tea\Utils\Utils; use Darabonba\OpenApi\Models\Config; use AlibabaCloud\SDK\Vod\V20170321\Models\CreateUploadVideoRequest; use AlibabaCloud\SDK\Vod\V20170321\Models\RefreshUploadVideoRequest; use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions; class Demo { private function createClient(){ $credential new Credential(); $config new Config([ accessKeyId 你的keyID, accessKeySecret 你的keySecret ]); $config-endpoint vod.cn-shanghai.aliyuncs.com; return new Vod($config); } /** * 获取音视频上传地址和凭证; */ public function createdUploadVideo(){ $client $this-createClient(); $createUploadVideoRequest new CreateUploadVideoRequest([ fileName demo.mp4, title demo ]); $runtime new RuntimeOptions([]); try { $result $client-createUploadVideoWithOptions($createUploadVideoRequest, $runtime); $res $result-body; return json($res); } catch (Exception $error) { if (!($error instanceof TeaError)) { $error new TeaError([], $error-getMessage(), $error-getCode(), $error); } $errMsg Utils::assertAsString($error-message); return $errMsg; } } /** *用于视频文件上传超时后重新获取视频上传凭证 */ public function refreshUploadVideo() { $client self::createClient(); $refreshUploadVideoRequest new RefreshUploadVideoRequest([ videoId 80aca47ad03271f0a7415420848d0102 ]); $runtime new RuntimeOptions([]); try { $result $client-refreshUploadVideoWithOptions($refreshUploadVideoRequest, $runtime); $res $result-body; return json($res); } catch (Exception $error) { if (!($error instanceof TeaError)) { $error new TeaError([], $error-getMessage(), $error-getCode(), $error); } $errMsg Utils::assertAsString($error-message); return $errMsg; } } }