这篇文章主要介绍了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;
}
}
到此这篇关于Laravel框架实现点播上传阿里云功能的文章就介绍到这了,更多相关Laravel点播上传阿里云内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
沃梦达教程
本文标题为:Laravel框架实现点播上传阿里云功能
基础教程推荐
猜你喜欢
- PHP命名空间简单用法示例 2022-12-01
- PHP获取MySQL执行sql语句的查询时间方法 2022-11-09
- laravel ORM关联关系中的 with和whereHas用法 2023-03-02
- PHP实现Redis单据锁以及防止并发重复写入 2022-10-12
- PHP中的错误及其处理机制 2023-06-04
- laravel 解决多库下的DB::transaction()事务失效问题 2023-03-08
- 使用PHP开发留言板功能 2023-03-13
- 在Laravel中实现使用AJAX动态刷新部分页面 2023-03-02
- thinkphp3.2.3框架动态切换多数据库的方法分析 2023-03-19
- php array分组,PHP中array数组的分组排序 2022-08-01