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