Oss.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php namespace Upload\Lib\Tool;
  2. use Dever;
  3. Dever::apply('sdk/oss', 'upload');
  4. # 后续开发吧
  5. class Oss
  6. {
  7. private $token;
  8. private $auth;
  9. private $bucket;
  10. public function __construct($config)
  11. {
  12. $this->auth = new \Qiniu\Auth($config['appkey'], $config['appsecret']);
  13. $this->token = $this->auth->uploadToken($config['bucket'], null, 3600);
  14. $this->bucket = $config['bucket'];
  15. $this->client = new OssClient($accessKey, $secretKey, $endpoint, false, $token);
  16. }
  17. # 获取基本信息
  18. public function getInfo()
  19. {
  20. return [
  21. 'token' => $this->token,
  22. 'bucket' => $this->bucket,
  23. 'host' => $this->host,
  24. ];
  25. }
  26. # 查看文件
  27. public function view($file)
  28. {
  29. return $this->auth->privateDownloadUrl($file);
  30. }
  31. # 上传文件
  32. public function upload($type, $source, $dest)
  33. {
  34. $ret = [];
  35. $options = [];
  36. if ($type == 1) {
  37. $client = new \Qiniu\Storage\UploadManager();
  38. list($ret, $err) = $client->putFile($this->token, $dest, $source, $options);
  39. } elseif ($type == 2) {
  40. $client = new \Qiniu\Storage\UploadManager();
  41. list($ret, $err) = $client->put($this->token, $dest, $source, $options);
  42. } elseif (strstr($source, 'http')) {
  43. $method = 'fetch';
  44. $client = new \Qiniu\Storage\BucketManager($this->auth);
  45. $items = $client->fetch($source, $this->bucket, $dest);
  46. if (isset($items[0])) {
  47. $ret = $items[0];
  48. }
  49. } else {
  50. Dever::error('上传类型无效');
  51. }
  52. return $ret;
  53. }
  54. # 删除文件
  55. public function delete($file, $bucket = false)
  56. {
  57. $bucket = $bucket ? $bucket : $this->bucket;
  58. $client = new \Qiniu\Storage\BucketManager($this->auth);
  59. $result = $client->delete($bucket, $file);
  60. return $result;
  61. }
  62. # 下载文件
  63. public function download($file, $bucket = false)
  64. {
  65. $bucket = $bucket ? $bucket : $this->bucket;
  66. $client = new \Qiniu\Storage\UploadManager();
  67. $content = $client->getObject($bucket, $file, []);
  68. return $content;
  69. }
  70. # 视频截图 vframe/jpg/offset/7/w/480/h/360
  71. public function cover($key, $file, $num = 1, $local = 2)
  72. {
  73. $file .= '?vframe/jpg/offset/' . $num;
  74. if ($local == 1) {
  75. $data = Dever::load('upload')->save(6, $file, 'jpg');
  76. return $data['url'] . '?time=' . time();
  77. } else {
  78. return $file;
  79. }
  80. }
  81. }