Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon S3 SDK PHP doesObjectExist() problems

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?

like image 361
Pete Avatar asked Apr 02 '14 08:04

Pete


People also ask

Does PHP work with AWS?

Get started quickly using AWS with the AWS SDK for PHP. The SDK is a modern, open-source PHP library that makes it easy to integrate your PHP application with AWS services like Amazon S3, Amazon Glacier, and Amazon DynamoDB.

How do I show an image from my Amazon S3 on my website?

Easiest thing to do is make them public in s3, at least read-only. If you don't want them to be public on s3, for whatever reason, you could add a cloudfront distribution that will serve the images from your s3 bucket, and you can give cloudfront access to the files, without making the images public in s3.

What is Getobject in AWS S3?

PDF. Retrieves objects from Amazon S3. To use GET , you must have READ access to the object. If you grant READ access to the anonymous user, you can return the object without using an authorization header.


3 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.
like image 147
Jeremy Lindblom Avatar answered Oct 20 '22 13:10

Jeremy Lindblom


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.

like image 44
fsimon Avatar answered Oct 20 '22 14:10

fsimon


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

like image 2
Pete Avatar answered Oct 20 '22 13:10

Pete