@@ -80,9 +80,9 @@ public static function getInstance(): S3Service
8080 */
8181 public function store (UploadedFile $ file , ?string $ location = null , array $ option = []): array |bool |string
8282 {
83- $ result = $ this ->put ($ file ->getHashName (), $ file ->getContent ());
83+ $ putResult = $ this ->put ($ file ->getHashName (), $ file ->getContent ());
8484
85- return $ result [ " Location " ] ;
85+ return $ putResult ? $ this -> path ( $ file -> getHashName ()) : false ;
8686 }
8787
8888 /**
@@ -100,14 +100,12 @@ public function put(string $file, string $content, array $options = []): bool
100100 ? ['visibility ' => $ options ]
101101 : (array )$ options ;
102102
103- return (bool )$ this ->client ->putObject (
104- [
103+ return (bool )$ this ->client ->putObject ([
105104 'Bucket ' => $ this ->config ['bucket ' ],
106105 'Key ' => $ file ,
107106 'Body ' => $ content ,
108107 "Visibility " => $ options ["visibility " ] ?? 'public '
109- ]
110- );
108+ ]);
111109 }
112110
113111 /**
@@ -134,12 +132,19 @@ public function append(string $file, string $content): bool
134132 */
135133 public function get (string $ file ): ?string
136134 {
137- $ result = $ this ->client ->getObject (
138- [
135+ try {
136+ $ this ->client ->headObject ([
137+ 'Bucket ' => $ this ->config ['bucket ' ],
138+ 'Key ' => $ file
139+ ]);
140+ } catch (\Exception $ e ) {
141+ return null ;
142+ }
143+
144+ $ result = $ this ->client ->getObject ([
139145 'Bucket ' => $ this ->config ['bucket ' ],
140146 'Key ' => $ file
141- ]
142- );
147+ ]);
143148
144149 if (isset ($ result ["Body " ])) {
145150 return $ result ["Body " ]->getContents ();
@@ -171,15 +176,16 @@ public function prepend(string $file, string $content): bool
171176 * @param string $dirname
172177 * @return array
173178 */
174- public function files (string $ dirname ): array
179+ public function files (string $ dirname = ' / ' ): array
175180 {
176- $ result = $ this ->client ->listObjects (
177- [
178- "Bucket " => $ dirname
179- ]
180- );
181-
182- return array_map (fn ($ file ) => $ file ["Key " ], $ result ["Contents " ]);
181+ $ result = $ this ->client ->listObjectsV2 ([
182+ 'Bucket ' => $ this ->config ['bucket ' ],
183+ 'Prefix ' => ltrim ($ dirname , '/ ' ),
184+ ]);
185+ if (!isset ($ result ['Contents ' ])) {
186+ return [];
187+ }
188+ return array_map (fn ($ file ) => $ file ['Key ' ], $ result ['Contents ' ]);
183189 }
184190
185191 /**
@@ -190,9 +196,15 @@ public function files(string $dirname): array
190196 */
191197 public function directories (string $ dirname ): array
192198 {
193- $ buckets = (array )$ this ->client ->listBuckets ();
194-
195- return array_map (fn ($ bucket ) => $ bucket ["Name " ], $ buckets );
199+ $ result = $ this ->client ->listObjectsV2 ([
200+ 'Bucket ' => $ this ->config ['bucket ' ],
201+ 'Delimiter ' => '/ ' ,
202+ 'Prefix ' => ltrim ($ dirname , '/ ' ),
203+ ]);
204+ if (!isset ($ result ['CommonPrefixes ' ])) {
205+ return [];
206+ }
207+ return array_map (fn ($ prefix ) => rtrim ($ prefix ['Prefix ' ], '/ ' ), $ result ['CommonPrefixes ' ]);
196208 }
197209
198210 /**
@@ -205,13 +217,13 @@ public function directories(string $dirname): array
205217 */
206218 public function makeDirectory (string $ dirname , int $ mode = 0777 , array $ option = []): bool
207219 {
208- $ result = $ this -> client -> createBucket (
209- [
210- " Bucket " => $ dirname
211- ]
212- );
213-
214- return isset ($ result [" Location " ]);
220+ // S3 does not have real directories, but we can create a placeholder object
221+ $ result = $ this -> client -> putObject ( [
222+ ' Bucket ' => $ this -> config [ ' bucket ' ],
223+ ' Key ' => rtrim ( $ dirname , ' / ' ) . ' / ' ,
224+ ' Body ' => '' ,
225+ ]);
226+ return isset ($ result [' ObjectURL ' ]) || isset ( $ result [ ' ETag ' ]);
215227 }
216228
217229 /**
@@ -223,11 +235,11 @@ public function makeDirectory(string $dirname, int $mode = 0777, array $option =
223235 */
224236 public function move (string $ source , string $ target ): bool
225237 {
226- $ this ->copy ($ source , $ target );
227-
228- $ this ->delete ($ source );
229-
230- return true ;
238+ $ copied = $ this ->copy ($ source , $ target );
239+ if ( $ copied ) {
240+ return $ this ->delete ($ source );
241+ }
242+ return false ;
231243 }
232244
233245 /**
@@ -239,15 +251,16 @@ public function move(string $source, string $target): bool
239251 */
240252 public function copy (string $ source , string $ target ): bool
241253 {
242- $ content = $ this ->get ($ source );
243-
244- if ($ content === null ) {
254+ try {
255+ $ this ->client ->copyObject ([
256+ 'Bucket ' => $ this ->config ['bucket ' ],
257+ 'CopySource ' => $ this ->config ['bucket ' ] . '/ ' . $ source ,
258+ 'Key ' => $ target ,
259+ ]);
260+ return true ;
261+ } catch (\Exception $ e ) {
245262 return false ;
246263 }
247-
248- $ this ->put ($ target , $ content );
249-
250- return true ;
251264 }
252265
253266 /**
@@ -258,12 +271,10 @@ public function copy(string $source, string $target): bool
258271 */
259272 public function delete (string $ file ): bool
260273 {
261- return (bool )$ this ->client ->deleteObject (
262- [
274+ return (bool ) $ this ->client ->deleteObject ([
263275 'Bucket ' => $ this ->config ['bucket ' ],
264276 'Key ' => $ file
265- ]
266- );
277+ ]);
267278 }
268279
269280 /**
@@ -274,7 +285,7 @@ public function delete(string $file): bool
274285 */
275286 public function exists (string $ file ): bool
276287 {
277- return (bool )$ this ->get ($ file );
288+ return (bool ) $ this ->get ($ file );
278289 }
279290
280291 /**
@@ -286,8 +297,7 @@ public function exists(string $file): bool
286297 public function isFile (string $ file ): bool
287298 {
288299 $ result = $ this ->get ($ file );
289-
290- return strlen ($ result ) > -1 ;
300+ return $ result !== null && $ result !== false ;
291301 }
292302
293303 /**
@@ -298,9 +308,8 @@ public function isFile(string $file): bool
298308 */
299309 public function isDirectory (string $ dirname ): bool
300310 {
301- $ result = $ this ->get ($ dirname );
302-
303- return isset ($ result ["Location " ]);
311+ $ result = $ this ->files ($ dirname );
312+ return is_array ($ result ) && count ($ result ) > 0 ;
304313 }
305314
306315 /**
0 commit comments