|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: onysko |
|
5
|
|
|
* Date: 17.11.2014 |
|
6
|
|
|
* Time: 14:12 |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace samsonphp\fs; |
|
9
|
|
|
|
|
10
|
|
|
use Aws\S3\S3Client; |
|
11
|
|
|
use Aws\Common\Credentials\Credentials; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Amazon Web Services Adapter implementation |
|
15
|
|
|
* @package samson\upload |
|
16
|
|
|
*/ |
|
17
|
|
|
class AWSFileService extends AbstractFileService |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var S3Client $client Aws services user */ |
|
20
|
|
|
protected $client; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string $bucket Aws bucket name */ |
|
23
|
|
|
public $bucket; |
|
24
|
|
|
|
|
25
|
|
|
/** @var string $accessKey */ |
|
26
|
|
|
public $accessKey; |
|
27
|
|
|
|
|
28
|
|
|
/** @var string $secretKey */ |
|
29
|
|
|
public $secretKey; |
|
30
|
|
|
|
|
31
|
|
|
/** @var string $bucketURL Url of amazon bucket */ |
|
32
|
|
|
public $bucketURL; |
|
33
|
|
|
|
|
34
|
|
|
/** @var string $region Region of amazon S3 service */ |
|
35
|
|
|
public $region; |
|
36
|
|
|
|
|
37
|
|
|
/** @var string Signature */ |
|
38
|
|
|
public $signature = 'v4'; |
|
39
|
|
|
|
|
40
|
|
|
/** @var int Resource caching age */ |
|
41
|
|
|
public $maxAge = 1296000; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param null $client AWSClient Object |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct($client = null) |
|
47
|
|
|
{ |
|
48
|
|
|
// Get client object instance from input parameters |
|
49
|
|
|
$this->client = & $client; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Initialization stage |
|
54
|
|
|
*/ |
|
55
|
|
|
public function initialize() |
|
56
|
|
|
{ |
|
57
|
|
|
// No client is passed |
|
58
|
|
|
if (!isset($this->client)) { |
|
59
|
|
|
// Use S3 clients, create authorization object and instantiate the S3 client with AWS credentials |
|
60
|
|
|
$this->client = S3Client::factory(array( |
|
61
|
|
|
'credentials' => new Credentials($this->accessKey, $this->secretKey), |
|
62
|
|
|
'region' => $this->region, |
|
63
|
|
|
'signature' => $this->signature |
|
64
|
|
|
)); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Write data to a specific relative location |
|
70
|
|
|
* |
|
71
|
|
|
* @param mixed $data Data to be written |
|
72
|
|
|
* @param string $filename File name |
|
73
|
|
|
* @param string $uploadDir Relative file path |
|
74
|
|
|
* @return string Relative path to created file, false if there were errors |
|
75
|
|
|
*/ |
|
76
|
|
|
public function write($data, $filename = '', $uploadDir = '') |
|
77
|
|
|
{ |
|
78
|
|
|
// Upload data to Amazon S3 |
|
79
|
|
|
$this->client->putObject(array( |
|
80
|
|
|
'Bucket' => $this->bucket, |
|
81
|
|
|
'Key' => $uploadDir.'/'.$filename, |
|
82
|
|
|
'Body' => $data, |
|
83
|
|
|
'CacheControl' => 'max-age='.$this->maxAge, |
|
84
|
|
|
'ACL' => 'public-read' |
|
85
|
|
|
)); |
|
86
|
|
|
|
|
87
|
|
|
// Build absolute path to uploaded resource |
|
88
|
|
|
return $this->bucketURL.'/'.(isset($uploadDir{0}) ? $uploadDir . '/' : ''); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Check existing current file in current file system |
|
93
|
|
|
* @param $url string Url |
|
94
|
|
|
* @return boolean File exists or not |
|
95
|
|
|
*/ |
|
96
|
|
|
public function exists($url) |
|
97
|
|
|
{ |
|
98
|
|
|
// Get file key name on amazon s3 |
|
99
|
|
|
$fileKey = str_replace($this->bucketURL, '', $url); |
|
100
|
|
|
return $this->client->doesObjectExist($this->bucket, $fileKey); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Read the file from current file system |
|
105
|
|
|
* @param $filePath string Full path to file |
|
106
|
|
|
* @param $filename string File name |
|
107
|
|
|
* @return string File data |
|
108
|
|
|
*/ |
|
109
|
|
|
public function read($filePath, $filename = null) |
|
110
|
|
|
{ |
|
111
|
|
|
return file_get_contents($filePath); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Delete file from current file system |
|
116
|
|
|
* @param $filePath string File for deleting |
|
117
|
|
|
* @return mixed |
|
118
|
|
|
*/ |
|
119
|
|
|
public function delete($filePath) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->client->deleteObject(array( |
|
122
|
|
|
'Bucket' => $this->bucket, |
|
123
|
|
|
'Key' => str_replace($this->bucketURL, '', $filePath) |
|
124
|
|
|
)); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Get file extension in current file system |
|
129
|
|
|
* @param $filePath string Path |
|
130
|
|
|
* @return string false if extension not found, otherwise file extension |
|
131
|
|
|
*/ |
|
132
|
|
|
public function extension($filePath) |
|
133
|
|
|
{ |
|
134
|
|
|
// Get last three symbols of file path |
|
135
|
|
|
$extension = substr($filePath, -3); |
|
136
|
|
|
|
|
137
|
|
|
// Fix jpeg extension |
|
138
|
|
|
if ($extension == 'peg') { |
|
139
|
|
|
$extension = 'jpeg'; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $extension; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Define if $filePath is directory |
|
147
|
|
|
* @param string $filePath Path |
|
148
|
|
|
* @return boolean Is $path a directory or not |
|
149
|
|
|
*/ |
|
150
|
|
|
public function isDir($filePath) |
|
151
|
|
|
{ |
|
152
|
|
|
$isDir = false; |
|
153
|
|
|
if ($this->exists($filePath)) { |
|
154
|
|
|
$isDir = $this->isKeyDir($filePath); |
|
155
|
|
|
} |
|
156
|
|
|
return $isDir; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Get recursive $path listing collection |
|
161
|
|
|
* @param string $path Path for listing contents |
|
162
|
|
|
* @param array $restrict Collection of restricted paths |
|
163
|
|
|
* @param array $result Collection of restricted paths |
|
164
|
|
|
* @return array $result Resulting collection used in recursion |
|
165
|
|
|
*/ |
|
166
|
|
|
public function dir($path, $restrict = array(), & $result = array()) |
|
167
|
|
|
{ |
|
168
|
|
|
$iterator = $this->client->getIterator('ListObjects', array( |
|
169
|
|
|
'Bucket' => $this->bucket, |
|
170
|
|
|
'Prefix' => $path |
|
171
|
|
|
)); |
|
172
|
|
|
|
|
173
|
|
|
foreach ($iterator as $object) { |
|
174
|
|
|
$key = $object['Key']; |
|
175
|
|
|
if (!$this->isKeyDir($key)) { |
|
176
|
|
|
$result[] = $key; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
// Sort results |
|
181
|
|
|
if (sizeof($result)) { |
|
182
|
|
|
sort($result); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return $result; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Define if $objectKey is directory |
|
190
|
|
|
* @param $objectKey Object Key |
|
191
|
|
|
* @return bool Is $objectKey a directory or not |
|
192
|
|
|
*/ |
|
193
|
|
|
private function isKeyDir($objectKey) |
|
194
|
|
|
{ |
|
195
|
|
|
$isDir = false; |
|
196
|
|
|
$fileKey = preg_replace('/.*'.quotemeta($this->bucket).'\//', '', $objectKey); |
|
197
|
|
|
if (($last = substr($fileKey, -1)) === '/') { |
|
198
|
|
|
$isDir = true; |
|
199
|
|
|
} |
|
200
|
|
|
return $isDir; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Create catalog in selected location |
|
205
|
|
|
* @param string $path Path for new catalog |
|
206
|
|
|
* @return boolean Result of catalog creating |
|
207
|
|
|
*/ |
|
208
|
|
|
public function mkDir($path) |
|
209
|
|
|
{ |
|
210
|
|
|
// We cannot create folders directly at AWS |
|
211
|
|
|
return true; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|