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.
If you're getting Access Denied errors on public read requests that are allowed, check the bucket's Amazon S3 block public access settings. Review the S3 Block Public Access settings at both the account and bucket level. These settings can override permissions that allow public read access.
If you're denied permissions, then use another IAM identity that has bucket access, and edit the bucket policy. Or, delete and recreate the bucket policy if no one has access to it. If you're trying to add a public read policy, then disable the bucket's S3 Block Public Access.
The "403 Forbidden" error can occur due to the following reasons: Permissions are missing for s3:PutObject to add an object or s3:PutObjectAcl to modify the object's ACL. You don't have permission to use an AWS Key Management Service (AWS KMS) key. There is an explicit deny statement in the bucket policy.
If the requested object was available in the S3 bucket for some time and you receive a 404 NoSuchKey error again, then check the following: Confirm that the request matches the object name exactly, including the capitalization of the object name. Requests for S3 objects are case sensitive.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With