Search code examples
phpamazon-web-servicesamazon-s3pemcacerts

Amazon S3 working in production but not from localhost


I cannot upload to my s3 bucket from localhost despite it working fine in production. No errors are caught during upload but the files never appear (neither pdf nor images). I have no blocking of public access (which appears to be the issue for most people).

I saw one person on here solve their problem by switching on allow_url_fopen in their php settings. Mine was already on.

EDIT: I should note that I have a python script which I run from Windows which uses the bucket without any problem. It makes me think it has something to do with my WAMP server settings but neither my php nor apache logs show anything and the settings look fine.

Any other ideas?

require_once '../../includes/aws/aws-autoloader.php';

$msg = "";

if( isset($_POST['fileUpload']) ) {
    $fileUpload = $_POST['fileUpload'];
    $dirPath = "drives/nota_simple/projects/";
    
    $pdf_parts = explode(";base64,", $fileUpload["pdf"]);
    $pdf_base64 = $pdf_parts[1];

    $s3 = new Aws\S3\S3Client([
        'region'  => 'eu-west-1',
        'version' => '2006-03-01',
        'credentials' => [
            'key'    => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
            'secret' => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        ]
    ]);
    

    $projectFolder = utf8_decode(filter_var($fileUpload['fileProject'], FILTER_SANITIZE_STRING));

    $dirPath .= $projectFolder;
    $dirPath .=  '/' . $fileUpload['fileIdTask'];
    

    $id_image = [taken from DB]
    $key = $dirPath .'/'.$id_image.'.'.$fileUpload['fileExt'];

    try { // Guardamos nota_simple
        $result = $s3->putObject([
            'Bucket' => 'BUCKET_NAME',
            'Key'    => $key,
            'Body'            => base64_decode($pdf_base64),
            'ContentType'     => 'application/' . $fileUpload['fileExt'],
            'ACL'   => 'public-read'
        ]);
    }
    catch(S3Exception $e) {
        echo $e->getMessage() . "\n";
        $msg = "Fallo al subir nota_simple: " . $e->getMessage();
        $response = array("error" => $msg);
    }

    $response = array("success" => "Guardado con suceso.");

Solution

  • The issue was within my localhost server settings:

    AWS HTTP error: cURL error 77: error setting certificate verify locations: CAfile: C:/wamp64/bin/php7.0.33/extras/ssl/cacert.pem

    This was not the correct path.

    I added the correct path to php.ini (as seen below) and restarted the server. Now it works!

    curl.cainfo="c:/wamp64/bin/php/php7.0.33/extras/ssl/cacert.pem" openssl.cafile="c:/wamp64/bin/php/php7.0.33/extras/ssl/cacert.pem"