$accessKeyId, // 您的 AccessKey Secret "accessKeySecret" => $accessKeySecret ]); // 访问的域名 $config->endpoint = "vod.cn-shanghai.aliyuncs.com"; return new Vod($config); } /** * 获取本地文件对象 * @return \think\File */ private function getUploadFile() { try { return $this->request->file('file'); } catch (\Exception $e) { $this->error(lang($e->getMessage())); } } /** * @title 获取视频播放凭证[STS方式] * @desc 获取视频播放凭证[STS方式] * @author qc * @url /api/Video_demand/getVideoCredentials * @method GET * @tag 视频播放凭证 * @header name:Authorization require:1 desc:Token * @return name:RequestId type:string default:-- desc:RequestId * @return name:Arn type:string default:-- desc:RoleArn(Arn) * @return name:AssumedRoleId type:string default:-- desc:AssumedRoleId * @return name:SecurityToken type:string default:-- desc:SecurityToken * @return name:AccessKeyId type:string default:-- desc:AccessKeyId * @return name:AccessKeySecret type:string default:-- desc:AccessKeySecret * @return name:Expiration type:string default:-- desc:Expiration */ public function getVideoCredentials() { //构建一个阿里云客户端,用于发起请求。 //设置调用者(RAM用户或RAM角色)的AccessKey ID和AccessKey Secret。 AlibabaCloud::accessKeyClient($this->access_key, $this->access_secret)->regionId('cn-hangzhou')->asDefaultClient(); //设置参数,发起请求。 try { $result = AlibabaCloud::rpc() ->product('Sts') ->scheme('https') // https | http ->version('2015-04-01') ->action('AssumeRole') ->method('POST') ->host('sts.aliyuncs.com') ->options([ 'query' => [ 'RegionId' => "cn-hangzhou", 'RoleArn' => $this->role_arn, 'RoleSessionName' =>$this->role_session_name, ],]) ->request(); $ret = $result->toArray(); $ret['Credentials']['region'] = 'cn-shanghai'; $this->success('请求成功',$ret); } catch (ClientException $e) { $this->error($e->getErrorMessage() . PHP_EOL); } catch (ServerException $e) { $this->error($e->getErrorMessage() . PHP_EOL); } } /** * @title 获取视频播放凭证[通过视频ID获取] * @desc 获取视频播放凭证[通过视频ID获取] * @author qc * @url /api/Video_demand/getVideoCredentials * @method GET * @tag 视频播放凭证 * @header name:Authorization require:1 desc:Token * @param name:ali_vid type:int require:1 default:0 desc:ali_vid * @return name:playAuth type:string default:-- desc:playAuth(时效3000s) * @return name:requestId type:string default:-- desc:requestId */ public function getVideoCredentialsByVideoId() { $client = self::createClient($this->access_key, $this->access_secret); $getVideoPlayAuthRequest = new GetVideoPlayAuthRequest([ 'videoId'=>input('ali_vid'), "authInfoTimeout" => 3000 ]); $runtime = new RuntimeOptions([]); try { $res = $client->getVideoPlayAuthWithOptions($getVideoPlayAuthRequest, $runtime); $json_res = json_encode($res,true); $res_arr = json_decode($json_res,true); }catch (\Exception $error){ if (!($error instanceof TeaError)) { $error = new TeaError([], $error->getMessage(), $error->getCode(), $error); } $this->error($error->message); } // 复制代码运行请自行打印 API 的返回值 $this->success('ok',$res_arr['body'] ); } /** * @title 获取视频上传凭证 * @desc 获取视频上传凭证 * @author qc * @url /api/Video_demand/createUploadVideo * @method POST * @tag 获取视频上传凭证 * @header name:Authorization require:1 desc:Token * @name name:file type:string default:-- desc:视频文件 * @return name:requestId type:string default:-- desc:requestId * @return name:uploadAddress type:string default:-- desc:uploadAddress * @return name:uploadAuth type:string default:-- desc:uploadAuth * @return name:videoId type:string default:-- desc:videoId */ public function createUploadVideo(){ if (!($file = $this->getUploadFile()) || empty($file)) $this->error('文件上传异常,文件可能过大或未上传!'); if (!$file->checkExt(strtolower(sysconf('storage_local_exts')))) $this->error('文件上传类型受限,请在后台配置!'); if ($file->checkExt('php,sh')) $this->error('可执行文件禁止上传到本地服务器!'); $size = $file->getSize(); $file_name = $file->getInfo('name'); $extend = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION); $up_param = [ 'fileName'=>$file_name, 'FileSize'=>$size, 'title'=>'视频上传', ]; try { $client = self::createClient($this->access_key, $this->access_secret); $createUploadVideoRequest = new CreateUploadVideoRequest($up_param); $runtime = new RuntimeOptions([]); $res = $client->createUploadVideoWithOptions($createUploadVideoRequest, $runtime); $json_res = json_encode($res,true); $res_arr = json_decode($json_res,true); }catch (\Exception $error){ if (!($error instanceof TeaError)) { $error = new TeaError([], $error->getMessage(), $error->getCode(), $error); } $this->error($error->message); } $this->success('ok',$res_arr['body']); } /** * @title 刷新视频上传凭证 * @desc 刷新视频上传凭证 * @author qc * @url /api/Video_demand/refreshUploadVideoToken * @method GET * @tag 刷新视频上传凭证 * @header name:Authorization require:1 desc:Token * @header name:Authorization require:1 desc:Token * @param name:ali_vid type:string default:-- desc:videoId * @return name:uploadAddress type:string default:-- desc:uploadAddress * @return name:uploadAuth type:string default:-- desc:uploadAuth * @return name:videoId type:string default:-- desc:videoId */ public function refreshUploadVideoToken() { $client = self::createClient($this->access_key, $this->access_secret); $refreshUploadVideoRequest = new RefreshUploadVideoRequest([ 'videoId'=>input('ali_vid'), ]); $runtime = new RuntimeOptions([]); try { // 复制代码运行请自行打印 API 的返回值 $res = $client->refreshUploadVideoWithOptions($refreshUploadVideoRequest, $runtime); $json_res = json_encode($res,true); $res_arr = json_decode($json_res,true); } catch (\Exception $error) { if (!($error instanceof TeaError)) { $error = new TeaError([], $error->getMessage(), $error->getCode(), $error); } $this->error($error->message); } $this->success('ok',$res_arr['body'] ); } /** * @title 获取视频播放地址[通过视频ID获取] * @desc 获取视频播放地址[通过视频ID获取] * @author qc * @url /api/Video_demand/getVideoPath * @method GET * @tag 视频播放凭证 * @header name:Authorization require:1 desc:Token * @param name:ali_vid type:int require:1 default:0 desc:ali_vid * @return name:playInfoList type:array default:-- desc:playInfoList * @return name:playInfoList.playInfo.playURL type:string default:-- desc:播放地址 */ public function getVideoPath() { $client = self::createClient($this->access_key, $this->access_secret); $getVideoPlayAuthRequest = new GetPlayInfoRequest(['videoId'=>input('ali_vid')]); $runtime = new RuntimeOptions([]); try { $res = $client->getPlayInfoWithOptions($getVideoPlayAuthRequest, $runtime); $json_res = json_encode($res,true); $res_arr = json_decode($json_res,true); }catch (\Exception $error){ if (!($error instanceof TeaError)) { $error = new TeaError([], $error->getMessage(), $error->getCode(), $error); } $this->error($error->message); } $this->success('ok',$res_arr['body'] ); } }