skip to Main Content

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


  1. Chosen as BEST ANSWER

    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:

    1. Credentials must be stored in the /var/www/.aws/credentials file. This is where the S3Client class will look for your S3 credentials. Passing them into the S3 class instantiation does not appear to work.
    2. Running from the CLI, indicated that I was missing the PHP module mbstring. I don't know why Amazon's compatibility-test.php file does not flag this module as missing. Go figure.

    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.


  2. 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 …

    //Create a S3Client
    $s3Client = new S3Client([
        'region' => 'us-east-1',
        'version' => '2006-03-01',
        'signature' => 'v4',
        'debug' => true,
        'credentials' => [
            'key'    => XXXXX,
            'secret' => XXXXX,
        ]
    ]);
    

    So if you minimally want to use the inline credentials instead of the credentials file, this may help you out a bit.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search