|
1
|
|
|
<?php |
|
2
|
|
|
namespace RazonYang\Yii2\Flysystem; |
|
3
|
|
|
|
|
4
|
|
|
use Aliyun\Flysystem\AliyunOss\AliyunOssAdapter; |
|
5
|
|
|
use OSS\OssClient; |
|
6
|
|
|
use creocoder\flysystem\Filesystem; |
|
7
|
|
|
use yii\base\InvalidArgumentException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* AliyunOssFilesystem |
|
11
|
|
|
* |
|
12
|
|
|
* @property string $accessKeyId access key ID. |
|
13
|
|
|
* @property string $accessKeySecret access key secret. |
|
14
|
|
|
* @property string $endpoint endpoint. |
|
15
|
|
|
* @property bool $isCName whether the endpoint is your cname domain. |
|
16
|
|
|
* @property null|string $securityToken security token. |
|
17
|
|
|
* @property string $bucket bucket name. |
|
18
|
|
|
* @property string $prefix prefix. |
|
19
|
|
|
* @property array $options options. |
|
20
|
|
|
*/ |
|
21
|
|
|
class AliyunOssFilesystem extends Filesystem |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string $accessKeyId access key ID. |
|
25
|
|
|
*/ |
|
26
|
|
|
private $accessKeyId; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Gets access key ID. |
|
30
|
|
|
* |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
3 |
|
public function getAccessKeyId(): string |
|
34
|
|
|
{ |
|
35
|
3 |
|
return $this->accessKeyId; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Sets access key ID. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $id |
|
42
|
|
|
*/ |
|
43
|
7 |
|
public function setAccessKeyId(string $id): void |
|
44
|
|
|
{ |
|
45
|
7 |
|
$this->accessKeyId = $id; |
|
46
|
7 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string $accessKeySecret access key secret. |
|
50
|
|
|
*/ |
|
51
|
|
|
private $accessKeySecret; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Sets access key secret. |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
3 |
|
public function getAccessKeySecret(): string |
|
59
|
|
|
{ |
|
60
|
3 |
|
return $this->accessKeySecret; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Sets access key secret. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $secret |
|
67
|
|
|
*/ |
|
68
|
7 |
|
public function setAccessKeySecret(string $secret): void |
|
69
|
|
|
{ |
|
70
|
7 |
|
$this->accessKeySecret = $secret; |
|
71
|
7 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var string $endpoint |
|
75
|
|
|
*/ |
|
76
|
|
|
private $endpoint; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Gets endpoint. |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
3 |
|
public function getEndpoint(): string |
|
84
|
|
|
{ |
|
85
|
3 |
|
return $this->endpoint; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Sets endpoint. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $endpoint |
|
92
|
|
|
*/ |
|
93
|
7 |
|
public function setEndpoint(string $endpoint): void |
|
94
|
|
|
{ |
|
95
|
7 |
|
$this->endpoint = $endpoint; |
|
96
|
7 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @var bool $isCName |
|
100
|
|
|
*/ |
|
101
|
|
|
private $isCName = false; |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Returns a bool value indicates that whether the endpoint is your custom domain. |
|
105
|
|
|
* |
|
106
|
|
|
* @return bool |
|
107
|
|
|
*/ |
|
108
|
2 |
|
public function getIsCName(): bool |
|
109
|
|
|
{ |
|
110
|
2 |
|
return $this->isCName; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Sets a bool value indicates that whether the endpoint is your custom domain. |
|
115
|
|
|
* |
|
116
|
|
|
* @param bool $value |
|
117
|
|
|
*/ |
|
118
|
2 |
|
public function setIsCName(bool $value): void |
|
119
|
|
|
{ |
|
120
|
2 |
|
$this->isCName = $value; |
|
121
|
2 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
private $securityToken; |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Gets security token. |
|
127
|
|
|
* |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
2 |
|
public function getSecurityToken(): ?string |
|
131
|
|
|
{ |
|
132
|
2 |
|
return $this->securityToken; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Sets security token. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $token |
|
139
|
|
|
*/ |
|
140
|
2 |
|
public function setSecurityToken(string $token): void |
|
141
|
|
|
{ |
|
142
|
2 |
|
$this->securityToken = $token; |
|
143
|
2 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @var string $bucket bucket name. |
|
147
|
|
|
*/ |
|
148
|
|
|
private $bucket; |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Gets bucket name. |
|
152
|
|
|
* |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
3 |
|
public function getBucket(): string |
|
156
|
|
|
{ |
|
157
|
3 |
|
return $this->bucket; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Sets bucket name. |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $bucket |
|
164
|
|
|
*/ |
|
165
|
7 |
|
public function setBucket(string $bucket): void |
|
166
|
|
|
{ |
|
167
|
7 |
|
$this->bucket = $bucket; |
|
168
|
7 |
|
} |
|
169
|
|
|
|
|
170
|
|
|
private $prefix; |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Gets prefix. |
|
174
|
|
|
* |
|
175
|
|
|
* @return null|string |
|
176
|
|
|
*/ |
|
177
|
2 |
|
public function getPrefix(): ?string |
|
178
|
|
|
{ |
|
179
|
2 |
|
return $this->prefix; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Sets prefix. |
|
184
|
|
|
* |
|
185
|
|
|
* @param string $prefix |
|
186
|
|
|
*/ |
|
187
|
2 |
|
public function setPrefix(string $prefix): void |
|
188
|
|
|
{ |
|
189
|
2 |
|
$this->prefix = $prefix; |
|
190
|
2 |
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @var array $options |
|
194
|
|
|
*/ |
|
195
|
|
|
private $options = []; |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Gets options. |
|
199
|
|
|
* |
|
200
|
|
|
* @return array |
|
201
|
|
|
*/ |
|
202
|
2 |
|
public function getOptions(): array |
|
203
|
|
|
{ |
|
204
|
2 |
|
return $this->options; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Sets options. |
|
209
|
|
|
* |
|
210
|
|
|
* @param array $options |
|
211
|
|
|
*/ |
|
212
|
2 |
|
public function setOptions(array $options): void |
|
213
|
|
|
{ |
|
214
|
2 |
|
$this->options = $options; |
|
215
|
2 |
|
} |
|
216
|
|
|
|
|
217
|
7 |
|
public function init() |
|
218
|
|
|
{ |
|
219
|
7 |
|
foreach (['accessKeyId', 'accessKeySecret', 'bucket', 'endpoint'] as $name) { |
|
220
|
7 |
|
if (empty($this->$name)) { |
|
221
|
4 |
|
throw new InvalidArgumentException($name . ' is required'); |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
3 |
|
parent::init(); |
|
226
|
3 |
|
} |
|
227
|
|
|
|
|
228
|
|
|
private $adapter; |
|
229
|
|
|
|
|
230
|
3 |
|
protected function prepareAdapter() |
|
231
|
|
|
{ |
|
232
|
3 |
|
if ($this->adapter === null) { |
|
233
|
3 |
|
$this->adapter = new AliyunOssAdapter($this->getClient(), $this->bucket, $this->prefix, $this->options); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
3 |
|
return $this->adapter; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
private $client; |
|
240
|
|
|
|
|
241
|
3 |
|
protected function getClient(): OssClient |
|
242
|
|
|
{ |
|
243
|
3 |
|
if ($this->client === null) { |
|
244
|
3 |
|
$this->client = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint, $this->isCName, $this->securityToken); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
3 |
|
return $this->client; |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|