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?
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.
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.
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.
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:
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
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