Oss.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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)
  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)
  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);
  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 download($bucket, $file, $local = false)
  98. {
  99. if (!$this->client) {
  100. return false;
  101. }
  102. if ($local) {
  103. $options = array(
  104. OssClient::OSS_FILE_DOWNLOAD => $local
  105. );
  106. } else {
  107. $options = array();
  108. }
  109. $content = $this->client->getObject($bucket, $file, $options);
  110. return $content;
  111. }
  112. public function callback()
  113. {
  114. $body = file_get_contents('php://input');
  115. Dever::log($body, 'oss_callback');
  116. $body = json_decode($body, true);
  117. return $body;
  118. }
  119. # 视频转码
  120. public function convert($key, $file, $config, $upload)
  121. {
  122. }
  123. }