9

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?

3 Answers 3

8

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.
Sign up to request clarification or add additional context in comments.

2 Comments

I added the leading slash as it wasn't working without, I'll remove it and update the top question, and double check that's not the problem - I suspect then it might be the permissions problem.
For me the problem was the leading slash, removing it solved it!
3

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

2 Comments

Ah, the bucket location being the problem makes sense.
As a side note you can pass @region into the opts for the call to doesObjectExist itself, which often makes the most sense if you have some sort of reverse lookup function like we do for processing s3 files inter-regionally ie getRegionFromBucket().
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.