Home » Php » Amazon S3 SDK PHP doesObjectExist() problems

Amazon S3 SDK PHP doesObjectExist() problems

Posted by: admin July 12, 2020 Leave a comment


I’ve re-written this question to make it clearer, since I’ve updated it.

I’m having trouble with the Amazon AWS S3 PHP SDK. I’m just trying to check if a file exists.
Using this PHP script:

    require INCLUDES_PATH . 'libraries/aws/aws-autoloader.php';

    use Aws\S3\S3Client;

    $client = S3Client::factory(array(
          'key' => AWS_ACCESS_KEY_ID,
          'secret' => AWS_SECRET_KEY

    $key = 'profile/avatar/80745d03-c295-4205-bd82-58161f2fd2d1-320.jpg';
    $result = $client->doesObjectExist( AWS_S3_BUCKET, $key );



This is the output:

string(19) "stage.socialite.app"
string(59) "profile/avatar/80745d03-c295-4205-bd82-58161f2fd2d1-320.jpg"

I know the file exists, it’s here:


This is the IAM policy for the user, whose Key ID and Secret Key I’m using:

  "Version": "2012-10-17",
  "Statement": [
      "Effect": "Allow",
      "Action": [
      "Resource": [

I’ve just created a new Key/Secret pair and added them to my config – what have I done wrong?

How to&Answers:

If var_dump() says bool(false) or bool(true), then it is correctly returning a boolean value. print_r() does not include detailed type information and returns an empty string for false and null values.

So does the object actually exist? Things to check on:

  • S3 keys do not have a leading slashes. According to your code and output above, I suspect this is the problem.
  • Make sure you are providing the actual S3 bucket name and not the CloudFront distribution name.
  • doesObjectExist() returns false if the user does not have read permissions for that object.


In addition to correct bucket location and read permissions:

If using server-side encryption you need to provide the SSE options with doesObjectExist.

$s3Client->doesObjectExist($bucket, $key, array(
    'SSECustomerAlgorithm' => 'AES256',
    'SSECustomerKey'       => $encryptionKey,
    'SSECustomerKeyMD5'    => md5($encryptionKey, true)

Missing or incorrect SSE options will yield false return from doesObjectExist.


I found the answer on another SO post, in a comment from Carlos Castillo:

AWS PHP SDK Version 2 S3 filename encoding issue

He pointed me in the direction of a Github Issue that suggested setting the region when initializing the S3 client, this is because I’m using an S3 instance in Ireland for my dev server, not the default US servers.

So this is the solution:

$client = S3Client::factory(array(
    'key' => AWS_ACCESS_KEY_ID,
    'secret' => AWS_SECRET_KEY,
    'region' => AWS_S3_REGION

Where AWS_S3_REGION is a constant set in my config file, like the Key and Secret.

Credit goes to neilscastle, Carlos and Stack Overflow for it’s excellent SEO