1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Qiniu objcet store 封装 |
4
|
|
|
* Class QiniuStoreAPI |
5
|
|
|
* @author iscod-ning |
6
|
|
|
* @computer kukuxiu |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
function classLoader($class) |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
$path = str_replace('\\', DIRECTORY_SEPARATOR, $class); |
12
|
|
|
$file = __DIR__ . '/src/' . $path . '.php'; |
13
|
|
|
|
14
|
|
|
if (file_exists($file)) { |
15
|
|
|
require_once $file; |
16
|
|
|
} |
17
|
|
|
} |
18
|
|
|
spl_autoload_register('classLoader'); |
19
|
|
|
|
20
|
|
|
require_once __DIR__ . '/src/Qiniu/functions.php'; |
21
|
|
|
|
22
|
|
|
use Qiniu\Auth; |
23
|
|
|
use Qiniu\Storage\BucketManager; |
24
|
|
|
use Qiniu\Storage\UploadManager; |
25
|
|
|
|
26
|
|
|
class QiniuStoreAPI{ |
27
|
|
|
|
28
|
|
|
private $accessKey = 'accessKey'; //AK |
29
|
|
|
private $secretKey = 'secretKey'; //SK |
30
|
|
|
private $bucket = 'bucket';//默认空间名 |
31
|
|
|
private $auth; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* 参数初始化 |
35
|
|
|
* @param $appKey |
36
|
|
|
* @param $appSecret |
37
|
|
|
* @param string $format |
|
|
|
|
38
|
|
|
*/ |
39
|
|
|
public function __construct(){ |
40
|
|
|
//初始化Auth状态: |
41
|
|
|
$auth = new Auth($this->accessKey, $this->secretKey); |
42
|
|
|
$this->auth = $auth; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
*第三方资源抓取 |
47
|
|
|
*@param $url第三方地址 |
48
|
|
|
*@param $bucket云空间 |
49
|
|
|
*@param $filename生成的名称 |
50
|
|
|
*@return bool |
51
|
|
|
*/ |
52
|
|
|
public function fetch($url, $filename, $bucket = ''){ |
53
|
|
|
if (!$bucket) $bucket = $this->bucket; |
54
|
|
|
$bmgr = new BucketManager($this->auth); |
55
|
|
|
|
56
|
|
|
list($ret, $err) = $bmgr->fetch($url, $bucket, $filename); |
57
|
|
|
if ($err !== null) { |
58
|
|
|
return false; |
59
|
|
|
}else{ |
60
|
|
|
return isset($ret['key']) ? $ret['key'] : ''; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
*@param $filePath 要上传文件的本地路径 |
66
|
|
|
*@param $key 上传到七牛后保存的文件名 |
67
|
|
|
*@param $bucket 上传的空间名称 |
68
|
|
|
*@return bool |
69
|
|
|
*/ |
70
|
|
|
public function Upload($filePath, $key, $bucket = ''){ |
71
|
|
|
if (!$bucket) $bucket = $this->bucket; |
72
|
|
|
$auth = $this->auth; |
73
|
|
|
|
74
|
|
|
// 生成上传 Token |
75
|
|
|
$token = $auth->uploadToken($bucket); |
76
|
|
|
|
77
|
|
|
// 初始化 UploadManager 对象并进行文件的上传。 |
78
|
|
|
$uploadMgr = new UploadManager(); |
79
|
|
|
|
80
|
|
|
list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath); |
81
|
|
|
|
82
|
|
|
if ($err !== null) { |
83
|
|
|
return false; |
84
|
|
|
} else { |
85
|
|
|
return isset($ret['key']) ? $ret['key'] : ''; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
*删除文件 |
91
|
|
|
*/ |
92
|
|
|
public function delete($key, $bucket = ''){ |
93
|
|
|
$auth = $this->auth; |
94
|
|
|
if (!$bucket) $bucket = $this->bucket; |
95
|
|
|
//初始化BucketManager |
96
|
|
|
$bucketMgr = new BucketManager($auth); |
97
|
|
|
|
98
|
|
|
$err = $bucketMgr->delete($bucket, $key); |
99
|
|
|
|
100
|
|
|
if ($err !== null) { |
101
|
|
|
return false; |
102
|
|
|
} else { |
103
|
|
|
return true; |
104
|
|
|
echo "Success!"; |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
} |
This check looks for functions that have already been defined in other files.
Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the
@ignore
annotation.See also the PhpDoc documentation for @ignore.