1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException; |
4
|
|
|
use Illuminate\Http\Response; |
|
|
|
|
5
|
|
|
use Sfneal\Helpers\Aws\S3\S3; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Return either an S3 file url. |
9
|
|
|
* |
10
|
|
|
* @param string $path |
11
|
|
|
* @param bool $temp |
12
|
|
|
* @param DateTimeInterface|null $expiration |
13
|
|
|
* @return string |
14
|
|
|
*/ |
15
|
|
|
function fileURL(string $path, bool $temp = true, DateTimeInterface $expiration = null): string |
16
|
|
|
{ |
17
|
|
|
// todo: refactor to `fileUrl()` |
18
|
|
|
return (new S3($path))->url($temp, $expiration); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Return either a temporary S3 file url. |
23
|
|
|
* |
24
|
|
|
* @param string $path |
25
|
|
|
* @param bool $temp |
26
|
|
|
* @param DateTimeInterface|null $expiration |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
function fileUrlTemp(string $path, bool $temp = true, DateTimeInterface $expiration = null): string |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
return (new S3($path))->urlTemp($expiration); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Determine if an S3 file exists. |
36
|
|
|
* |
37
|
|
|
* @param string $s3_key |
38
|
|
|
* @return bool |
39
|
|
|
*/ |
40
|
|
|
function s3_exists(string $s3_key): bool |
41
|
|
|
{ |
42
|
|
|
return (new S3($s3_key))->exists(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Upload a file to an S3 bucket. |
47
|
|
|
* |
48
|
|
|
* @param $s3_key |
49
|
|
|
* @param $file_path |
50
|
|
|
* @param null $acl |
|
|
|
|
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
function s3_upload($s3_key, $file_path, $acl = null): string |
54
|
|
|
{ |
55
|
|
|
return (new S3($s3_key))->upload($file_path, $acl); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Download a file from an S3 bucket. |
60
|
|
|
* |
61
|
|
|
* @param $file_url |
62
|
|
|
* @param string|null $file_name |
63
|
|
|
* @return Response |
64
|
|
|
* @throws FileNotFoundException|\League\Flysystem\FileNotFoundException |
65
|
|
|
*/ |
66
|
|
|
function s3_download($file_url, string $file_name = null): Response |
67
|
|
|
{ |
68
|
|
|
return (new S3($file_url))->download($file_name); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Delete a file or folder from an S3 bucket. |
73
|
|
|
* |
74
|
|
|
* @param $s3_key |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
function s3_delete($s3_key): bool |
78
|
|
|
{ |
79
|
|
|
return (new S3($s3_key))->delete(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Upload raw file contents to an S3 bucket. |
84
|
|
|
* |
85
|
|
|
* @param $s3_key |
86
|
|
|
* @param string $file_contents |
87
|
|
|
* @param string|null $acl |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
function s3_upload_raw($s3_key, string $file_contents, string $acl = null): string |
91
|
|
|
{ |
92
|
|
|
return (new S3($s3_key))->upload_raw($file_contents, $acl); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* List all of the files in an S3 directory. |
97
|
|
|
* |
98
|
|
|
* @param $s3_key |
99
|
|
|
* @return array |
100
|
|
|
*/ |
101
|
|
|
function s3_list($s3_key): array |
102
|
|
|
{ |
103
|
|
|
return (new S3($s3_key))->list(); |
104
|
|
|
} |
105
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: