Qiniu.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace app\common\service\storage\engine;
  3. use Exception;
  4. use Qiniu\Auth;
  5. use Qiniu\Storage\UploadManager;
  6. use Qiniu\Storage\BucketManager;
  7. /**
  8. * 七牛云存储引擎
  9. * Class Qiniu
  10. * @package app\common\library\storage\engine
  11. */
  12. class Qiniu extends Server
  13. {
  14. private $config;
  15. /**
  16. * 构造方法
  17. * Qiniu constructor.
  18. * @param $config
  19. */
  20. public function __construct($config)
  21. {
  22. parent::__construct();
  23. $this->config = $config;
  24. }
  25. /**
  26. * @notes 执行上传
  27. * @param $save_dir
  28. * @return bool|mixed
  29. * @author 张无忌
  30. * @date 2021/7/27 16:02
  31. */
  32. public function upload($save_dir)
  33. {
  34. // 要上传图片的本地路径
  35. $realPath = $this->getRealPath();
  36. // 构建鉴权对象
  37. $auth = new Auth($this->config['access_key'], $this->config['secret_key']);
  38. // 要上传的空间
  39. $token = $auth->uploadToken($this->config['bucket']);
  40. // 初始化 UploadManager 对象并进行文件的上传
  41. $uploadMgr = new UploadManager();
  42. try {
  43. // 调用 UploadManager 的 putFile 方法进行文件的上传
  44. $key = $save_dir . '/' . $this->fileName;
  45. list(, $error) = $uploadMgr->putFile($token, $key, $realPath);
  46. if ($error !== null) {
  47. $this->error = $error->message();
  48. return false;
  49. }
  50. return true;
  51. } catch (Exception $e) {
  52. $this->error = $e->getMessage();
  53. return false;
  54. }
  55. }
  56. /**
  57. * @notes 抓取远程资源
  58. * @param $url
  59. * @param null $key
  60. * @return bool|mixed
  61. * @author 张无忌
  62. * @date 2021/7/27 16:02
  63. */
  64. public function fetch($url, $key=null)
  65. {
  66. try {
  67. if (substr($url, 0, 1) !== '/' || strstr($url, 'http://') || strstr($url, 'https://')) {
  68. $auth = new Auth($this->config['access_key'], $this->config['secret_key']);
  69. $bucketManager = new BucketManager($auth);
  70. list(, $err) = $bucketManager->fetch($url, $this->config['bucket'], $key);
  71. } else {
  72. $auth = new Auth($this->config['access_key'], $this->config['secret_key']);
  73. $token = $auth->uploadToken($this->config['bucket']);
  74. $uploadMgr = new UploadManager();
  75. list(, $err) = $uploadMgr->putFile($token, $key, $url);
  76. }
  77. if ($err !== null) {
  78. $this->error = $err->message();
  79. return false;
  80. }
  81. return true;
  82. } catch (Exception $e) {
  83. $this->error = $e->getMessage();
  84. return false;
  85. }
  86. }
  87. /**
  88. * @notes 删除文件
  89. * @param $fileName
  90. * @return bool|mixed
  91. * @author 张无忌
  92. * @date 2021/7/27 16:02
  93. */
  94. public function delete($fileName)
  95. {
  96. // 构建鉴权对象
  97. $auth = new Auth($this->config['access_key'], $this->config['secret_key']);
  98. // 初始化 UploadManager 对象并进行文件的上传
  99. $bucketMgr = new BucketManager($auth);
  100. try {
  101. $error = $bucketMgr->delete($this->config['bucket'], $fileName);
  102. if ($error !== null) {
  103. $this->error = $error->message();
  104. return false;
  105. }
  106. return true;
  107. } catch (Exception $e) {
  108. $this->error = $e->getMessage();
  109. return false;
  110. }
  111. }
  112. /**
  113. * 返回文件路径
  114. * @return mixed
  115. */
  116. public function getFileName()
  117. {
  118. return $this->fileName;
  119. }
  120. }