So I have a block of code to upload an image to an S3 Bucket. It’s fairly boilerplate. Works perfectly running from localhost.
I push it to my Ubuntu EC2 server, and the code fails. No error, no exceptions, Debug => true outputs nothing.. Trying to var_dump the $s3Client variable reports nothing.
- E_ALL Error reporting is on
- PHP 8.1.4
- Nginx 1.21.6
- Compatability-test.php passes successfully – all required modules enabled.
Code:
<?php
require 'S3/aws-autoloader.php';
use AwsS3S3Client;
use AwsS3ExceptionS3Exception;
$bucket = 'XXXXX';
try {
//Create a S3Client
$s3Client = new S3Client([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2006-03-01',
'signature' => 'v4',
'debug' => true,
'credentials' => [
'key' => XXXXX,
'secret' => XXXXX,
]
]);
$result = $s3Client->putObject([
'Bucket' => $bucket,
'Key' => $fileName,
'SourceFile' => $filePath,
'ACL' => 'public-read'
]);
echo json_encode(array('success' => true, 'imageUrl' => $result->get('ObjectURL')));
} catch (S3Exception $exception) {
echo $exception->getMessage() . "n";
}
Has anyone else experienced this ‘quiet failure’ with the AWS PHP-SDK S3?
2
Answers
I solved this, after 3 days. There were several problems, which I managed to solve and inspect by running the code from the PHP CLI.
First things to note:
Why the script failed silently, I really don't know. Maybe some error suppression somewhere in the S3Client class. I highly recommend running from the PHP-CLI to diagnose any errors with the PHP AWS S3 SDK.
I realize this is a bit late, but I experienced a similar issue recently where specifically I did not want to use the credentials file, whereas your answer notes that the credentials must be stored in the credentials file.
In my experience, what wound up working for me was removing the ‘profile’ => ‘default’, in the s3Client creation. So like this …
So if you minimally want to use the inline credentials instead of the credentials file, this may help you out a bit.