Kodo.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace plugin\qiniu_kodo\service;
  3. use laytp\traits\Error;
  4. use Qiniu\Auth;
  5. use Qiniu\Storage\UploadManager;
  6. use think\facade\Config;
  7. /**
  8. * Class Kodo
  9. * @package plugin\qiniu\service
  10. */
  11. class Kodo
  12. {
  13. use Error;
  14. protected static $instance;
  15. /**
  16. * 单例
  17. * @return static
  18. */
  19. public static function instance()
  20. {
  21. if (is_null(self::$instance)) {
  22. self::$instance = new static();
  23. }
  24. return self::$instance;
  25. }
  26. /**
  27. * 上传文件
  28. * @param $localFileName
  29. * @param $saveFileName
  30. * @param $config
  31. * @return bool
  32. * @throws \Exception
  33. */
  34. public function upload($localFileName, $saveFileName, $config)
  35. {
  36. try {
  37. $accessKey = $config['accessKey'];
  38. if (!$accessKey) {
  39. $this->setError('上传失败,七牛云的accessKey为空,请先配置');
  40. return false;
  41. }
  42. $secretKey = $config['secretKey'];
  43. if (!$secretKey) {
  44. $this->setError('上传失败,七牛云的secretKey为空,请先配置');
  45. return false;
  46. }
  47. $bucket = $config['bucket'];
  48. if (!$bucket) {
  49. $this->setError('上传失败,七牛云的bucket为空,请先配置');
  50. return false;
  51. }
  52. $domain = $config['domain'];
  53. if (!$domain) {
  54. $this->setError('上传失败,七牛云的domain为空,请先配置');
  55. return false;
  56. }
  57. $client = new Auth($accessKey, $secretKey);
  58. $token = $client->uploadToken($bucket);
  59. $uploadMgr = new UploadManager();
  60. list($ret, $err) = $uploadMgr->putFile($token, $saveFileName, $localFileName);
  61. if ($err !== null) {
  62. $this->setError('上传失败,' . $err);
  63. return false;
  64. } else {
  65. return $domain . '/' . $saveFileName;
  66. }
  67. } catch (\Exception $e) {
  68. $this->setError('上传失败,' . $e->getMessage());
  69. return false;
  70. }
  71. }
  72. /**
  73. * 客户端获取上传凭证token,然后由客户端JS-SDK将文件上传到七牛云KODO
  74. * @param $config
  75. * @return bool
  76. */
  77. public function token($config){
  78. try {
  79. $accessKey = $config['accessKey'];
  80. if (!$accessKey) {
  81. $this->setError('上传失败,七牛云的accessKey为空,请先配置');
  82. return false;
  83. }
  84. $secretKey = $config['secretKey'];
  85. if (!$secretKey) {
  86. $this->setError('上传失败,七牛云的secretKey为空,请先配置');
  87. return false;
  88. }
  89. $bucket = $config['bucket'];
  90. if (!$bucket) {
  91. $this->setError('上传失败,七牛云的bucket为空,请先配置');
  92. return false;
  93. }
  94. $domain = $config['domain'];
  95. if (!$domain) {
  96. $this->setError('上传失败,七牛云的domain为空,请先配置');
  97. return false;
  98. }
  99. $client = new Auth($accessKey, $secretKey);
  100. $token = $client->uploadToken($bucket);
  101. if ($token) {
  102. return $token;
  103. } else {
  104. $this->setError('七牛云上传凭证Token获取失败');
  105. return false;
  106. }
  107. } catch (\Exception $e) {
  108. $this->setError($e->getMessage());
  109. return false;
  110. }
  111. }
  112. }