8

I'm trying to get the filesize of an S3 object using the Stream API with the following code:

try{
    $fileSize = filesize("s3://".$bucket."/".$filename);
}catch(Aws\S3\Exception\NoSuchKeyException $e) {
    return false;
}

If the key doesn't exist I get the following error:

[Tue Oct 13 23:03:32 2015] [error] [client 54.225.205.152] PHP Warning: File or directory not found: s3://mybucket/myfile.jpg in /var/www/vendor/aws/aws-sdk-php/src/Aws/S3/StreamWrapper.php on line 774

[Tue Oct 13 23:03:32 2015] [error] [client 54.225.205.152] PHP Warning: filesize(): stat failed for s3://mybucket/myfile.jpg in /var/www/api-dev/awsFunc.php on line 278

[Tue Oct 13 23:03:32 2015] [error] [client 54.225.205.152] PHP Fatal error: Uncaught Aws\S3\Exception\NoSuchKeyException: AWS Error Code: NoSuchKey, Status Code: 404, AWS Request ID: 4A6F1372301D02F7, AWS Error Type: client, AWS Error Message: The specified key does not exist., User-Agent: aws-sdk-php2/2.8.21 Guzzle/3.9.3 curl/7.22.0 PHP/5.3.10-1ubuntu3.19\n thrown in /var/www/vendor/aws/aws-sdk-php/src/Aws/Common/Exception/NamespaceExceptionFactory.php on line 91

So, although I explicitly try to catch the Aws\S3\Exception\NoSuchKeyException the system throws it anyway.

UPDATE:

I found the error. The exception should namespace should start with '\' instead of Aws, like that:

try{
    $fileSize = filesize("s3://".$bucket."/".$filename);
}catch(\Aws\S3\Exception\NoSuchKeyException $e) {
    return false;
}

I don't know though why when I use a namespace the namespace doesn't start with '\' but in the exception it needs it. I'd like someone to explain.

Vasilis
  • 2,721
  • 7
  • 33
  • 54
  • Possible duplicate of [Handling errors in AWS PHP SDK 2](http://stackoverflow.com/questions/15918746/handling-errors-in-aws-php-sdk-2) – Mircea Oct 13 '15 at 23:31
  • I don't think it's duplicate, the answers in the above question suggest to use what I'm already using but the exception isn't caught – Vasilis Oct 13 '15 at 23:49

3 Answers3

12

Ok so here is what worked for me:

use Aws\S3\Exception\S3Exception as S3;


try {
    $podcast = $this->uploadFileToS3($request);
} catch(S3 $e) {
    return $e->getMessage();
 }

In my case, i passed the message to a session flash like so:

 return redirect('dashboard/episode/create')->with('status', $e->getMessage());

So it all depends on how you want to use it.

try {
    $fileSize = filesize("s3://".$bucket."/".$filename);
} catch(S3 $e) {
    return $e->getMessage();
 }

Good Luck!

fokosun
  • 548
  • 1
  • 6
  • 18
2

This is the right way.

use Aws\Exception\AwsException;

try{
    // something
}catch(AwsException $e){
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
Maverick
  • 905
  • 8
  • 23
  • Thanks. Do you know why the namespace doesn't require to be declared in the global scope with the leading slash when using `use`, but inside the `catch` it requires the leading slash? – Vasilis Oct 09 '18 at 00:30
  • Maybe you haven't correctly declared the namespace in `use` line. If you did that there's non need for the leading slash in `catch`. My example works without it. Read this [answer](https://stackoverflow.com/questions/4849279/importing-classes-and-namespaces-in-php-what-difference-does-a-leading-backslas) maybe it can help you. ;) – Maverick Oct 09 '18 at 01:23
1

You can make use of AWSException class getAwsErrorMessage() method So you could do

use Aws\S3\Exception\S3Exception;
 try {
     // Your Code       
 } catch (S3Exception $e) {
     return $e->getAwsErrorMessage();
 }
Mojtaba Ahmadi
  • 1,044
  • 19
  • 38