Friday 23 October 2015

Establish connection with an Amazon S3 client.using PHP

If PHP S3 client connection is not set correctly, you will get following error:
1) Error retrieving credentials from the instance profile metadata server
2) AWS HTTP error: cURL error 60: SSL certificate problem:

For S3 PHP SDK version 3, the correct connection for S3 client as follows:

 <?php
  require $_SERVER['DOCUMENT_ROOT'] . '/aws/aws-autoloader.php';
  use Aws\S3\S3Client;
  use Aws\S3\Exception\S3Exception;
  use Aws\Common\Credentials\Credentials;


//AWS access info
if (!defined('awsAccessKey')) define('awsAccessKey', 'mykey');
if (!defined('awsSecretKey')) define('awsSecretKey', 'mysecret');


$bucket = 'defashion2u';

try {
$client = S3Client::factory(array(
    'version' => 'latest',
    'region'  => 'us-east-1',
    'credentials' => array(
    'key' => awsAccessKey,
    'secret'  => awsSecretKey
  ),
    'http'    => [
        'verify' => $_SERVER['DOCUMENT_ROOT'] . '/aws/cacert.pem'
    ]
));
} catch (S3Exception $e) {
    echo $e->getMessage() . "\n";
}

?>


The parameter version, region, credentials and SSL cert must be set.
More info on the ca cert, here

No comments:

Post a Comment