Oss.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. public function token($config, $upload)
  14. {
  15. $region = 'cn-' . $config['region_id'];
  16. if (!$config['role_arn']) {
  17. return array('oss', '||' . $config['appkey'] . '||' . $config['appsecret'], $region);
  18. }
  19. $cur = time();
  20. if (!$config['token'] || ($config['token_endtime'] && $cur > $config['token_endtime'])) {
  21. AlibabaCloud::accessKeyClient($config['appkey'], $config['appsecret'])->regionId($region)->asDefaultClient();
  22. //设置参数,发起请求。
  23. try {
  24. $result = AlibabaCloud::rpc()
  25. ->product('Sts')
  26. ->scheme('https') // https | http
  27. ->version('2015-04-01')
  28. ->action('AssumeRole')
  29. ->method('POST')
  30. ->host('sts.aliyuncs.com')
  31. ->options([
  32. 'query' => [
  33. 'RegionId' => $region,
  34. 'RoleArn' => "acs:ram::1118875946432366:role/api",
  35. 'RoleArn' => $config['role_arn'],
  36. 'RoleSessionName' => "upload",
  37. ],
  38. ])
  39. ->request();
  40. $data = $result->toArray();
  41. if (isset($data['Credentials']['SecurityToken'])) {
  42. $token = $data['Credentials']['SecurityToken'];
  43. $endtime = $data['Credentials']['Expiration'];
  44. $appkey = $data['Credentials']['AccessKeyId'];
  45. $appsecret = $data['Credentials']['AccessKeySecret'];
  46. $token = $token . '||' . $appkey . '||' . $appsecret;
  47. $up['token'] = $token;
  48. $up['token_endtime'] = Dever::maketime($endtime) - 60;
  49. $up['where_id'] = $config['id'];
  50. Dever::db('upload/yun')->update($up);
  51. } else {
  52. echo 'oss token获取失败,请检查配置';die;
  53. }
  54. } catch (ClientException $e) {
  55. echo $e->getErrorMessage() . PHP_EOL;die;
  56. } catch (ServerException $e) {
  57. echo $e->getErrorMessage() . PHP_EOL;die;
  58. }
  59. } else {
  60. $token = $config['token'];
  61. }
  62. return array('oss', $token, $region);
  63. }
  64. public function callback()
  65. {
  66. $body = file_get_contents('php://input');
  67. Dever::log($body, 'oss_callback');
  68. $body = json_decode($body, true);
  69. return $body;
  70. }
  71. # 视频转码
  72. public function convert($key, $file, $config, $upload)
  73. {
  74. }
  75. }