Oss.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace Upload\Lib\View;
  3. use Dever;
  4. Dever::apply('sdk/oss', 'upload');
  5. Dever::apply('vendor/autoload', 'alibaba');
  6. use OSS\OssClient;
  7. use OSS\Core\OssException;
  8. use AlibabaCloud\Client\AlibabaCloud;
  9. use AlibabaCloud\Client\Exception\ClientException;
  10. use AlibabaCloud\Client\Exception\ServerException;
  11. class Oss
  12. {
  13. private $client;
  14. public function token($config, $upload, $file = '')
  15. {
  16. $region = 'cn-' . $config['region_id'];
  17. if (!$config['role_arn']) {
  18. return array('oss', '||' . $config['appkey'] . '||' . $config['appsecret'], $region, $upload['bucket']);
  19. }
  20. $cur = time();
  21. if (!$config['token'] || ($config['token_endtime'] && $cur > $config['token_endtime'])) {
  22. AlibabaCloud::accessKeyClient($config['appkey'], $config['appsecret'])->regionId($region)->asDefaultClient();
  23. //设置参数,发起请求。
  24. try {
  25. $result = AlibabaCloud::rpc()
  26. ->product('Sts')
  27. ->scheme('https') // https | http
  28. ->version('2015-04-01')
  29. ->action('AssumeRole')
  30. ->method('POST')
  31. ->host('sts.aliyuncs.com')
  32. ->options([
  33. 'query' => [
  34. 'RegionId' => $region,
  35. 'RoleArn' => "acs:ram::1118875946432366:role/api",
  36. 'RoleArn' => $config['role_arn'],
  37. 'RoleSessionName' => "upload",
  38. ],
  39. ])
  40. ->request();
  41. $data = $result->toArray();
  42. if (isset($data['Credentials']['SecurityToken'])) {
  43. $token = $data['Credentials']['SecurityToken'];
  44. $endtime = $data['Credentials']['Expiration'];
  45. $appkey = $data['Credentials']['AccessKeyId'];
  46. $appsecret = $data['Credentials']['AccessKeySecret'];
  47. $token = $token . '||' . $appkey . '||' . $appsecret;
  48. $up['token'] = $token;
  49. $up['token_endtime'] = Dever::maketime($endtime) - 60;
  50. $up['where_id'] = $config['id'];
  51. Dever::db('upload/yun')->update($up);
  52. } else {
  53. echo 'oss token获取失败,请检查配置';die;
  54. }
  55. } catch (ClientException $e) {
  56. echo $e->getErrorMessage() . PHP_EOL;die;
  57. } catch (ServerException $e) {
  58. echo $e->getErrorMessage() . PHP_EOL;die;
  59. }
  60. } else {
  61. $token = $config['token'];
  62. }
  63. return array('oss', $token, $region, $upload['bucket']);
  64. }
  65. # 连接
  66. public function connect($config, $upload, $file = '')
  67. {
  68. $accessKey = $config['appkey'];
  69. $secretKey = $config['appsecret'];
  70. $endpoint = "http://oss-cn-".$config['region_id'].".aliyuncs.com";
  71. list($type, $token, $domain, $bucket) = $this->token($config, $upload, $file);
  72. $data = explode('||', $token);
  73. if ($data[0]) {
  74. $token = $data[0];
  75. $accessKey = $data[1];
  76. $secretKey = $data[2];
  77. } else {
  78. $token = false;
  79. }
  80. $this->client = new OssClient($accessKey, $secretKey, $endpoint, false, $token);
  81. return $this;
  82. }
  83. # 上传文件
  84. public function upload($bucket, $file, $source_file, $options = array(), $base64 = false)
  85. {
  86. if (!$this->client) {
  87. return array();
  88. }
  89. $method = 'uploadFile';
  90. if ($base64) {
  91. $method = 'putObject';
  92. }
  93. $ret = $this->client->$method($bucket, $file, $source_file, $options);
  94. return $ret;
  95. }
  96. # 删除文件
  97. public function delete($bucket, $file)
  98. {
  99. $this->client = new \Qiniu\Storage\BucketManager($this->auth);
  100. $result = $this->client->delete($bucket, $file);
  101. return $result;
  102. }
  103. # 下载文件
  104. public function download($bucket, $file, $local = false)
  105. {
  106. if (!$this->client) {
  107. return false;
  108. }
  109. if ($local) {
  110. $options = array(
  111. OssClient::OSS_FILE_DOWNLOAD => $local
  112. );
  113. } else {
  114. $options = array();
  115. }
  116. $content = $this->client->getObject($bucket, $file, $options);
  117. return $content;
  118. }
  119. public function callback()
  120. {
  121. $body = file_get_contents('php://input');
  122. Dever::log($body, 'oss_callback');
  123. $body = json_decode($body, true);
  124. return $body;
  125. }
  126. # 视频转码
  127. public function convert($key, $file, $config, $upload)
  128. {
  129. //?x-oss-process=video/snapshot,t_7000,f_jpg,w_800,h_600,m_fast
  130. //t以后是毫秒
  131. }
  132. # 视频截图
  133. public function cover($key, $video, $num = 1)
  134. {
  135. $num = $num * 1000;
  136. $file = $video . '?x-oss-process=video/snapshot,t_'.$num.',f_jpg,m_fast';
  137. if ($local == 1) {
  138. $data = Dever::load('upload/save')->copy($file, 7, false, false, 'jpg');
  139. return $data['url'] . '?time=' . time();
  140. } else {
  141. return $file;
  142. }
  143. //x-oss-process=video/snapshot,t_1000,f_jpg,m_fast
  144. }
  145. }