Qiniu.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\librarys\storage\drivers;
  8. use app\librarys\storage\exceptions\StorageException;
  9. use Qiniu\Auth;
  10. use Qiniu\Storage\UploadManager;
  11. class Qiniu extends BaseDriver
  12. {
  13. public $region = '';
  14. /**
  15. * Qiniu auth class
  16. *
  17. * @var Auth
  18. */
  19. protected $qiniuAuth;
  20. /**
  21. * Qiniu upload manager class
  22. *
  23. * @var UploadManager
  24. */
  25. protected $qiniuUploadManager;
  26. public function __construct($config = [])
  27. {
  28. parent::__construct($config);
  29. $this->qiniuAuth = new Auth($this->accessKey, $this->secretKey);
  30. // $zone = \Qiniu\Zone::zonez0(); //华东
  31. $arr = array('upload.qiniup.com', 'upload-z1.qiniup.com', 'upload-z2.qiniup.com', 'upload-as0.qiniup.com', 'upload-na0.qiniup.com');
  32. $url = $arr[(int)$this->zone];
  33. $zone = new \Qiniu\Zone([$url]);
  34. $config1 = new \Qiniu\Config($zone);
  35. $this->qiniuUploadManager = new UploadManager($config1);
  36. }
  37. public function put($localFile, $saveTo)
  38. {
  39. $token = $this->qiniuAuth->uploadToken($this->bucket);
  40. list($res, $err) = $this->qiniuUploadManager->putFile($token, $saveTo, $localFile);
  41. if ($err !== null) {
  42. throw new StorageException($err->message());
  43. }
  44. $ress = $this->region . '/' . $res['key'];
  45. return $ress;
  46. }
  47. }