|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException; |
|
4
|
|
|
use Illuminate\Http\Response; |
|
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Storage; |
|
6
|
|
|
use Sfneal\Helpers\Aws\S3\Utils\S3; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Return either an S3 file url. |
|
10
|
|
|
* |
|
11
|
|
|
* @param string $path |
|
12
|
|
|
* @param bool $temp |
|
13
|
|
|
* @param DateTimeInterface|null $expiration |
|
14
|
|
|
* @return string |
|
15
|
|
|
*/ |
|
16
|
|
|
function fileURL(string $path, bool $temp = true, DateTimeInterface $expiration = null): string |
|
17
|
|
|
{ |
|
18
|
|
|
// todo: refactor to `fileUrl()` |
|
19
|
|
|
if ($temp) { |
|
20
|
|
|
return (new S3($path))->urlTemp($expiration); |
|
21
|
|
|
} else { |
|
22
|
|
|
return (new S3($path))->url(); |
|
23
|
|
|
} |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Return either a temporary S3 file url. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $path |
|
30
|
|
|
* @param DateTimeInterface|null $expiration |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
|
|
function fileUrlTemp(string $path, DateTimeInterface $expiration = null): string |
|
34
|
|
|
{ |
|
35
|
|
|
return (new S3($path))->urlTemp($expiration); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Determine if an S3 file exists. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $s3_key |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
|
|
function s3_exists(string $s3_key): bool |
|
45
|
|
|
{ |
|
46
|
|
|
return Storage::disk('s3')->exists($s3_key); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Upload a file to an S3 bucket. |
|
51
|
|
|
* |
|
52
|
|
|
* @param $s3_key |
|
53
|
|
|
* @param $file_path |
|
54
|
|
|
* @param null $acl |
|
|
|
|
|
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
function s3_upload($s3_key, $file_path, $acl = null): string |
|
58
|
|
|
{ |
|
59
|
|
|
return (new S3($s3_key))->upload($file_path, $acl); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Download a file from an S3 bucket. |
|
64
|
|
|
* |
|
65
|
|
|
* @param $file_url |
|
66
|
|
|
* @param string|null $file_name |
|
67
|
|
|
* @return Response |
|
68
|
|
|
* @throws FileNotFoundException|\League\Flysystem\FileNotFoundException |
|
69
|
|
|
*/ |
|
70
|
|
|
function s3_download($file_url, string $file_name = null): Response |
|
71
|
|
|
{ |
|
72
|
|
|
return (new S3($file_url))->download($file_name); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Upload raw file contents to an S3 bucket. |
|
77
|
|
|
* |
|
78
|
|
|
* @param $s3_key |
|
79
|
|
|
* @param string $file_contents |
|
80
|
|
|
* @param string|null $acl |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
function s3_upload_raw($s3_key, string $file_contents, string $acl = null): string |
|
84
|
|
|
{ |
|
85
|
|
|
return (new S3($s3_key))->upload_raw($file_contents, $acl); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* List all of the files in an S3 directory. |
|
90
|
|
|
* |
|
91
|
|
|
* @param $s3_key |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
function s3_list($s3_key): array |
|
95
|
|
|
{ |
|
96
|
|
|
return (new S3($s3_key))->list(); |
|
97
|
|
|
} |
|
98
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: