1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException; |
4
|
|
|
use Illuminate\Support\Facades\Response; |
5
|
|
|
use Illuminate\Support\Facades\Storage; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Return either an S3 file url or a local file url |
10
|
|
|
* |
11
|
|
|
* @param $path |
12
|
|
|
* @param bool $temp |
13
|
|
|
* @return mixed |
14
|
|
|
*/ |
15
|
|
|
function fileURL($path, $temp=true) { |
16
|
|
|
if ($temp) { |
17
|
|
|
return Storage::disk('s3')->temporaryUrl($path, now()->addMinutes(60)); |
18
|
|
|
} else { |
19
|
|
|
return Storage::disk('s3')->url($path); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Determine if an S3 file exists |
26
|
|
|
* |
27
|
|
|
* @param string $s3_key |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
|
|
function s3_exists(string $s3_key) { |
31
|
|
|
return Storage::disk('s3')->exists($s3_key); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Upload a file to an S3 bucket |
37
|
|
|
* |
38
|
|
|
* @param $s3_key |
39
|
|
|
* @param $file_path |
40
|
|
|
* @param null $acl |
|
|
|
|
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
|
|
function s3_upload($s3_key, $file_path, $acl=null) { |
44
|
|
|
if(is_null($acl)) { |
|
|
|
|
45
|
|
|
Storage::disk('s3')->put($s3_key, fopen($file_path, 'r+')); |
|
|
|
|
46
|
|
|
} else { |
47
|
|
|
Storage::disk('s3')->put($s3_key, fopen($file_path, 'r+'), $acl); |
48
|
|
|
} |
49
|
|
|
return fileURL($s3_key); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Download a file from an S3 bucket |
55
|
|
|
* |
56
|
|
|
* @param $file_url |
57
|
|
|
* @param null $file_name |
|
|
|
|
58
|
|
|
* @return \Illuminate\Http\Response |
59
|
|
|
* @throws FileNotFoundException |
60
|
|
|
*/ |
61
|
|
|
function s3_download($file_url, $file_name=null) { |
62
|
|
|
if (is_null($file_name)) { |
|
|
|
|
63
|
|
|
$file_name = basename($file_url); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$mime = Storage::disk('s3')->getMimetype($file_url); |
|
|
|
|
67
|
|
|
$size = Storage::disk('s3')->getSize($file_url); |
|
|
|
|
68
|
|
|
|
69
|
|
|
$response = [ |
70
|
|
|
'Content-Type' => $mime, |
71
|
|
|
'Content-Length' => $size, |
72
|
|
|
'Content-Description' => 'File Transfer', |
73
|
|
|
'Content-Disposition' => "attachment; filename={$file_name}", |
74
|
|
|
'Content-Transfer-Encoding' => 'binary', |
75
|
|
|
]; |
76
|
|
|
|
77
|
|
|
return Response::make(Storage::disk('s3')->get($file_url), 200, $response); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Delete a file or folder from an S3 bucket |
83
|
|
|
* |
84
|
|
|
* @param $s3_key |
85
|
|
|
*/ |
86
|
|
|
function s3_delete($s3_key) { |
87
|
|
|
Storage::disk('s3')->delete($s3_key); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Upload raw file contents to an S3 bucket |
93
|
|
|
* |
94
|
|
|
* @param $s3_key |
95
|
|
|
* @param $file_contents |
96
|
|
|
* @param null $acl |
|
|
|
|
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
function s3_upload_raw($s3_key, $file_contents, $acl=null) { |
100
|
|
|
if(is_null($acl)) { |
|
|
|
|
101
|
|
|
Storage::disk('s3')->put($s3_key, $file_contents); |
102
|
|
|
} else { |
103
|
|
|
Storage::disk('s3')->put($s3_key, $file_contents, $acl); |
104
|
|
|
} |
105
|
|
|
return fileURL($s3_key); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* List all of the files in an S3 directory |
111
|
|
|
* |
112
|
|
|
* @param $s3_key |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
function s3_list($s3_key) |
116
|
|
|
{ |
117
|
|
|
$storage = Storage::disk('s3'); |
118
|
|
|
$client = $storage->getAdapter()->getClient(); |
|
|
|
|
119
|
|
|
$command = $client->getCommand('ListObjects'); |
120
|
|
|
$command['Bucket'] = $storage->getAdapter()->getBucket(); |
121
|
|
|
$command['Prefix'] = $s3_key; |
122
|
|
|
$result = $client->execute($command); |
123
|
|
|
|
124
|
|
|
$files = array(); |
125
|
|
|
if (isset($result['Contents']) && !empty($result['Contents'])) { |
126
|
|
|
foreach ($result['Contents'] as $content) { |
127
|
|
|
$url = fileURL($content['Key']); |
128
|
|
|
$parts = explode('/', explode('?', $url, 2)[0]); |
129
|
|
|
$files[] = [ |
130
|
|
|
'name' => end($parts), |
131
|
|
|
'url' => $url, |
132
|
|
|
'key' => $content['Key'] |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
return $files; |
137
|
|
|
} |
138
|
|
|
|