Oss.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 array
  21. (
  22. 'token' => $this->token,
  23. 'bucket' => $this->bucket,
  24. 'host' => $this->host,
  25. );
  26. }
  27. # 查看文件
  28. public function view($file)
  29. {
  30. return $this->auth->privateDownloadUrl($file);
  31. }
  32. # 上传文件
  33. public function upload($type, $source, $dest)
  34. {
  35. $ret = array();
  36. $options = array();
  37. if ($type == 1) {
  38. $client = new \Qiniu\Storage\UploadManager();
  39. list($ret, $err) = $client->putFile($this->token, $dest, $source, $options);
  40. } elseif ($type == 2) {
  41. $client = new \Qiniu\Storage\UploadManager();
  42. list($ret, $err) = $client->put($this->token, $dest, $source, $options);
  43. } elseif (strstr($source, 'http')) {
  44. $method = 'fetch';
  45. $client = new \Qiniu\Storage\BucketManager($this->auth);
  46. $items = $client->fetch($source, $this->bucket, $dest);
  47. if (isset($items[0])) {
  48. $ret = $items[0];
  49. }
  50. } else {
  51. Dever::error('上传类型无效');
  52. }
  53. return $ret;
  54. }
  55. # 删除文件
  56. public function delete($file, $bucket = false)
  57. {
  58. $bucket = $bucket ? $bucket : $this->bucket;
  59. $client = new \Qiniu\Storage\BucketManager($this->auth);
  60. $result = $client->delete($bucket, $file);
  61. return $result;
  62. }
  63. # 下载文件
  64. public function download($file, $bucket = false)
  65. {
  66. $bucket = $bucket ? $bucket : $this->bucket;
  67. $client = new \Qiniu\Storage\UploadManager();
  68. $content = $client->getObject($bucket, $file, array());
  69. return $content;
  70. }
  71. # 视频截图 vframe/jpg/offset/7/w/480/h/360
  72. public function cover($key, $file, $num = 1, $local = 2)
  73. {
  74. $file .= '?vframe/jpg/offset/' . $num;
  75. if ($local == 1) {
  76. $data = Dever::load('upload')->save(6, $file, 'jpg');
  77. return $data['url'] . '?time=' . time();
  78. } else {
  79. return $file;
  80. }
  81. }
  82. }