OSS.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace common\components\uploaddrive;
  3. use Yii;
  4. use yii\helpers\Json;
  5. use common\models\common\Attachment;
  6. use Xxtime\Flysystem\Aliyun\OssAdapter;
  7. /**
  8. * Class OSS
  9. * @package common\components\uploaddrive
  10. */
  11. class OSS extends DriveInterface
  12. {
  13. /**
  14. * 获取阿里云js直传
  15. *
  16. * @param $maxSize
  17. * @param string dir 用户上传文件时指定的前缀
  18. * @param int $expire 设置该policy超时时间是10s. 即这个policy过了这个有效时间,将不能访问
  19. * @param string $type 文件类型
  20. * @param string $callbackUrl 为上传回调服务器的URL,请将下面的IP和Port配置为您自己的真实URL信息
  21. * @return array
  22. * @throws \Exception
  23. */
  24. public function config($maxSize, $path = '', $expire = 30, $type = Attachment::UPLOAD_TYPE_FILES, $callbackUrl = '')
  25. {
  26. $config = $this->config;
  27. $id = $config['storage_aliyun_accesskeyid'];
  28. $key = $config['storage_aliyun_accesskeysecret'];
  29. $bucket = $config['storage_aliyun_bucket'];
  30. $endpoint = $config['storage_aliyun_endpoint'];
  31. $host = "https://$bucket.$endpoint";
  32. // CNAME别名
  33. if (!empty($config['storage_aliyun_user_url'])) {
  34. $host = $config['storage_aliyun_transport_protocols'] . "://" . $config['storage_aliyun_user_url'];
  35. }
  36. //回调地址
  37. //!$callbackUrl && $callbackUrl = Url::toFront(['storage/oss'], true);
  38. $callback_param = [
  39. 'callbackUrl' => $callbackUrl,
  40. 'callbackBody' => 'filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}&format=${imageInfo.format}&md5=${x:md5}&merchant_id=${x:merchant_id}&type=${x:type}&host=${x:host}&upload_id=${x:upload_id}',
  41. 'callbackBodyType' => "application/x-www-form-urlencoded"
  42. ];
  43. $base64_callback_body = base64_encode(Json::encode($callback_param));
  44. $expiration = $this->expiration(time() + $expire);
  45. // 最大文件大小
  46. $conditions[] = ['content-length-range', 0, $maxSize];
  47. // 表示用户上传的数据,必须是以$dir开始,不然上传会失败,这一步不是必须项,只是为了安全起见,防止用户通过policy上传到别人的目录。
  48. // $conditions[] = ['starts-with','$filename', $dir];
  49. $arr = [
  50. 'expiration' => $expiration,
  51. 'conditions' => $conditions
  52. ];
  53. $policy = Json::encode($arr);
  54. $base64_policy = base64_encode($policy);
  55. $signature = base64_encode(hash_hmac('sha1', $base64_policy, $key, true));
  56. return [
  57. 'Filename' => '${filename}',
  58. 'key' => $path . '${filename}',
  59. 'OSSAccessKeyId' => $id,
  60. 'success_action_status' => '201',
  61. 'host' => $host,
  62. 'policy' => $base64_policy,
  63. 'signature' => $signature,
  64. 'callback' => $base64_callback_body,
  65. 'x:type' => $type,
  66. 'x:host' => $host,
  67. ];
  68. }
  69. /**
  70. * 截止日期
  71. *
  72. * @param $time
  73. * @return string
  74. * @throws \Exception
  75. */
  76. protected function expiration($time)
  77. {
  78. $dtStr = date("c", $time);
  79. $datatime = new \DateTime($dtStr);
  80. $expiration = $datatime->format(\DateTime::ISO8601);
  81. $pos = strpos($expiration, '+');
  82. $expiration = substr($expiration, 0, $pos);
  83. return $expiration . "Z";
  84. }
  85. /**
  86. * @param $baseInfo
  87. * @param $fullPath
  88. * @return mixed
  89. */
  90. protected function baseUrl($baseInfo, $fullPath)
  91. {
  92. $user_url = $this->config['storage_aliyun_user_url'];
  93. if (!empty($user_url)) {
  94. $baseInfo['url'] = $this->config['storage_aliyun_transport_protocols'] . '://' . $user_url . '/' . $baseInfo['url'];
  95. } else {
  96. $raw = $this->adapter->supports->getFlashData();
  97. $baseInfo['url'] = $raw['info']['url'];
  98. }
  99. return $baseInfo;
  100. }
  101. /**
  102. * @param $baseInfo
  103. * @param $fullPath
  104. * @return mixed
  105. */
  106. protected function domainName($baseInfo, $fullPath)
  107. {
  108. $user_url = $this->config['storage_aliyun_user_url'];
  109. if (!empty($user_url)) {
  110. $baseInfo['domain_name'] = $this->config['storage_aliyun_transport_protocols'] . '://' . $user_url . '/';
  111. } else {
  112. $raw = $this->adapter->supports->getFlashData();
  113. $baseInfo['domain_name'] = $raw['info']['domain_name'];
  114. }
  115. return $baseInfo;
  116. }
  117. /**
  118. * @return mixed|void
  119. * @throws \Exception
  120. */
  121. protected function create()
  122. {
  123. $this->adapter = new OssAdapter([
  124. 'accessId' => $this->config['storage_aliyun_accesskeyid'],
  125. 'accessSecret' => $this->config['storage_aliyun_accesskeysecret'],
  126. 'bucket' => $this->config['storage_aliyun_bucket'],
  127. 'endpoint' => ($this->config['storage_aliyun_is_internal'] ?? false) == true ? $this->config['storage_aliyun_endpoint_internal'] : $this->config['storage_aliyun_endpoint'],
  128. // 'timeout' => 3600,
  129. // 'connectTimeout' => 10,
  130. // 'isCName' => false,
  131. // 'token' => '',
  132. ]);
  133. }
  134. }