Getting Started with ECS and PHP (AWS SDK)

This guide will show you how to connect to and ECS instance using PHP and Amazon's AWS SDK and assumes that the SDK has already been installed following Amazon's own installation instructions here: Installation — AWS SDK for PHP documentation

 

Using .aws/credentials File

Amazon recommends using a credential file in your home directory as opposed to hardcoding credentials into the code. This credential file should be located in ~/.aws/credentials and look something like:

 

[default]
aws_access_key_id=YOUR_AWS_ACCESS_KEY
aws_secret_access_key=YOUR_AWS_SECRET_KEY

 

Once this file is in place you can create the connection object like so:


require 'aws/aws-autoloader.php';

$ecs_endpoint="http://your_ecs_endpoint:9020";
  // be sure to include the port number (usually 9020 or 9021)

$s3Client = new \Aws\S3\S3Client([
   'version'    => 'latest',
   'region'     => 'us-west-2',
   'endpoint'   => $ecs_endpoint
    ],
]);

Without using .aws/credentials File

While the credentials file is recommended, it is not required. A connection can be created like so:

 


require 'aws/aws-autoloader.php';

$aws_access_key_id = "your_aws_access_key";
$aws_secret_access_key = "your_secret_key";

s3Client = new \Aws\S3\S3Client([
   'version'     => 'latest',
   'region'      => 'us-west-2',
   'endpoint'    => $ecs_endpoint,
   'credentials' => [
      'key'      => $aws_access_key_id,
      'secret'   => $aws_secret_access_key,
   ],
]);

 

Confirm connection by listing the buckets:


$result = $s3Client->listBuckets();
print ($result);