Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using Version 3 of the AWS SDK for PHP

Using Version 3 of the AWS SDK for PHP

Amazon Web Services (AWS) offers a broad set of global compute, storage, database, analytics, application, and deployment services that can help PHP developers build scalable applications in the cloud. These services provide APIs that allow you to control all of your resources programmatically, even through your PHP code. Let's talk about how to use Version 3 of the open source AWS SDK for PHP (built on the Guzzle library and PSR-7) to control your AWS resources and use the AWS services from within your applications.

Jeremy Lindblom

September 10, 2015
Tweet

More Decks by Jeremy Lindblom

Other Decks in Programming

Transcript

  1. By Jeremy Lindblom (@jeremeamia)

    View Slide

  2. Jeremy Lindblom (@jeremeamia)

    View Slide

  3. View Slide

  4. $ec2 =
    'region' => 'us-east-1'
    'version' => '2014-06-15'
    $ec2->runInstances
    'ImageId' => 'ami-6a6dcc02'
    'MinCount' =>
    'MaxCount' =>
    'InstanceType' => 'm1.small'

    View Slide

  5. View Slide

  6. View Slide

  7. View Slide

  8. $ec2 = ::factory
    'region' => 'us-east-1'
    $ec2->runInstances
    'ImageId' => 'ami-6a6dcc02'
    'MinCount' =>
    'MaxCount' =>
    'InstanceType' => 'm1.small'

    View Slide

  9. $ec2 = ::factory
    'region' => 'us-east-1'
    $ec2->runInstances
    'ImageId' => 'ami-6a6dcc02'
    'MinCount' =>
    'MaxCount' =>
    'InstanceType' => 'm1.small'
    'version' => '2014-06-15'

    View Slide

  10. $ec2 = ::factory
    'region' => 'us-east-1'
    $ec2->runInstances
    'ImageId' => 'ami-6a6dcc02'
    'MinCount' =>
    'MaxCount' =>
    'InstanceType' => 'm1.small'

    View Slide

  11. $ec2 = ::factory
    'region' => 'us-east-1'
    $ec2->runInstances
    'ImageId' => 'ami-6a6dcc02'
    'MinCount' =>
    'MaxCount' =>
    'InstanceType' => 'm1.small'
    'version' => '2014-06-15'

    View Slide

  12. $ec2 = ::factory
    'region' => 'us-east-1'
    $ec2->runInstances
    'ImageId' => 'ami-6a6dcc02'
    'MinCount' =>
    'MaxCount' =>
    'InstanceType' => 'm1.small'
    'version' => '2014-06-15'

    View Slide

  13. •  Async requests with a Promise API
    •  Support for custom HTTP adapters
    –  cURL no longer required (still the default)
    –  Possible to implement with non-blocking event loops
    •  Result "Paginators" for iterating paginated data
    •  JMESPath querying of result data
    •  "debug" client option for easy debugging

    View Slide

  14. View Slide

  15. PHP PHP
    #nofilter
    #selphpie
    #instagood

    View Slide

  16. PHP PHP

    View Slide

  17. (Storage of selPHPies)
    (Storage of URLs/captions)
    PHP
    PHP

    View Slide

  18. View Slide

  19. "require": {
    "aws/aws-sdk-php": "~3.0",
    "silex/silex": "~1.2",
    "twig/twig": "~1.16",
    }
    getcomposer.org

    View Slide

  20. •  Instance profile credentials
    •  Credentials file
    •  Environment variables
    •  Hard coding

    View Slide

  21. •  Instance profile credentials
    •  Credentials file
    •  Environment variables
    •  Client configuration

    View Slide

  22. •  Instance profile credentials
    •  Credentials file
    •  Environment variables
    •  Client configuration
    FYI: Also supported by the
    AWS CLI and other SDKs.

    View Slide

  23. •  Instance profile credentials
    •  Credentials file
    •  Environment variables
    •  Client configuration

    View Slide

  24. •  Instance profile credentials
    •  Credentials file
    •  Environment variables
    •  Client Configuration (BEWARE)
    'credentials' =>
    'key' => $yourAccessKeyId
    'secret' => $yourSecretAccessKey

    View Slide

  25. View Slide

  26. php bin/setup.php
    S3 bucket
    DynamoDB table
    Amazon S3
    Amazon
    DynamoDB

    View Slide

  27. $s3->createBucket 'Bucket' => $bucket
    $s3->waitUntil 'BucketExists' 'Bucket' => $bucket
    $dynamoDb->createTable(['TableName' => $table ...
    $dynamoDb->waitUntil 'TableExists'
    'TableName' => $table
    echo "Done.\n"

    View Slide

  28. $s3->createBucket 'Bucket' => $bucket
    $s3->waitUntil 'BucketExists' 'Bucket' => $bucket
    $dynamoDb->createTable(['TableName' => $table ...
    $dynamoDb->waitUntil 'TableExists'
    'TableName' => $table
    echo "Done.\n"

    View Slide

  29. View Slide

  30. $s3->createBucket 'Bucket' => $bucket
    $s3->waitUntil 'BucketExists' 'Bucket' => $bucket
    $dynamoDb->createTable(['TableName' => $table ...
    $dynamoDb->waitUntil 'TableExists'
    'TableName' => $table
    echo "Done.\n"

    View Slide

  31. View Slide

  32. View Slide

  33. View Slide

  34. $app = new
    $app 'aws' = function
    return new
    'region' => 'us-east-1'
    'version' => 'latest'
    // ROUTES AND OTHER APPLICATION LOGIC
    $app->run

    View Slide

  35. $app = new
    $app 'aws' = function
    return new
    'region' => 'us-east-1'
    'version' => 'latest'
    // ROUTES AND OTHER APPLICATION LOGIC
    $app->run

    View Slide

  36. $app = new
    $app 'aws' = function
    return new
    'region' => 'us-east-1'
    'S3' => 'version' => '2006-03-01'
    'DynamoDb' => 'version' => '2012-08-10'

    View Slide

  37. View Slide

  38. $app->get '/' function ...

    View Slide

  39. $dynamoDb = $app 'aws' ->createDynamoDb
    $result = $dynamoDb->query
    'TableName' => 'selphpies'
    'Limit' =>
    // ...
    $items = $result 'Items'

    View Slide

  40. $results = $dynamoDb->getPaginator 'Query'
    'TableName' => 'selphpies'
    // ...
    $items = $results->search 'Items[]'

    View Slide

  41. $results = $s3->getPaginator 'ListObjects' ...
    $files = $results->search 'Contents[].Key'

    View Slide

  42. # With Paginators
    $results = $s3->getPaginator 'ListObjects'
    'Bucket' => 'my-bucket'
    $keys = $results->search 'Contents[].Key'
    foreach $keys as $key
    echo $key . "\n"
    # Without Paginators
    $marker =
    do
    $args = 'Bucket' => 'my-bucket'
    if $marker
    $args 'Marker' = $marker
    $result = $s3->listObjects $args
    $objects = array $result 'Contents'
    foreach $objects as $object
    echo $object 'Key' . "\n"
    $marker $result->search
    'NextMarker || Contents[-1].Key'
    while $result 'IsTruncated'

    View Slide

  43. http://jmespath.org/
    $result->search ''
    > 'Contents[].Key'
    > '[CommonPrefixes[].Prefix, Contents[].Key][]'
    > 'NextMarker || Contents[-1].Key'

    View Slide

  44. $app->post '/upload' function ...

    View Slide

  45. try {
    $caption = $request->request->get('selphpieCaption', '...');
    $file = $request->files->get('selphpieImage');
    if (!$file instanceof UploadedFile || $file->getError()) {
    throw new \RuntimeException('...');
    }
    #1. UPLOAD THE IMAGE TO S3
    #2. SAVE THE IMAGE DATA TO DYNAMODB
    $app['session']->getFlashBag()->add('alerts', 'success');
    return $app->redirect('/');
    } catch (\Exception $e) {
    $app['session']->getFlashBag()->add('alerts', 'danger');
    return $app->redirect('/upload');
    }

    View Slide

  46. $s3 = $app['aws']->createS3();
    $result = $s3->putObject([
    'Bucket' => 'selphpies',
    'Key' => $file->getClientOriginalName(),
    'Body' => fopen($file->getFileName(), 'r'),
    'ACL' => 'public-read',
    ]);

    View Slide

  47. // Automatically switches to multipart uploads
    // if the file is larger than default threshold.
    $result = $s3->upload(
    'selphpies',
    $file->getClientOriginalName(),
    fopen($file->getPathname(), 'r'),
    'public-read'
    );

    View Slide

  48. $dynamoDb->putItem([
    'TableName' => 'selphpies',
    'Item' => [
    // ...
    'src' => ['S' => $result['ObjectURL']],
    'caption' => ['S' => $caption],
    ],
    ]);

    View Slide

  49. $m = new ();
    $dynamoDb->putItem([
    'TableName' => 'selphpies',
    'Item' => $m->marshalItem([
    // ...
    'src' => $result['ObjectURL’],
    'caption' => $caption,
    ]),
    ]);

    View Slide

  50. PHP
    such php
    so cloud
    wow
    very selfie
    much app
    #phpdoge

    View Slide

  51. github.com/aws/aws-sdk-php/releases
    @awsforphp
    blogs.aws.amazon.com/php

    View Slide

  52. github.com/aws/aws-sdk-php
    forums.aws.amazon.com

    View Slide

  53. View Slide

  54. By Jeremy Lindblom (@jeremeamia)

    View Slide