这篇文章次要介绍了 Laravel 框架实现点播上传阿里云性能,本文给大家分享一段残缺的实例代码,代码简略易懂, 须要的敌人能够参考下
1、确认框架中装置了第三方 alibabacoud 控件
实现代码如下
上传过程中遇到工作问题,能够进行留言
<?php
namespace App\Api\xxxx\Controllers;
use DB;
use Illuminate\Http\Request;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\SDK\Vod\V20170321\Vod;
use AlibabaCloud\SDK\Vod\V20170321\Models\DeleteImageRequest;
use AlibabaCloud\SDK\Vod\V20170321\Models\GetPlayInfoRequest;
use AlibabaCloud\SDK\Vod\V20170321\Models\DeleteVideoRequest;
use AlibabaCloud\SDK\Vod\V20170321\Models\GetImageInfoRequest;
use AlibabaCloud\SDK\Vod\V20170321\Models\CreateUploadVideoRequest;
use AlibabaCloud\SDK\Vod\V20170321\Models\CreateUploadImageRequest;
use Darabonba\OpenApi\Models\Config;
use OSS\OssClient;
use AlibabaCloud\Tea\Tea;
class CommonController extends XXController
{
// 视频点播上传
public function uploadVideo($fileName,$title)
{$title = explode('.',$param['title']);
$fileName = $param['fileName'];
if($title[1] == 'mp4') {$cateId = 3479;// 分类 ID,阿里云点播能够设置}
$client = self::createClient("xxx", "xxx");// 依据申请的值放入
$createUploadVideoRequest = new CreateUploadVideoRequest([
'Action' => 'CreateUploadVideo',// 申请的上传函数
"fileName" => $fileName,// 文件名称
"cateId" => $cateId,// 类型名称
'title' => $param['title'],// 题目
]);
$createRes = $client->createUploadVideo($createUploadVideoRequest);
$videoId = $createRes->body->videoId; // 获取视频播放地址须要的参数
$formats = $title[1];
//base64 解码
$uploadAddress = json_decode(base64_decode($createRes->body->uploadAddress), true);
$uploadAuth = json_decode(base64_decode($createRes->body->uploadAuth), true);
// 应用 UploadAuth 和 UploadAddress 初始化 OSS 客户端
$ossClient = $this->init_oss_client($uploadAuth, $uploadAddress);
$localFile = $fileName; // 须要上传到 VOD 的本地视频文件的残缺门路 改这个有成果
// 上传文件,留神是同步上传会阻塞期待,耗时与文件大小和网络上行带宽无关
$result = $this->upload_local_file($ossClient, $uploadAddress, $localFile);
//$result = multipart_upload_file($ossClient, $uploadAddress, $localFile);
$arr = $this->playinfo($videoId,$formats);
$data = Utils::toArray(Tea::merge($arr->body));
$brr = array();
$brr['VideoId'] = $data['VideoBase']['VideoId'];// 视频 id
$brr['PlayURL'] = $data['PlayInfoList']['PlayInfo'][0]['PlayURL'];// 返回的播放地址
return $brr;
}
public 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;
}
}