-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathS3Service.php
More file actions
326 lines (293 loc) · 7.63 KB
/
Copy pathS3Service.php
File metadata and controls
326 lines (293 loc) · 7.63 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
declare(strict_types=1);
namespace Bow\Storage\Service;
use Aws\S3\S3Client;
use Bow\Http\UploadedFile;
use Bow\Storage\Contracts\ServiceInterface;
class S3Service implements ServiceInterface
{
/**
* The S3Service instance
*
* @var ?S3Service
*/
private static ?S3Service $instance = null;
/**
* The attribute define the service configuration
*
* @var array
*/
private array $config;
/**
* The attribute define the guzzle http configuration
*
* @var S3Client
*/
private S3Client $client;
/**
* S3Service constructor
*
* @param array $config
* @return void
*/
private function __construct(array $config)
{
$this->config = $config;
$this->client = new S3Client($config);
}
/**
* S3Service Configuration
*
* @param array $config
*
* @return S3Service
*/
public static function configure(array $config): S3Service
{
if (is_null(static::$instance)) {
static::$instance = new S3Service($config);
}
return static::$instance;
}
/**
* Get S3Service
*
* @return S3Service
*/
public static function getInstance(): S3Service
{
return static::$instance;
}
/**
* Function to upload a file
*
* @param UploadedFile $file
* @param string|null $location
* @param array $option
* @return array|bool|string
*/
public function store(UploadedFile $file, ?string $location = null, array $option = []): array|bool|string
{
$putResult = $this->put($file->getHashName(), $file->getContent());
return $putResult ? $this->path($file->getHashName()) : false;
}
/**
* Put other file content in given file
*
* @param string $file
* @param string $content
* @param array $options
*
* @return bool
*/
public function put(string $file, string $content, array $options = []): bool
{
$options = is_string($options)
? ['visibility' => $options]
: (array)$options;
return (bool)$this->client->putObject([
'Bucket' => $this->config['bucket'],
'Key' => $file,
'Body' => $content,
"Visibility" => $options["visibility"] ?? 'public'
]);
}
/**
* Add content after the contents of the file
*
* @param string $file
* @param string $content
* @return bool
*/
public function append(string $file, string $content): bool
{
$result = $this->get($file);
$new_content = $result . PHP_EOL . $content;
$this->put($file, $new_content);
return isset($result["Location"]);
}
/**
* Recover the contents of the file
*
* @param string $file
* @return ?string
*/
public function get(string $file): ?string
{
try {
$this->client->headObject([
'Bucket' => $this->config['bucket'],
'Key' => $file
]);
} catch (\Exception $e) {
return null;
}
$result = $this->client->getObject([
'Bucket' => $this->config['bucket'],
'Key' => $file
]);
if (isset($result["Body"])) {
return $result["Body"]->getContents();
}
return null;
}
/**
* Add content before the contents of the file
*
* @param string $file
* @param string $content
* @return bool
* @throws
*/
public function prepend(string $file, string $content): bool
{
$result = $this->get($file);
$new_content = $content . PHP_EOL . $result;
$this->put($file, $new_content);
return true;
}
/**
* List the files of a folder passed as a parameter
*
* @param string $dirname
* @return array
*/
public function files(string $dirname = '/'): array
{
$result = $this->client->listObjectsV2([
'Bucket' => $this->config['bucket'],
'Prefix' => ltrim($dirname, '/'),
]);
if (!isset($result['Contents'])) {
return [];
}
return array_map(fn($file) => $file['Key'], $result['Contents']);
}
/**
* List the folder of a folder passed as a parameter
*
* @param string $dirname
* @return array
*/
public function directories(string $dirname): array
{
$result = $this->client->listObjectsV2([
'Bucket' => $this->config['bucket'],
'Delimiter' => '/',
'Prefix' => ltrim($dirname, '/'),
]);
if (!isset($result['CommonPrefixes'])) {
return [];
}
return array_map(fn($prefix) => rtrim($prefix['Prefix'], '/'), $result['CommonPrefixes']);
}
/**
* Create a directory
*
* @param string $dirname
* @param int $mode
* @param array $option
* @return bool
*/
public function makeDirectory(string $dirname, int $mode = 0777, array $option = []): bool
{
// S3 does not have real directories, but we can create a placeholder object
$result = $this->client->putObject([
'Bucket' => $this->config['bucket'],
'Key' => rtrim($dirname, '/') . '/',
'Body' => '',
]);
return isset($result['ObjectURL']) || isset($result['ETag']);
}
/**
* Renames or moves a source file to a target file.
*
* @param string $source
* @param string $target
* @return bool
*/
public function move(string $source, string $target): bool
{
$copied = $this->copy($source, $target);
if ($copied) {
return $this->delete($source);
}
return false;
}
/**
* Copy the contents of a source file to a target file.
*
* @param string $source
* @param string $target
* @return bool
*/
public function copy(string $source, string $target): bool
{
try {
$this->client->copyObject([
'Bucket' => $this->config['bucket'],
'CopySource' => $this->config['bucket'] . '/' . $source,
'Key' => $target,
]);
return true;
} catch (\Exception $e) {
return false;
}
}
/**
* Delete file or directory
*
* @param string $file
* @return bool
*/
public function delete(string $file): bool
{
return (bool) $this->client->deleteObject([
'Bucket' => $this->config['bucket'],
'Key' => $file
]);
}
/**
* Check the existence of a file
*
* @param string $file
* @return bool
*/
public function exists(string $file): bool
{
return (bool) $this->get($file);
}
/**
* isFile alias of is_file.
*
* @param string $file
* @return bool
*/
public function isFile(string $file): bool
{
$result = $this->get($file);
return $result !== null && $result !== false;
}
/**
* isDirectory alias of is_dir.
*
* @param string $dirname
* @return bool
*/
public function isDirectory(string $dirname): bool
{
$result = $this->files($dirname);
return is_array($result) && count($result) > 0;
}
/**
* Resolves file path.
* Give the absolute path of a path
*
* @param string $file
* @return string
*/
public function path(string $file): string
{
return $this->client->getObjectUrl($this->config["bucket"], $file);
}
}