Qiniu.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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\Http\Error;
  15. use Qiniu\Storage\UploadManager;
  16. use Qiniu\Storage\BucketManager;
  17. /**
  18. * 七牛云存储引擎
  19. * Class Qiniu
  20. * @package app\common\library\storage\engine
  21. */
  22. class Qiniu extends Basics
  23. {
  24. /**
  25. * 执行上传
  26. * @return bool
  27. * @throws \Exception
  28. */
  29. public function upload(): bool
  30. {
  31. // 要上传图片的本地路径
  32. $realPath = $this->getRealPath();
  33. // 构建鉴权对象
  34. $auth = new Auth($this->config['access_key'], $this->config['secret_key']);
  35. // 要上传的空间
  36. $token = $auth->uploadToken($this->config['bucket']);
  37. // 初始化 UploadManager 对象并进行文件的上传
  38. $uploadMgr = new UploadManager();
  39. // 调用 UploadManager 的 putFile 方法进行文件的上传
  40. [, $error] = $uploadMgr->putFile($token, $this->getSaveFileInfo()['file_path'], $realPath);
  41. /* @var $error Error */
  42. if ($error !== null) {
  43. $this->error = $error->message();
  44. return false;
  45. }
  46. return true;
  47. }
  48. /**
  49. * 删除文件
  50. * @param string $filePath
  51. * @return bool
  52. */
  53. public function delete(string $filePath): bool
  54. {
  55. // 构建鉴权对象
  56. $auth = new Auth($this->config['access_key'], $this->config['secret_key']);
  57. // 初始化 UploadManager 对象并进行文件的上传
  58. $bucketMgr = new BucketManager($auth);
  59. /* @var $error Error */
  60. [, $error] = $bucketMgr->delete($this->config['bucket'], $filePath);
  61. if ($error !== null) {
  62. $this->error = $error->message();
  63. return false;
  64. }
  65. return true;
  66. }
  67. }