-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQiniu.php
executable file
·127 lines (112 loc) · 3.53 KB
/
Qiniu.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017/4/25
* Time: 17:14
*/
namespace gmars\qiniu;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\Cache;
use think\Config;
use think\Exception;
require 'qiniu_driver/autoload.php';
class Qiniu
{
private $_accessKey;
private $_secretKey;
private $_bucket;
private $_error;
/**
* Qiniu constructor.
* @param string $accessKey
* @param string $secretKey
* @param string $bucketName
* 初始化参数可以直接配置到tp的配置中
*/
public function __construct($accessKey = "", $secretKey = "", $bucketName = "")
{
if (empty($accessKey) || empty($secretKey) || empty($bucketName)) {
$qiniuConfig = Config::get('qiniu');
if (empty($qiniuConfig['accesskey']) || empty($qiniuConfig['secretkey'])) {
$this->_error = '你的配置信息不完整!';
return false;
}
$this->_accessKey = $qiniuConfig['accesskey'];
$this->_secretKey = $qiniuConfig['secretkey'];
}else{
$this->_accessKey = $accessKey;
$this->_secretKey = $secretKey;
}
if (!empty($bucketName)) {
$this->_bucket = $bucketName;
}
}
/**
* @return bool|string
* 获取bucket
*/
private function _getBucket()
{
if (!empty($this->_bucket)) {
return $this->_bucket;
}
$bucket = Config::get('qiniu.bucket');
if (empty($bucket)) {
return false;
}
return $bucket;
}
/**
* @param string $saveName
* @param string $bucket
* @return mixed
* @throws Exception
* @throws \Exception
* 单文件上传,如果添加多个文件则只上传第一个
*/
public function upload($saveName = '', $bucket = '')
{
$token = $this->_getUploadToken($bucket);
$files = $_FILES;
if (empty($files)) {
throw new Exception('没有文件被上传', 10002);
}
$values = array_values($files);
$uploadManager = new UploadManager();
if (empty($saveName)) {
$saveName = hash_file('sha1', $values[0]['tmp_name']).time();
}
$infoArr = explode('.', $values[0]['name']);
$extension = array_pop($infoArr);
$fileInfo = $saveName . '.' . $extension;
list($ret, $err) = $uploadManager->putFile($token, $saveName, $values[0]['tmp_name']);
if ($err !== null) {
throw new Exception('上传出错'.serialize($err));
}
return $ret['key'];
}
/**
* @param $bucketName
* @return mixed|string
* @throws Exception
* 只有设置到配置的bucket才会使用缓存功能
*/
private function _getUploadToken($bucketName)
{
$upToken = Cache::get('qiniu_upload_token');
if (!empty($upToken) && empty($bucketName)) {
return $upToken;
}else{
$auth = new Auth($this->_accessKey, $this->_secretKey);
$bucket = empty($bucketName)? $this->_getBucket():$bucketName;
if ($bucket === false) {
throw new Exception('你还没有设置或者传入bucket', 100001);
}
$upToken = $auth->uploadToken($bucket);
Cache::set('qiniu_upload_token', $upToken);
return $upToken;
}
}
}