123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- // by 请勿倒卖,已申请软著,否则追究法律责任
- namespace app\qiniuoss;
- use Qiniu\Auth;
- use Qiniu\Config;
- use Qiniu\Storage\UploadManager;
- use Qiniu\Storage\BucketManager;
- use Qiniu\Cdn\CdnManager;
- class Qiniuoss
- {
- public $config = ["accessKeyId" => IN_REMOTEAK, "accessKeySecret" => IN_REMOTESK, "bucket" => IN_REMOTEBK, "domain" => IN_REMOTEDK];
- public $bucket = "";
- public $domain = "";
- public $auth;
- public $bucketManager;
- public $token = "";
- function __construct($config = [])
- {
- if (!empty($config)) {
- $this->config = $config;
- }
- $accessKeyId = $this->config['accessKeyId'];
- $accessKeySecret = $this->config['accessKeySecret'];
- $this->bucket = $this->config['bucket'];
- $this->domain = $this->config['domain'];
- $this->auth = new Auth($accessKeyId, $accessKeySecret);
- $this->token = $this->getToken();
- $this->config = new Config();
- }
- function getToken()
- {
- return $this->auth->uploadToken($this->bucket);
- }
- function download($Key)
- {
- return false;
- }
- function delete($Key)
- {
- if (!$this->file_exists($Key)) {
- return true;
- }
- $this->bucketManager = new BucketManager($this->auth, $this->config);
- list($fileInfo, $err) = $this->bucketManager->delete($this->bucket, $Key);
- if ($err) {
- return false;
- } else {
- return true;
- }
- }
- function file_exists($Key)
- {
- $this->bucketManager = new BucketManager($this->auth, $this->config);
- list($fileInfo, $err) = $this->bucketManager->stat($this->bucket, $Key);
- if ($err) {
- return false;
- } else {
- return array('src' => $Key, 'domain_src' => $this->domain . $Key);
- }
- }
- function upload($Key = '', $filePath = '')
- {
- if ($Key && $filePath) {
- } else {
- if (!$_FILES && !isset($_FILES['file'])) {
- return array('msg' => '无法识别的文件');
- }
- $name = date('Ymd-His') . $_FILES['file']['name'];
- $Key = "uploads/test/" . $name;
- $filePath = $_FILES['file']['tmp_name'];
- }
- $uploadMgr = new UploadManager();
- try {
- list($ret, $err) = $uploadMgr->putFile($this->token, $Key, $filePath);
- return array('src' => $Key, 'domain_src' => $this->domain . $Key);
- } catch (\Exception $exception) {
- return array('error' => $exception);
- }
- }
- function refresh($urls = [], $dirs = [])
- {
- $cdnManager = new CdnManager($this->auth);
- $res = array();
- if ($urls) {
- list($refreshResult, $refreshErr) = $cdnManager->refreshUrls($urls);
- if ($refreshErr != null) {
- $res['urls'] = $refreshErr;
- } else {
- $res['urls'] = $refreshResult;
- }
- }
- if ($dirs) {
- list($refreshResult, $refreshErr) = $cdnManager->refreshDirs($dirs);
- if ($refreshErr != null) {
- $res['dirs'] = $refreshErr;
- } else {
- $res['dirs'] = $refreshResult;
- }
- }
- return $res;
- }
- }
|