The AWS SDK for PHP allows PHP developers to interact with AWS services in a fluid and familiar way. Learn how to use convenience features, like iterators and waiters, as well as high-level abstractions, such as the Amazon Simple Storage Service (Amazon S3) stream wrapper. We also demonstrate the powerful capabilities inherited from the underlying Guzzle library.
4. AWS SDK for PHP
• Suite of HTTP clients for AWS services
• Built on top of Guzzle (guzzlephp.org)
– Persistent connections, parallel requests
– Event hooks, plugins, wire logging
• Helpful functionality
–
–
–
–
Easy pagination
"waiters"
Automatic retries
etc.
6. AWS SDK for PHP – Installing via Composer
In your composer.json:
{
"require": {
"aws/aws-sdk-php": "~2.4"
}
}
From your terminal:
> composer.phar install
http://getcomposer.org
27. Command Syntax – Short vs. Long Form
// Short form
$result = $s3->putObject([
// ...
]);
// Long form
$command = $s3->getCommand('PutObject', [
// ...
]);
$result = $command->getResult();
31. Iterators – Enumerate Your Data Easily
• Iterate through entire result sets
• No handling of markers or tokens
• Lazy loads results
• Compatible with SPL iterators
32. Iterators – Enumerating Your Data Easily
$objects = $s3->getListObjectsIterator([
'Bucket' => 'my-bucket-name',
]);
foreach ($objects as $object) {
echo $object['Key'] . "n";
}
33. SDK 1.x – A Time Before Iterators
$sk = null;
$people = array();
do {
$params = array('TableName'=>'people');
if ($sk) {
$params['ExclusiveStartKey'] = array(
'HashKeyElement' => array(
'S' => $sk
)
);
$sk= null;
}
$r = $dynamo_db->scan($params);
if ($r->isOK()) {
foreach ($r->body->Items as $item) {
echo (string) $item->name->S;
}
if ($lk = $r->body->LastEvaluatedKey) {
$sk = (string) $lk->HashKeyElement->S;
}
} else {
throw new DynamoDB_Exception('...');
}
}
while ($sk);
34. SDK 1.x – A Time Before Iterators
$sk = null;
$people = array();
do {
$params = array('TableName'=>'people');
if ($sk) {
$params['ExclusiveStartKey'] = array(
'HashKeyElement' => array(
'S' => $sk
)
);
$sk= null;
}
$r = $dynamo_db->scan($params);
if ($r->isOK()) {
foreach ($r->body->Items as $item) {
echo (string) $item->name->S;
}
if ($lk = $r->body->LastEvaluatedKey) {
$sk = (string) $lk->HashKeyElement->S;
}
} else {
throw new DynamoDB_Exception('...');
}
}
while ($sk);
35. SDK 2.x – The Era of Iterators
$scan = $db->getScanIterator([
'TableName'
=> 'People',
'AttributesToGet' => ['Id', 'Name']
]);
foreach ($scan as $person) {
echo $item['Name']['S'];
}
36. Advanced Iterator Usage – Example
$objects = $s3->getListObjectsIterator([
'Bucket' => 'my-bucket-name',
]);
$objects = new LimitIterator($objects, 0, 5);
foreach ($objects as $object) {
echo $object['Key'] . "n";
}
37. Using the AWS SDK for PHP
CreateBucket
PutObject
ListObjects
44. Pseudo-directory
• Amazon S3 knows about buckets and keys
• There aren't real subdirectories
• Pseudo-directories can be created using a key
prefix
Bucket: foo
Key: baz/bar/bam
URL: http://foo.s3.amazonaws.com/baz/bar/bam
45. Reading bytes off of a stream
$fp = fopen('s3://bucket_name/key', 'r');
while (!feof($fp)) {
echo fread($fp, 1024);
}
fclose($fp);
Instead of reading all bytes up front and then using them
46. Using Stream Filters
Filters are used for: Conversion, Compression, Encryption, etc.
$in = fopen('s3://bucket_name/key.zip', 'r');
$out = fopen('/path/to/file', 'w');
stream_filter_append($out, 'zlib.inflate');
stream_copy_to_stream($in, $out);
fclose($in);
fclose($out);
47. Listing Objects and Buckets
$dir = "s3://bucket_name/";
if (is_dir($dir) && ($dh = opendir($dir))) {
while (($file = readdir($dh)) !== false) {
echo "Name: {$file}n";
}
closedir($dh);
}
48. Listing Objects and Buckets
The more intuitive recursive iterator iterator iterator iterator …
$dir = 's3://bucket_name';
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($iterator as $file) {
echo $file->getType() . ': ' . $file . "n";
}
50. PHP Stream Wrappers: Summary
•
•
•
•
Access Amazon S3 like a local filesystem
Read bytes off of a stream on demand
Use familiar methods like file_get_contents
Access to PHP's stream filters
51. "I need to upload a local directory to
an Amazon S3 bucket"
For static blogs, websites, etc.
57. uploadDirectory()
1. Recursively iterates over all local files
2. Checks if the file does not exist or if it has changed
3. Uploads matching files (single or multipart)
$client->uploadDirectory('/local/directory', 'bucket');
public function uploadDirectory(
$directory,
$bucket,
$keyPrefix = null,
array $options = array()
)
60. "I need to download an Amazon S3 bucket to
my local filesystem"
61. downloadBucket()
Works exactly like uploadDirectory()
$client->downloadBucket ('/local/directory', 'bucket');
public function downloadBucket(
$directory,
$bucket,
$keyPrefix = null,
array $options = array()
)
62. Sync Amazon S3 -> Amazon S3
Upload the contents of one Amazon S3 bucket to another using
uploadDirectory().
$client->uploadDirectory(
's3://bucket_src',
'bucket_dest'
);
64. Third-party Integrations
• Laravel 4 Service Provider
• Zend Framework 2 Module
• Silex Service Provider
https://github.com/aws
If you'd like to help us or contribute to another
framework integration, please come talk to us.
65. Mastering the AWS SDK for PHP
Learned how to use the AWS SDK for PHP by
working with some basic Amazon S3 operations.
Learned how to speed up some common
development use cases by using some of the
SDK's high-level abstractions.
66. What you will do next
• Star ★ the AWS SDK for PHP GitHub repo
https://github.com/aws/aws-sdk-php
• composer install the SDK into your project
https://packagist.org/packages/aws/aws-sdk-php
• Subscribe to the AWS PHP Development Blog
http://blogs.aws.amazon.com/php
• Build cool PHP apps on the AWS cloud!
67. Mastering the AWS SDK for PHP
Please give us your feedback on this
presentation
TLS306
As a thank you, we will select prize
winners daily for completed surveys!
Follow us: @awsforphp