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

Amazon S3 SDK PHP doesObjectExist() problems

Posted by: admin July 12, 2020 Leave a comment

Questions:

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:

<?php
    require_once("../../../configs/config.".get_current_user().".php");
    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 );

    var_dump(AWS_S3_BUCKET);
    var_dump($key);
    var_dump($result);

?>

This is the output:

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

I know the file exists, it’s here:

http://stage.socialite.app.s3.amazonaws.com/profile/avatar/80745d03-c295-4205-bd82-58161f2fd2d1-320.jpg

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": [
          "s3:*"
      ],
      "Resource": [
        "arn:aws:s3:::stage.socialite.app/*",
        "arn:aws:s3:::stage.socialite.app"
      ]
    }
  ]
}

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.

Answer:

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.

Answer:

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