Qiniu.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace common\components\uploaddrive;
  3. use Yii;
  4. use Qiniu\Auth;
  5. use Qiniu\Storage\UploadManager;
  6. use Overtrue\Flysystem\Qiniu\Plugins\FileUrl;
  7. use Overtrue\Flysystem\Qiniu\QiniuAdapter;
  8. /**
  9. * Class Qiniu
  10. * @package common\components\uploaddrive
  11. */
  12. class Qiniu extends DriveInterface
  13. {
  14. /**
  15. * @var string[]
  16. */
  17. protected $host_area = [
  18. 1 => 'upload.qiniup.com', // 华东
  19. 2 => 'upload-z1.qiniup.com', // 华北区
  20. 3 => 'upload-z2.qiniup.com', // 华南区
  21. 4 => 'upload-na0.qiniup.com', // 北美区
  22. 5 => 'upload-as0.qiniup.com', // 东南亚区
  23. ];
  24. /**
  25. * 直传 token
  26. *
  27. * @return array
  28. */
  29. public function config()
  30. {
  31. $config = $this->config;
  32. $accessKey = $config['storage_qiniu_accesskey'];
  33. $secretKey = $config['storage_qiniu_secrectkey'];
  34. $host = $config['storage_qiniu_host'];
  35. $auth = new Auth($accessKey, $secretKey);
  36. $bucket = $config['storage_qiniu_bucket'];
  37. // 生成上传Token
  38. $token = $auth->uploadToken($bucket);
  39. return [
  40. 'token' => $token,
  41. 'x:host' => $this->host_area[$host],
  42. ];
  43. }
  44. /**
  45. * @param $baseInfo
  46. * @param $fullPath
  47. */
  48. protected function baseUrl($baseInfo, $fullPath)
  49. {
  50. $this->filesystem->addPlugin(new FileUrl());
  51. $baseInfo['url'] = $this->filesystem->getUrl($baseInfo['url']);
  52. return $baseInfo;
  53. }
  54. /**
  55. * @param $baseInfo
  56. * @param $fullPath
  57. */
  58. protected function domainName($baseInfo, $fullPath)
  59. {
  60. $this->filesystem->addPlugin(new FileUrl());
  61. $baseInfo['domain_name'] = $this->filesystem->getUrl($baseInfo['domain_name']);
  62. return $baseInfo;
  63. }
  64. /**
  65. * @return mixed|void
  66. * @throws \Exception
  67. */
  68. protected function create()
  69. {
  70. $config = $this->config;
  71. $accessKey = $config['access_key_id'];
  72. $secretKey = $config['access_key_secret'];
  73. $cdnHost = $config['domain'];
  74. $bucket = $config['bucket'];
  75. $this->adapter = new QiniuAdapter($accessKey, $secretKey, $bucket, $cdnHost);
  76. }
  77. }