/aws

AWS SDK with readable code and async responses

Primary LanguagePHPMIT LicenseMIT

Async AWS client

If you are one of those people that like the Amazon PHP SDK but hate the fact that you need to download Guzzle, PSR-7 and every AWS API client to use it?

This is the library for you!

What is new?

The official AWS PHP SDK is great. It is feature complete, it supports all Amazon APIs and is maintained by some really talented developers. This library is different. It is maintained by some dumb and lazy people.

The goals of this client are:

  1. Async first. Everything is asynchronous and responses are only downloaded if needed.
  2. Not feature complete. We are only covering the handful of services that are used by most people. We are not even covering all of those popular services.
  3. No frequent updates. Updates are great, but if changes are released multiple times every week you cannot keep up-to-date with the changelog. That is especially annoying when the changes are not related to services you use.
  4. Readable code. One should be able to read the code and follow the logic.

and much more...

Installation and usage

All APIs are located in different packages. To install SQS run

composer require async-aws/sqs
use AsyncAws\Sqs\SqsClient;
use AsyncAws\Sqs\Input\SendMessageRequest;

$sqsClient = new SqsClient([
    'region' => 'eu-central-1',
    'accessKeyId' => 'foo',
    'accessKeySecret' => 'bar',
]);

// Call a client's method with an array
$result = $sqsClient->createQueue(['QueueName' => 'bar']);

// Request is automatically sent when reading the result
echo $result->getMessageId();

// You can also call a client's method with an input object
$input = new SendMessageRequest();
$input
    ->setQueueUrl('https://foo.com/bar')
    ->setMessageBody('foobar');

// Since we dont use a $result to get the return value,
// the HTTP request is sent automatically.
$sqsClient->sendMessage($input);

How is it async first?

The secret ingredient in creating asynchronous first is not implemented in this library. It actually comes from the Symfony HTTP client. They have implemented all the cool async features that this AWS library just take advantage of.

So what is this library really doing?

Except for being a wrapper around Symfony's HTTP client and make sure we use the async features properly, we also handle authentication, exceptions and provide some response objects.

Pagination

Some API Results are lists of items, like the result of S3Client::listObjectsV2(). These results implement \IteratorAggregate and will automatically use AWS's pagination API to make a new request to fetch the remaining resources in the list.

use AsyncAws\S3\S3Client;
use AsyncAws\S3\ValueObject\AwsObject;
use AsyncAws\S3\ValueObject\CommonPrefix;

$s3Client = new S3Client();
$result = $s3Client->listObjectsV2(['Bucket' => 'foo']);

/** @var AwsObject|CommonPrefix $file */
foreach($result as $file) {
    if ($file instanceof AwsObject) {
        echo $file->getKey();
    }
}

Waiter

Similar to Official AWS PHP SDK, AsyncAws provides waiters to let you wait until an long operation finished.

use AsyncAws\DynamoDb\DynamoDbClient;

$dbClient = new DynamoDbClient();
$dbClient->createTable([]);

// create a new table. It normally takes around 5 seconds to complete this action.
$dbClient->createTable(['TableName' => 'foobar', /* ...*/]);

$waiter = $dbClient->tableExists(['TableName' => 'foobar']);
echo $waiter->isSuccess(); // false

$waiter->wait();
echo $waiter->isSuccess(); // true

more information about waiters and hasers...

Packages overview

Package name Badges BC check
async-aws/core Latest Stable Version Total Downloads
async-aws/cloud-formation Latest Stable Version Total Downloads
async-aws/dynamo-db Latest Stable Version Total Downloads
async-aws/lambda Latest Stable Version Total Downloads
async-aws/s3 Latest Stable Version Total Downloads
async-aws/ses Latest Stable Version Total Downloads
async-aws/sns Latest Stable Version Total Downloads
async-aws/sqs Latest Stable Version Total Downloads
async-aws/flysystem-s3 Latest Stable Version Total Downloads
async-aws/async-aws-bundle Latest Stable Version Total Downloads

The main repository is not tagged and cannot be installed as a composer package.