I am trying to get the file size 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 does not 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: inactive Aws \ S3 \ Exception \ NoSuchKeyException: AWS Error code: NoSuchKey, status code: 404, AWS request ID: 4A6F1372301D02F7, AWS Type of error: 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 is thrown in / var / www / vendor / aws / aws -sdk-php / src / Aws / Common / Exception / NamespaceExceptionFactory.php on line 91
So, although I am clearly trying to catch Aws \ S3 \ Exception \ NoSuchKeyException, the system still throws it.
UPDATE:
I found a mistake. The exception must contain a namespace starting with '\' instead of Aws, for example:
try{
$fileSize = filesize("s3://".$bucket."/".$filename);
}catch(\Aws\S3\Exception\NoSuchKeyException $e) {
return false;
}
, , use
'\', , . , - .