1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace diecoding\flysystem; |
4
|
|
|
|
5
|
|
|
use Aws\S3\S3Client; |
6
|
|
|
use League\Flysystem\AwsS3V3\AwsS3V3Adapter; |
7
|
|
|
use yii\base\InvalidConfigException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Interacting with Aws S3 filesystem |
11
|
|
|
* @see https://flysystem.thephpleague.com/docs/adapter/aws-s3-v3/ |
12
|
|
|
* |
13
|
|
|
* ```php |
14
|
|
|
* 'components' => [ |
15
|
|
|
* 'fs' => [ |
16
|
|
|
* 'class' => \diecoding\flysystem\AwsS3Component::class, |
17
|
|
|
* 'endpoint' => 'http://your-endpoint', |
18
|
|
|
* 'key' => 'your-key', |
19
|
|
|
* 'secret' => 'your-secret', |
20
|
|
|
* 'bucket' => 'your-bucket', |
21
|
|
|
* // 'region' => 'us-east-1' |
22
|
|
|
* // 'version' => 'latest', |
23
|
|
|
* // 'usePathStyleEndpoint' => false, |
24
|
|
|
* // 'streamReads' => false, |
25
|
|
|
* // 'options' => [], |
26
|
|
|
* // 'credentials' => [], |
27
|
|
|
* // 'debug' => false, |
28
|
|
|
* // 'prefix' => '', |
29
|
|
|
* ], |
30
|
|
|
* ], |
31
|
|
|
* ``` |
32
|
|
|
* |
33
|
|
|
* @link https://sugengsulistiyawan.my.id/ |
34
|
|
|
* @author Sugeng Sulistiyawan <[email protected]> |
35
|
|
|
* @copyright Copyright (c) 2023 |
36
|
|
|
*/ |
37
|
|
|
class AwsS3Component extends AbstractComponent |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
public $endpoint; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
public $key; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
public $secret; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
public $bucket; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
public $region = 'us-east-1'; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
public $version = 'latest'; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var bool |
71
|
|
|
*/ |
72
|
|
|
public $usePathStyleEndpoint = false; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var bool |
76
|
|
|
*/ |
77
|
|
|
public $streamReads = false; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var array |
81
|
|
|
*/ |
82
|
|
|
public $options = []; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var array |
86
|
|
|
*/ |
87
|
|
|
public $credentials = []; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var S3Client |
91
|
|
|
*/ |
92
|
|
|
protected $client; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @var string[] |
96
|
|
|
*/ |
97
|
|
|
protected $_availableOptions = [ |
98
|
|
|
'endpoint' => 'endpoint', |
99
|
|
|
'use_path_style_endpoint' => 'usePathStyleEndpoint', |
100
|
|
|
'region' => 'region', |
101
|
|
|
'version' => 'version', |
102
|
|
|
'debug' => 'debug', |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritdoc |
107
|
|
|
* @throws InvalidConfigException |
108
|
|
|
*/ |
109
|
|
|
public function init() |
110
|
|
|
{ |
111
|
|
|
$properties = ['bucket']; |
112
|
|
|
if (empty($this->credentials)) { |
113
|
|
|
$properties[] = 'key'; |
114
|
|
|
$properties[] = 'secret'; |
115
|
|
|
} |
116
|
|
|
$this->validateProperties($properties); |
117
|
|
|
|
118
|
|
|
parent::init(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return AwsS3V3Adapter |
123
|
|
|
*/ |
124
|
|
|
protected function initAdapter() |
125
|
|
|
{ |
126
|
|
|
$config = [ |
127
|
|
|
'credentials' => $this->credentials, |
128
|
|
|
]; |
129
|
|
|
|
130
|
|
|
if (empty($config['credentials'])) { |
131
|
|
|
$config['credentials'] = [ |
132
|
|
|
'key' => $this->key, |
133
|
|
|
'secret' => $this->secret, |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
foreach ($this->_availableOptions as $key => $property) { |
138
|
|
|
if ($this->$property !== null) { |
139
|
|
|
$config[$key] = $this->$property; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@see S3Client::__construct}, S3Client accepts the following |
145
|
|
|
* {@see Aws\AwsClient::__construct}, S3Client accepts the following |
146
|
|
|
*/ |
147
|
|
|
$this->client = new S3Client($config); |
148
|
|
|
|
149
|
|
|
return new AwsS3V3Adapter($this->client, $this->bucket, (string) $this->prefix, null, null, $this->options, $this->streamReads); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|