VideoDemand.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace app\api\controller;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use AlibabaCloud\SDK\Vod\V20170321\Vod;
  7. use \Exception;
  8. use library\File;
  9. use AlibabaCloud\Tea\Exception\TeaError;
  10. use AlibabaCloud\Tea\Utils\Utils;
  11. use Darabonba\OpenApi\Models\Config;
  12. use AlibabaCloud\SDK\Vod\V20170321\Models\CreateUploadVideoRequest;
  13. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  14. use AlibabaCloud\SDK\Vod\V20170321\Models\GetVideoPlayAuthRequest;
  15. require_once '../vendor/aliyunmail/aliyun-php-sdk-core/Config.php'; // 假定您的源码文件和aliyun-php-sdk处于同一目录
  16. //require '../vendor/autoload.php';
  17. $path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
  18. if (file_exists($path)) require_once $path;
  19. /**
  20. * @title 阿里云视频点播
  21. * @controller VideoDemand
  22. * @group base
  23. */
  24. class VideoDemand extends Base
  25. {
  26. protected $account_name = 'vod@1122764885952286.onaliyun.com'; // 账号
  27. protected $login_secret = 'NKna#5y1mKBp58k)qqwJHhRcrUE!{ECT';// 密码
  28. protected $account_uid = '285664060640337784';// uid
  29. protected $access_key = 'LTAI5tN5SwDsN2km6PgZPScp';// key
  30. protected $access_secret = 'SqRcmQNYG8d7l2vjDLpVvibwT59bSj';//secret
  31. protected $role_arn= 'acs:ram::1122764885952286:role/voderole';//role_arn
  32. protected $role_session_name= 'voderole';//role_session_name
  33. /**
  34. * 使用AK&SK初始化账号Client
  35. * @param string $accessKeyId
  36. * @param string $accessKeySecret
  37. * @return Vod Client
  38. */
  39. public static function createClient($accessKeyId, $accessKeySecret){
  40. $config = new Config([
  41. // 您的 AccessKey ID
  42. "accessKeyId" => $accessKeyId,
  43. // 您的 AccessKey Secret
  44. "accessKeySecret" => $accessKeySecret
  45. ]);
  46. // 访问的域名
  47. $config->endpoint = "vod.cn-shanghai.aliyuncs.com";
  48. return new Vod($config);
  49. }
  50. /**
  51. * 获取本地文件对象
  52. * @return \think\File
  53. */
  54. private function getUploadFile()
  55. {
  56. try {
  57. return $this->request->file('file');
  58. } catch (\Exception $e) {
  59. $this->error(lang($e->getMessage()));
  60. }
  61. }
  62. /**
  63. * @title 获取视频播放凭证[STS方式]
  64. * @desc 获取视频播放凭证[STS方式]
  65. * @author qc
  66. * @url /api/Video_demand/getVideoCredentials
  67. * @method GET
  68. * @tag 视频播放凭证
  69. * @header name:Authorization require:1 desc:Token
  70. * @return name:RequestId type:string default:-- desc:RequestId
  71. * @return name:Arn type:string default:-- desc:RoleArn(Arn)
  72. * @return name:AssumedRoleId type:string default:-- desc:AssumedRoleId
  73. * @return name:SecurityToken type:string default:-- desc:SecurityToken
  74. * @return name:AccessKeyId type:string default:-- desc:AccessKeyId
  75. * @return name:AccessKeySecret type:string default:-- desc:AccessKeySecret
  76. * @return name:Expiration type:string default:-- desc:Expiration
  77. */
  78. public function getVideoCredentials()
  79. {
  80. //构建一个阿里云客户端,用于发起请求。
  81. //设置调用者(RAM用户或RAM角色)的AccessKey ID和AccessKey Secret。
  82. AlibabaCloud::accessKeyClient($this->access_key, $this->access_secret)->regionId('cn-hangzhou')->asDefaultClient();
  83. //设置参数,发起请求。
  84. try {
  85. $result = AlibabaCloud::rpc()
  86. ->product('Sts')
  87. ->scheme('https') // https | http
  88. ->version('2015-04-01')
  89. ->action('AssumeRole')
  90. ->method('POST')
  91. ->host('sts.aliyuncs.com')
  92. ->options([
  93. 'query' => [
  94. 'RegionId' => "cn-hangzhou",
  95. 'RoleArn' => $this->role_arn,
  96. 'RoleSessionName' =>$this->role_session_name,
  97. ],])
  98. ->request();
  99. $ret = $result->toArray();
  100. $ret['Credentials']['region'] = 'cn-shanghai';
  101. $this->success('请求成功',$ret);
  102. } catch (ClientException $e) {
  103. $this->error($e->getErrorMessage() . PHP_EOL);
  104. } catch (ServerException $e) {
  105. $this->error($e->getErrorMessage() . PHP_EOL);
  106. }
  107. }
  108. /**
  109. * @title 获取视频上传凭证
  110. * @desc 获取视频上传凭证
  111. * @author qc
  112. * @url /api/Video_demand/createUploadVideo
  113. * @method GET
  114. * @tag 获取视频上传凭证
  115. * @header name:Authorization require:1 desc:Token
  116. * @return name:requestId type:string default:-- desc:requestId
  117. * @return name:uploadAddress type:string default:-- desc:uploadAddress
  118. * @return name:uploadAuth type:string default:-- desc:uploadAuth
  119. * @return name:videoId type:string default:-- desc:videoId
  120. */
  121. public function createUploadVideo(){
  122. if (!($file = $this->getUploadFile()) || empty($file)) $this->error('文件上传异常,文件可能过大或未上传!');
  123. if (!$file->checkExt(strtolower(sysconf('storage_local_exts')))) $this->error('文件上传类型受限,请在后台配置!');
  124. if ($file->checkExt('php,sh')) $this->error('可执行文件禁止上传到本地服务器!');
  125. $size = $file->getSize();
  126. $file_name = $file->getInfo('name');
  127. $extend = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
  128. $up_param = [
  129. 'fileName'=>$file_name,
  130. 'FileSize'=>$size,
  131. 'title'=>'视频上传',
  132. ];
  133. $client = self::createClient($this->access_key, $this->access_secret);
  134. $createUploadVideoRequest = new CreateUploadVideoRequest($up_param);
  135. $runtime = new RuntimeOptions([]);
  136. $res = $client->createUploadVideoWithOptions($createUploadVideoRequest, $runtime);
  137. $json_res = json_encode($res,true);
  138. $res_arr = json_decode($json_res,true);
  139. $this->success('ok',$res_arr['body']);
  140. }
  141. /**
  142. * @title 获取视频播放凭证[通过视频ID获取]
  143. * @desc 获取视频播放凭证[通过视频ID获取]
  144. * @author qc
  145. * @url /api/Video_demand/getVideoCredentials
  146. * @method GET
  147. * @tag 视频播放凭证
  148. * @header name:Authorization require:1 desc:Token
  149. * @param name:ali_vid type:int require:1 default:0 desc:ali_vid
  150. * @return name:playAuth type:string default:-- desc:playAuth
  151. * @return name:requestId type:string default:-- desc:requestId
  152. */
  153. public function getVideoCredentialsByVideoId()
  154. {
  155. $client = self::createClient($this->access_key, $this->access_secret);
  156. $getVideoPlayAuthRequest = new GetVideoPlayAuthRequest(['videoId'=>input('ali_vid')]);
  157. $runtime = new RuntimeOptions([]);
  158. // 复制代码运行请自行打印 API 的返回值
  159. $res = $client->getVideoPlayAuthWithOptions($getVideoPlayAuthRequest, $runtime);
  160. $json_res = json_encode($res,true);
  161. $res_arr = json_decode($json_res,true);
  162. $this->success('ok',$res_arr['body'] );
  163. }
  164. /**
  165. * @title 获取视频播放地址[通过视频ID获取]
  166. * @desc 获取视频播放地址[通过视频ID获取]
  167. * @author qc
  168. * @url /api/Video_demand/getVideoPath
  169. * @method GET
  170. * @tag 视频播放凭证
  171. * @header name:Authorization require:1 desc:Token
  172. * @param name:ali_vid type:int require:1 default:0 desc:ali_vid
  173. * @return name:playAuth type:string default:-- desc:playAuth
  174. * @return name:requestId type:string default:-- desc:requestId
  175. */
  176. public function getVideoPath()
  177. {
  178. $client = self::createClient($this->access_key, $this->access_secret);
  179. $getVideoPlayAuthRequest = new GetVideoPlayAuthRequest(['videoId'=>input('ali_vid')]);
  180. $runtime = new RuntimeOptions([]);
  181. $res = $client->getVideoPlayAuthWithOptions($getVideoPlayAuthRequest, $runtime);
  182. $json_res = json_encode($res,true);
  183. $res_arr = json_decode($json_res,true);
  184. $this->success('ok',$res_arr['body'] );
  185. }
  186. }