当前位置:首页 > CMS教程 > 其它CMS > 列表

Laravel框架实现点播上传阿里云功能

发布:smiling 来源: PHP粉丝网  添加日期:2022-05-15 11:59:43 浏览: 评论:0 

这篇文章主要介绍了Laravel框架实现点播上传阿里云功能,本文给大家分享一段完整的实例代码,代码简单易懂,需要的朋友可以参考下。

1、确认框架中安装了第三方alibabacoud控件

实现代码如下

上传过程中遇到任务问题,可以进行留言

  1. <?php 
  2.    
  3. namespace App\Api\xxxx\Controllers; 
  4. use DB; 
  5. use Illuminate\Http\Request; 
  6. use AlibabaCloud\Tea\Utils\Utils; 
  7. use AlibabaCloud\SDK\Vod\V20170321\Vod; 
  8. use AlibabaCloud\SDK\Vod\V20170321\Models\DeleteImageRequest; 
  9. use AlibabaCloud\SDK\Vod\V20170321\Models\GetPlayInfoRequest; 
  10. use AlibabaCloud\SDK\Vod\V20170321\Models\DeleteVideoRequest; 
  11. use AlibabaCloud\SDK\Vod\V20170321\Models\GetImageInfoRequest; 
  12. use AlibabaCloud\SDK\Vod\V20170321\Models\CreateUploadVideoRequest; 
  13. use AlibabaCloud\SDK\Vod\V20170321\Models\CreateUploadImageRequest; 
  14. use Darabonba\OpenApi\Models\Config; 
  15. use OSS\OssClient; 
  16. use AlibabaCloud\Tea\Tea; 
  17. class CommonController extends XXController 
  18.     //视频点播上传 
  19.     public function uploadVideo($fileName,$title
  20.     { 
  21.         $title = explode('.',$param['title']); 
  22.         $fileName = $param['fileName']; 
  23.         if($title[1] == 'mp4') { 
  24.             $cateId = 3479;//分类ID,阿里云点播可以设置 
  25.          } 
  26.         $client = self::createClient("xxx""xxx");//根据申请的值放入 
  27.         $createUploadVideoRequest = new CreateUploadVideoRequest([ 
  28.             'Action' => 'CreateUploadVideo',//请求的上传函数 
  29.             "fileName" => $fileName,//文件名称 
  30.             "cateId" => $cateId,//类型名称 
  31.             'title' => $param['title'],//标题 
  32.         ]); 
  33.         $createRes = $client->createUploadVideo($createUploadVideoRequest); 
  34.         $videoId = $createRes->body->videoId; //获取视频播放地址需要的参数 
  35.         $formats = $title[1]; 
  36.         //base64解码 
  37.         $uploadAddress = json_decode(base64_decode($createRes->body->uploadAddress), true); 
  38.         $uploadAuth = json_decode(base64_decode($createRes->body->uploadAuth), true); 
  39.    
  40.         // 使用UploadAuth和UploadAddress初始化OSS客户端 
  41.         $ossClient = $this->init_oss_client($uploadAuth$uploadAddress); 
  42.         $localFile = $fileName;   // 需要上传到VOD的本地视频文件的完整路径  改这个有效果 
  43.          
  44.    
  45.         // 上传文件,注意是同步上传会阻塞等待,耗时与文件大小和网络上行带宽有关 
  46.         $result = $this->upload_local_file($ossClient$uploadAddress$localFile); 
  47.         //$result = multipart_upload_file($ossClient, $uploadAddress, $localFile); 
  48.         $arr = $this->playinfo($videoId,$formats); 
  49.         $data = Utils::toArray(Tea::merge($arr->body)); 
  50.         $brr = array(); 
  51.         $brr['VideoId'] = $data['VideoBase']['VideoId'];//视频id 
  52.         $brr['PlayURL'] = $data['PlayInfoList']['PlayInfo'][0]['PlayURL'];//返回的播放地址 
  53.         return $brr
  54.     } 
  55.    
  56.     public function init_oss_client($uploadAuth$uploadAddress
  57.     { 
  58.         $ossClient = new OssClient($uploadAuth['AccessKeyId'], $uploadAuth['AccessKeySecret'], $uploadAddress['Endpoint'], 
  59.             false, $uploadAuth['SecurityToken']); 
  60.         $ossClient->setTimeout(86400*7);    // 设置请求超时时间,单位秒,默认是5184000秒, 建议不要设置太小,如果上传文件很大,消耗的时间会比较长 
  61.         $ossClient->setConnectTimeout(10);  // 设置连接超时时间,单位秒,默认是10秒 
  62.         return $ossClient
  63.     } 
  64. }

Tags: Laravel点播上传阿里云

分享到: