|
27 | 27 | use League\Flysystem\UnableToCheckDirectoryExistence; |
28 | 28 | use League\Flysystem\UnableToCheckFileExistence; |
29 | 29 | use League\Flysystem\UnableToCopyFile; |
| 30 | +use League\Flysystem\UnableToCreateDirectory; |
| 31 | +use League\Flysystem\UnableToDeleteDirectory; |
30 | 32 | use League\Flysystem\UnableToDeleteFile; |
31 | 33 | use League\Flysystem\UnableToGeneratePublicUrl; |
32 | 34 | use League\Flysystem\UnableToGenerateTemporaryUrl; |
| 35 | +use League\Flysystem\UnableToListContents; |
33 | 36 | use League\Flysystem\UnableToMoveFile; |
34 | 37 | use League\Flysystem\UnableToProvideChecksum; |
35 | 38 | use League\Flysystem\UnableToReadFile; |
36 | 39 | use League\Flysystem\UnableToRetrieveMetadata; |
37 | 40 | use League\Flysystem\UnableToSetVisibility; |
| 41 | +use League\Flysystem\UnableToWriteFile; |
38 | 42 | use League\Flysystem\UrlGeneration\PublicUrlGenerator; |
39 | 43 | use League\Flysystem\UrlGeneration\TemporaryUrlGenerator; |
40 | 44 | use League\Flysystem\Visibility; |
@@ -177,32 +181,42 @@ public function deleteDirectory(string $path): void |
177 | 181 |
|
178 | 182 | $objects = []; |
179 | 183 | $params = ['Bucket' => $this->bucket, 'Prefix' => $prefix]; |
180 | | - $result = $this->client->listObjectsV2($params); |
181 | | - /** @var AwsObject $item */ |
182 | | - foreach ($result->getContents() as $item) { |
183 | | - $key = $item->getKey(); |
184 | | - if (null !== $key) { |
185 | | - $objects[] = new ObjectIdentifier(['Key' => $key]); |
| 184 | + |
| 185 | + try { |
| 186 | + $result = $this->client->listObjectsV2($params); |
| 187 | + /** @var AwsObject $item */ |
| 188 | + foreach ($result->getContents() as $item) { |
| 189 | + $key = $item->getKey(); |
| 190 | + if (null !== $key) { |
| 191 | + $objects[] = new ObjectIdentifier(['Key' => $key]); |
| 192 | + } |
186 | 193 | } |
187 | | - } |
188 | 194 |
|
189 | | - if (empty($objects)) { |
190 | | - return; |
191 | | - } |
| 195 | + if (empty($objects)) { |
| 196 | + return; |
| 197 | + } |
192 | 198 |
|
193 | | - foreach (array_chunk($objects, 1000) as $chunk) { |
194 | | - $this->client->deleteObjects([ |
195 | | - 'Bucket' => $this->bucket, |
196 | | - 'Delete' => ['Objects' => $chunk], |
197 | | - ]); |
| 199 | + foreach (array_chunk($objects, 1000) as $chunk) { |
| 200 | + $this->client->deleteObjects([ |
| 201 | + 'Bucket' => $this->bucket, |
| 202 | + 'Delete' => ['Objects' => $chunk], |
| 203 | + ]); |
| 204 | + } |
| 205 | + } catch (\Throwable $e) { |
| 206 | + throw UnableToDeleteDirectory::atLocation($path, $e->getMessage(), $e); |
198 | 207 | } |
199 | 208 | } |
200 | 209 |
|
201 | 210 | public function createDirectory(string $path, Config $config): void |
202 | 211 | { |
203 | 212 | $defaultVisibility = $config->get(Config::OPTION_DIRECTORY_VISIBILITY, $this->visibility->defaultForDirectories()); |
204 | 213 | $config = $config->withDefaults([Config::OPTION_VISIBILITY => $defaultVisibility]); |
205 | | - $this->upload(rtrim($path, '/') . '/', '', $config); |
| 214 | + |
| 215 | + try { |
| 216 | + $this->upload(rtrim($path, '/') . '/', '', $config); |
| 217 | + } catch (Throwable $e) { |
| 218 | + throw UnableToCreateDirectory::dueToFailure($path, $e); |
| 219 | + } |
206 | 220 | } |
207 | 221 |
|
208 | 222 | public function setVisibility(string $path, string $visibility): void |
@@ -292,16 +306,20 @@ public function listContents(string $path, bool $deep): iterable |
292 | 306 | $options['Delimiter'] = '/'; |
293 | 307 | } |
294 | 308 |
|
295 | | - $listing = $this->retrievePaginatedListing($options); |
| 309 | + try { |
| 310 | + $listing = $this->retrievePaginatedListing($options); |
296 | 311 |
|
297 | | - foreach ($listing as $item) { |
298 | | - $item = $this->mapS3ObjectMetadata($item); |
| 312 | + foreach ($listing as $item) { |
| 313 | + $item = $this->mapS3ObjectMetadata($item); |
299 | 314 |
|
300 | | - if ($item->path() === $path) { |
301 | | - continue; |
302 | | - } |
| 315 | + if ($item->path() === $path) { |
| 316 | + continue; |
| 317 | + } |
303 | 318 |
|
304 | | - yield $item; |
| 319 | + yield $item; |
| 320 | + } |
| 321 | + } catch (\Throwable $e) { |
| 322 | + throw UnableToListContents::atLocation($path, $deep, $e); |
305 | 323 | } |
306 | 324 | } |
307 | 325 |
|
@@ -363,16 +381,20 @@ private function upload(string $path, $body, Config $config): void |
363 | 381 | $options['ContentType'] = $mimeType; |
364 | 382 | } |
365 | 383 |
|
366 | | - if ($this->client instanceof SimpleS3Client) { |
367 | | - // Supports upload of files larger than 5GB |
368 | | - $this->client->upload($this->bucket, $key, $body, array_merge($options, ['ACL' => $acl])); |
369 | | - } else { |
370 | | - $this->client->putObject(array_merge($options, [ |
371 | | - 'Bucket' => $this->bucket, |
372 | | - 'Key' => $key, |
373 | | - 'Body' => $body, |
374 | | - 'ACL' => $acl, |
375 | | - ])); |
| 384 | + try { |
| 385 | + if ($this->client instanceof SimpleS3Client) { |
| 386 | + // Supports upload of files larger than 5GB |
| 387 | + $this->client->upload($this->bucket, $key, $body, array_merge($options, ['ACL' => $acl])); |
| 388 | + } else { |
| 389 | + $this->client->putObject(array_merge($options, [ |
| 390 | + 'Bucket' => $this->bucket, |
| 391 | + 'Key' => $key, |
| 392 | + 'Body' => $body, |
| 393 | + 'ACL' => $acl, |
| 394 | + ])); |
| 395 | + } |
| 396 | + } catch (Throwable $exception) { |
| 397 | + throw UnableToWriteFile::atLocation($path, $exception->getMessage(), $exception); |
376 | 398 | } |
377 | 399 | } |
378 | 400 |
|
|
0 commit comments