Qiniu.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\common\library\storage\engine;
  13. use Qiniu\Auth;
  14. use Qiniu\Storage\UploadManager;
  15. use Qiniu\Storage\BucketManager;
  16. /**
  17. * 七牛云存储引擎
  18. * Class Qiniu
  19. * @package app\common\library\storage\engine
  20. */
  21. class Qiniu extends Basics
  22. {
  23. /**
  24. * 执行上传
  25. * @return bool|mixed
  26. * @throws \Exception
  27. */
  28. public function upload()
  29. {
  30. // 要上传图片的本地路径
  31. $realPath = $this->getRealPath();
  32. // 构建鉴权对象
  33. $auth = new Auth($this->config['access_key'], $this->config['secret_key']);
  34. // 要上传的空间
  35. $token = $auth->uploadToken($this->config['bucket']);
  36. // 初始化 UploadManager 对象并进行文件的上传
  37. $uploadMgr = new UploadManager();
  38. // 调用 UploadManager 的 putFile 方法进行文件的上传
  39. list(, $error) = $uploadMgr->putFile($token, $this->getSaveFileInfo()['file_path'], $realPath);
  40. /* @var $error \Qiniu\Http\Error */
  41. if ($error !== null) {
  42. $this->error = $error->message();
  43. return false;
  44. }
  45. return true;
  46. }
  47. /**
  48. * 删除文件
  49. * @param string $filePath
  50. * @return bool|mixed
  51. */
  52. public function delete(string $filePath)
  53. {
  54. // 构建鉴权对象
  55. $auth = new Auth($this->config['access_key'], $this->config['secret_key']);
  56. // 初始化 UploadManager 对象并进行文件的上传
  57. $bucketMgr = new BucketManager($auth);
  58. /* @var $error \Qiniu\Http\Error */
  59. $error = $bucketMgr->delete($this->config['bucket'], $filePath);
  60. if ($error !== null) {
  61. $this->error = $error->message();
  62. return false;
  63. }
  64. return true;
  65. }
  66. }