Capturing errors with put and s3 object

I use S3 php sdk and a liver implementation of this to put an object on S3.

try {
    $s3->putObject(array(
    'Bucket' => 'mybucket', 
    'Key' =>  'abc',
    'Body' => $img->encode(null, 90),
    'ACL' => 'public-read',
    'ContentType' => $img->mime()
));

}
catch (S3Exception $e) {

    var_dump('error');
    die();
}

But the code above does not seem to cause errors. I installed the wrong bucket and I still get the error:

type:Aws\S3\Exception\NoSuchBucketException, message:The specified bucket does not exist,โ€ฆ

How can I catch this error and act on it accordingly.

+4
source share
1 answer

Use the correct parent class

To catch all S3 exceptions, be sure to refer to the correct namespace:

catch (\Aws\S3\Exception\S3Exception $e) {

or

<?php
use Aws\S3\Exception\S3Exception;

...

catch (S3Exception $e) {

Otherwise, the catch block will not catch exceptions thrown by the S3 library.

+6
source

Source: https://habr.com/ru/post/1545552/


All Articles