Qiniuoss.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. // by 请勿倒卖,已申请软著,否则追究法律责任
  3. namespace app\qiniuoss;
  4. use Qiniu\Auth;
  5. use Qiniu\Config;
  6. use Qiniu\Storage\UploadManager;
  7. use Qiniu\Storage\BucketManager;
  8. use Qiniu\Cdn\CdnManager;
  9. class Qiniuoss
  10. {
  11. public $config = ["accessKeyId" => IN_REMOTEAK, "accessKeySecret" => IN_REMOTESK, "bucket" => IN_REMOTEBK, "domain" => IN_REMOTEDK];
  12. public $bucket = "";
  13. public $domain = "";
  14. public $auth;
  15. public $bucketManager;
  16. public $token = "";
  17. function __construct($config = [])
  18. {
  19. if (!empty($config)) {
  20. $this->config = $config;
  21. }
  22. $accessKeyId = $this->config['accessKeyId'];
  23. $accessKeySecret = $this->config['accessKeySecret'];
  24. $this->bucket = $this->config['bucket'];
  25. $this->domain = $this->config['domain'];
  26. $this->auth = new Auth($accessKeyId, $accessKeySecret);
  27. $this->token = $this->getToken();
  28. $this->config = new Config();
  29. }
  30. function getToken()
  31. {
  32. return $this->auth->uploadToken($this->bucket);
  33. }
  34. function download($Key)
  35. {
  36. return false;
  37. }
  38. function delete($Key)
  39. {
  40. if (!$this->file_exists($Key)) {
  41. return true;
  42. }
  43. $this->bucketManager = new BucketManager($this->auth, $this->config);
  44. list($fileInfo, $err) = $this->bucketManager->delete($this->bucket, $Key);
  45. if ($err) {
  46. return false;
  47. } else {
  48. return true;
  49. }
  50. }
  51. function file_exists($Key)
  52. {
  53. $this->bucketManager = new BucketManager($this->auth, $this->config);
  54. list($fileInfo, $err) = $this->bucketManager->stat($this->bucket, $Key);
  55. if ($err) {
  56. return false;
  57. } else {
  58. return array('src' => $Key, 'domain_src' => $this->domain . $Key);
  59. }
  60. }
  61. function upload($Key = '', $filePath = '')
  62. {
  63. if ($Key && $filePath) {
  64. } else {
  65. if (!$_FILES && !isset($_FILES['file'])) {
  66. return array('msg' => '无法识别的文件');
  67. }
  68. $name = date('Ymd-His') . $_FILES['file']['name'];
  69. $Key = "uploads/test/" . $name;
  70. $filePath = $_FILES['file']['tmp_name'];
  71. }
  72. $uploadMgr = new UploadManager();
  73. try {
  74. list($ret, $err) = $uploadMgr->putFile($this->token, $Key, $filePath);
  75. return array('src' => $Key, 'domain_src' => $this->domain . $Key);
  76. } catch (\Exception $exception) {
  77. return array('error' => $exception);
  78. }
  79. }
  80. function refresh($urls = [], $dirs = [])
  81. {
  82. $cdnManager = new CdnManager($this->auth);
  83. $res = array();
  84. if ($urls) {
  85. list($refreshResult, $refreshErr) = $cdnManager->refreshUrls($urls);
  86. if ($refreshErr != null) {
  87. $res['urls'] = $refreshErr;
  88. } else {
  89. $res['urls'] = $refreshResult;
  90. }
  91. }
  92. if ($dirs) {
  93. list($refreshResult, $refreshErr) = $cdnManager->refreshDirs($dirs);
  94. if ($refreshErr != null) {
  95. $res['dirs'] = $refreshErr;
  96. } else {
  97. $res['dirs'] = $refreshResult;
  98. }
  99. }
  100. return $res;
  101. }
  102. }