|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.tintsoft.com/ |
|
4
|
|
|
* @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd. |
|
5
|
|
|
* @license http://www.tintsoft.com/license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace yuncms\filesystem\adapters; |
|
9
|
|
|
|
|
10
|
|
|
use Aws\S3\S3Client; |
|
11
|
|
|
use Yii; |
|
12
|
|
|
use yii\base\InvalidConfigException; |
|
13
|
|
|
use yuncms\filesystem\Adapter; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class AwsS3Adapter |
|
17
|
|
|
* |
|
18
|
|
|
* @author Tongle Xu <[email protected]> |
|
19
|
|
|
* @since 3.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
class AwsS3Adapter extends Adapter |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
public $key; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
public $secret; |
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
public $region; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
public $baseUrl; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
public $version; |
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
public $bucket; |
|
47
|
|
|
/** |
|
48
|
|
|
* @var string|null |
|
49
|
|
|
*/ |
|
50
|
|
|
public $prefix; |
|
51
|
|
|
/** |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
public $options = []; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @inheritdoc |
|
58
|
|
|
*/ |
|
59
|
|
|
public function init() |
|
60
|
|
|
{ |
|
61
|
|
|
if ($this->key === null) { |
|
62
|
|
|
throw new InvalidConfigException('The "key" property must be set.'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if ($this->secret === null) { |
|
66
|
|
|
throw new InvalidConfigException('The "secret" property must be set.'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if ($this->bucket === null) { |
|
70
|
|
|
throw new InvalidConfigException('The "bucket" property must be set.'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
parent::init(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @inheritdoc |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function displayName(): string |
|
80
|
|
|
{ |
|
81
|
|
|
return Yii::t('yuncms', 'Aws S3'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return \League\Flysystem\AwsS3v3\AwsS3Adapter |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function prepareAdapter() |
|
88
|
|
|
{ |
|
89
|
|
|
$config = [ |
|
90
|
|
|
'credentials' => [ |
|
91
|
|
|
'key' => $this->key, |
|
92
|
|
|
'secret' => $this->secret |
|
93
|
|
|
] |
|
94
|
|
|
]; |
|
95
|
|
|
|
|
96
|
|
|
if ($this->region !== null) { |
|
97
|
|
|
$config['region'] = $this->region; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if ($this->baseUrl !== null) { |
|
101
|
|
|
$config['base_url'] = $this->baseUrl; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$config['version'] = (($this->version !== null) ? $this->version : 'latest'); |
|
105
|
|
|
|
|
106
|
|
|
$client = new S3Client($config); |
|
107
|
|
|
|
|
108
|
|
|
return new \League\Flysystem\AwsS3v3\AwsS3Adapter($client, $this->bucket, $this->prefix, $this->options); |
|
109
|
|
|
} |
|
110
|
|
|
} |