© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Takayuki Shimizu @shimy_net
Solutions Architect, Amazon Web Services Japan K.K.
July 16, 2016 – PHP Conference Kansai 2016
/ / . . k d
aep l w
. 9A e n t
B
•  3:3E :9 / 9 9 3 PQ H MZ
/
•  w
•  BB DDD A 9 A 3 B A 9 E B
QKR ZI Z 6 a a a 4& &:4& &:4 Q M Q a 5QRM QZP 7 I V75a A ZPIOV I D QZKP 4 ZV IZQV
z i M P
AWS IoT
ls z i
MQTT
h h h
Amazon Web Services W
•  , h b,. h
•  0) h
Amazon
EC2
AWS
Lambda
Amazon
Kinesis
Amazon
S3
Amazon API
Gateway
Amazon
SNS
Amazon
RDS
AWS IoT
etc.
T h Pe
•  4 D h b h hv w 4A
Ruby
iOS
 Python
Android
 Node.js
.NET
 PHP
JavaScript
Java
 Xamarin
AWS Management Console k AWS CLI k
AWS SDKs k
https://aws.amazon.com/jp/tools/
•  A:A u 4 D h w
•  4 D h v 4A IW
AWS SDK for PHP W H
b
Put, Get
DB
AWS SDK for PHP
h h
h
h h
D, h
1.  Composer k
2.  u AWS SDK hk
3.  Require Composer n
•  Composer
•  PHP h
h h
Composer k
curl -sS https://getcomposer.org/installer | php
php composer.phar require aws/aws-sdk-php
<?php
require 'vendor/autoload.php';
•  Phar h
•  PZZW2((LVK I I I_V KV (I & LR&WPW( ,(LV SVIL(I WPI
•  G A h
•  PZZW2((LVK I I I_V KV (I & LR&WPW( ,(LV SVIL(I _QW
b
<?php
require '/path/to/aws.phar';
<?php
require '/path/to/aws-autoloader.php';
Phar
: http://yomikata.org/word/phar
AWS Service Provider for Laravel
Version 3.1.0 – For Version 3 of the AWS SDK for PHP and Laravel 5.1
Older Versions:
2.0 branch – For Version 2 of the AWS SDK for PHP and Laravel 5.0
1.0 branch – For Version 2 of the AWS SDK for PHP and Laravel 4.x
AWS SDK ZF2 Module (Zend Framework 2)
Version 2.1.0 – For Version 3 of the AWS SDK for PHP and ZF2
Older Versions:
1.2 branch – For Version 2 of the AWS SDK for PHP and ZF2
AWS Service Provider for Silex
Version 3.0.0 – For Version 3 of the AWS SDK for PHP and Silex 2
Older Versions:
2.0 branch – For Version 3 of the AWS SDK for PHP and Silex 1.x
1.1. branch – For Version 2 of the AWS SDK for PHP and Silex 1.x
Framework-specific modules support Version 3 of AWS SDK
•  i
•  h , b h u w rb
API h v b g
•  i h h
•  API yb
h h h b w
w r h w c
AWS SDK for PHP “Version 3”
•  composer.json
•  Factory -> New
Version 2 -> Version 3 iz
"require": {
- "aws/aws-sdk-php": "2.*"
+ "aws/aws-sdk-php": "3.*"
},
$ composer.phar update
$ec2client = Ec2Client::factory([
'region' => 'ap-northeast-1'
]);
$ec2client = new Ec2Client([
'region' => 'ap-northeast-1',
'version' => 'latest'
]);
•  AWS SDK for PHP Version 3 Guzzle Promises t b
Promises w c
•  SDK h API h
h h t b h Async
w c
•  h Promise bPromise h
fulfill ub h reject c
n t W Promise
// Sync
$client->get()
// Async
$client->getAsync()
• 
// Result
$result = $s3Client->putObject([
    'Bucket' => 'your-bucket',
    'Key'    => 'docs/file.pdf',
    'Body'   => fopen('/path/to/file.pdf', 'r'),
]);
 
// Result
echo $result['ObjectURL'];
n t Promise fe
1
2
3
4
5
6
7
8
9
// Promise
$promise = $s3Client->putObjectAsync([
    'Bucket' => 'your-bucket',
    'Key'    => 'docs/file.pdf',
    'Body'   => fopen('/path/to/file.pdf', 'r'),
]);
 
// Result
$result = $promise->wait();
 
// Result
echo $result['ObjectURL'];
1
2
3
4
5
6
7
8
9
10
11
12
• 
Promise
Promise
Promise W
•  Promise h
Promise
h
Resolve
Reject
Promise
Promise
then
then
catch
Method Chain
$s3Client = new AwsS3S3Client([
    ‘region’ => ‘us-west-2’,
    ‘version’ => ‘2006-03-01’,
]);
 
// response
$buckets = $s3Client->listBuckets();
 
//
$promise = $s3Client->listBucketsAsync();
 
// request callback
$promise->then(
    //
    function (AwsResultInterface $buckets) {
        echo ‘My buckets are: ‘
             . implode(‘, ‘, $buckets->search(‘Buckets[].Name’));
    },
    //
    function ($reason) {
        echo "The promise was rejected with {$reason}";
    }
);
Promises
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
•  h A V Q M t b h v
h – c
•  r h b h h
t v c
$filesToUpload = [
    ‘/path/to/file/1.ext’,
    ‘/path/to/file/2.ext’,
    …
    ‘/path/to/file/10.ext’,
];
$promises = [];
// putObjectAsync
foreach ($filesToUpload as $path) {
    $promises []= $s3Client->putObjectAsync([
        ‘Bucket’ => $bucketName,
        ‘Key’ => basename($path),
        ‘SourceFile’ => $path,
    ]);
}
// promise fulfilled fulfilled allPomise
$allPromise = GuzzleHttpPromiseall($promises);
$allPromise->wait();
Promises
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
•  A V Q M v h t p c
•  D, 5 KRMZ ) h b h
h yb ) b ) v
– c
PZZW2((LVK I I I_V KV (I & LR&WPW( ,(O QLM(O QLM(W V Q M PZ S
$sdk = new AwsSdk([‘region’ => ‘us-west-2’, ‘version’ => ‘latest’]);
$elasticacheClient = $sdk->get(‘elasticache’);
$s3Client = $sdk->get(‘s3’);
$promises = [];
// S3 bucket
for ($i = 0; $i < 3; $i++) {
    $promises []= $s3Client->createBucket([
        ‘Bucket’ => “my-bucket-$i”,
    ]);
}
// ElastiCache cluster
$cacheClusterId = uniqid(‘cache’);
$promises []= $elasticacheClient->createCacheCluster([
    ‘CacheClusterId’ => $cacheClusterId,
]);
// promise fulfilled fulfilled metaPomise
$metaPromise = GuzzleHttpPromiseall($promises);
$metaPromise->wait();
Waiters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
•  S3 buckets ElastiCache cluster b v r b y u
AWS h u p c
•  $metaPromise createBucket createCacheCluster h
v w c
$waiterPromises = [];
// S3 Waiter
for ($i = 0; $i < 3; $i++) {
    $waiter = $s3Client->getWaiter(‘BucketExists’, [
        ‘Bucket’ => “my-bucket-$i”,
    ]);
    // Waiter promise
    $waiterPromises []= $waiter->promise();
}
// ElastiCache cluster Waiter
$waiterPromises []= $elasticacheClient
    ->getWaiter(‘CacheClusterAvailable’, [
        ‘CacheClusterId’ => $cacheClusterId,
    ])
    ->promise();
// waiterPromise fulfilled fulfilled metaWaiterPomise
$metaWaiterPromise = GuzzleHttpPromiseall($waiterPromises);
$metaWaiterPromise->wait();
Waiters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
•  h v w r r vp c
•  s–bS3Client wailUntil s–b Bucket v v veeea
$s3Client->waitUntil(‘BucketExists’, [‘Bucket’ => $bucketName]);
http://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/waiters.html
Command Pool
•  h h Command Pool w c
•  Command Pool Client Command c b6V I L
b h getCommand w c
// S3Client
$s3Client = new AwsS3S3Client([
    ‘region’ => ‘us-west-2’,
    ‘version’ => ‘latest’,
]);
 
// Commands
$commands = [
    $s3Client->getCommand(‘ListObjects’, [‘Bucket’ => ‘bucket1’]),
    $s3Client->getCommand(‘ListObjects’, [‘Bucket’ => ‘bucket2’]),
    $s3Client->getCommand(‘ListObjects’, [‘Bucket’ => ‘bucket3’]),
];
 
// Command pool
$pool = new AwsCommandPool($s3Client, $commands);
 
// Commands
$promise = $pool->promise();
 
// pool
$promise->wait();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Command Pool z H
•  g h u Promise b $s3Client-
>listObjectsAsync(…) h z v c b
6V I L AVVS – $pool->promise( h
b h hv v w c
•  Command Pool h 25 c
h h w c
AWS SDK for PHP D,
Command
Pool
http://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/commands.html#commandpool
fe AWS r Wi o py
Amazon S3 – n k u
•  h h
•  h h
h
h h4
h h5
h h6f 95(
h 2
11 111111111
h u
Amazon S3 – n k u
C7D
5
4G 4G
C7D
66 66 D,
. . . .
b
Amazon DynamoDB – NoSQL w
•  e h
•  h , 4G v
•  h CMIL QZM b h
v w
h
6 6
Amazon DynamoDB – NoSQL w
5
6 6
7 I V75
. .. .
Amazon SQS – o r
•  @ ZW Z 4 b W Z 5
•  4 @ ZW Z y
•  5 v u
•  DBD b v
4 5
4 5
Amazon SQS
Amazon SQS – o r
C7D
5
C7D
V RMDBD66 66
. .
4G 4G
. .
. .
4 I_V D D
V QSM A P
Apple APNS
Google GCM
Amazon ADM
Amazon SNS Mobile Push – zt
•  h w
•  p v w
k
z m k i
h
h
, h
- h
6 MIZM ASIZNV
LWVQ Z
. LWVQ Z4
/ LWVQ Z4
9 . A
k
3B3 3A
. .
Amazon SNS Mobile Push – zt
k
z m k i
. h
LWVQ Z4
9 . A
3B3 3A
:
, A SQ P- h
h
. .
Amazon SNS Mobile Push – zt
•  4 D D7 NV A:A EM QV ,
•  % h
•  h
•  A V Q M % IQZM 6V I L AVVS
•  y r 4 D h h
•  4 I_V D,% 7 I V75% DBD D D
_Wa
Thank you !

AWS SDK for PHP のインストールから 始めるクラウドマスターへの道 〜 Promise による非同期オペレーション 〜

  • 1.
    © 2016, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Takayuki Shimizu @shimy_net Solutions Architect, Amazon Web Services Japan K.K. July 16, 2016 – PHP Conference Kansai 2016 / / . . k d aep l w . 9A e n t
  • 2.
    B •  3:3E :9/ 9 9 3 PQ H MZ / •  w •  BB DDD A 9 A 3 B A 9 E B QKR ZI Z 6 a a a 4& &:4& &:4 Q M Q a 5QRM QZP 7 I V75a A ZPIOV I D QZKP 4 ZV IZQV
  • 3.
    z i MP AWS IoT ls z i MQTT h h h
  • 4.
    Amazon Web ServicesW •  , h b,. h •  0) h Amazon EC2 AWS Lambda Amazon Kinesis Amazon S3 Amazon API Gateway Amazon SNS Amazon RDS AWS IoT etc.
  • 5.
    T h Pe • 4 D h b h hv w 4A Ruby iOS Python Android Node.js .NET PHP JavaScript Java Xamarin AWS Management Console k AWS CLI k AWS SDKs k https://aws.amazon.com/jp/tools/
  • 6.
    •  A:A u4 D h w •  4 D h v 4A IW AWS SDK for PHP W H b Put, Get DB AWS SDK for PHP h h h h h D, h
  • 7.
    1.  Composer k 2. u AWS SDK hk 3.  Require Composer n •  Composer •  PHP h h h Composer k curl -sS https://getcomposer.org/installer | php php composer.phar require aws/aws-sdk-php <?php require 'vendor/autoload.php';
  • 8.
    •  Phar h • PZZW2((LVK I I I_V KV (I & LR&WPW( ,(LV SVIL(I WPI •  G A h •  PZZW2((LVK I I I_V KV (I & LR&WPW( ,(LV SVIL(I _QW b <?php require '/path/to/aws.phar'; <?php require '/path/to/aws-autoloader.php';
  • 9.
  • 10.
    AWS Service Providerfor Laravel Version 3.1.0 – For Version 3 of the AWS SDK for PHP and Laravel 5.1 Older Versions: 2.0 branch – For Version 2 of the AWS SDK for PHP and Laravel 5.0 1.0 branch – For Version 2 of the AWS SDK for PHP and Laravel 4.x AWS SDK ZF2 Module (Zend Framework 2) Version 2.1.0 – For Version 3 of the AWS SDK for PHP and ZF2 Older Versions: 1.2 branch – For Version 2 of the AWS SDK for PHP and ZF2 AWS Service Provider for Silex Version 3.0.0 – For Version 3 of the AWS SDK for PHP and Silex 2 Older Versions: 2.0 branch – For Version 3 of the AWS SDK for PHP and Silex 1.x 1.1. branch – For Version 2 of the AWS SDK for PHP and Silex 1.x Framework-specific modules support Version 3 of AWS SDK
  • 11.
    •  i •  h, b h u w rb API h v b g •  i h h •  API yb h h h b w w r h w c AWS SDK for PHP “Version 3”
  • 12.
    •  composer.json •  Factory-> New Version 2 -> Version 3 iz "require": { - "aws/aws-sdk-php": "2.*" + "aws/aws-sdk-php": "3.*" }, $ composer.phar update $ec2client = Ec2Client::factory([ 'region' => 'ap-northeast-1' ]); $ec2client = new Ec2Client([ 'region' => 'ap-northeast-1', 'version' => 'latest' ]);
  • 13.
    •  AWS SDKfor PHP Version 3 Guzzle Promises t b Promises w c •  SDK h API h h h t b h Async w c •  h Promise bPromise h fulfill ub h reject c n t W Promise // Sync $client->get() // Async $client->getAsync()
  • 14.
    •  // Result $result =$s3Client->putObject([     'Bucket' => 'your-bucket',     'Key'    => 'docs/file.pdf',     'Body'   => fopen('/path/to/file.pdf', 'r'), ]);   // Result echo $result['ObjectURL']; n t Promise fe 1 2 3 4 5 6 7 8 9 // Promise $promise = $s3Client->putObjectAsync([     'Bucket' => 'your-bucket',     'Key'    => 'docs/file.pdf',     'Body'   => fopen('/path/to/file.pdf', 'r'), ]);   // Result $result = $promise->wait();   // Result echo $result['ObjectURL']; 1 2 3 4 5 6 7 8 9 10 11 12 • 
  • 15.
    Promise Promise Promise W •  Promiseh Promise h Resolve Reject Promise Promise then then catch Method Chain
  • 16.
    $s3Client = newAwsS3S3Client([     ‘region’ => ‘us-west-2’,     ‘version’ => ‘2006-03-01’, ]);   // response $buckets = $s3Client->listBuckets();   // $promise = $s3Client->listBucketsAsync();   // request callback $promise->then(     //     function (AwsResultInterface $buckets) {         echo ‘My buckets are: ‘              . implode(‘, ‘, $buckets->search(‘Buckets[].Name’));     },     //     function ($reason) {         echo "The promise was rejected with {$reason}";     } ); Promises 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 •  h A V Q M t b h v h – c •  r h b h h t v c
  • 17.
    $filesToUpload = [     ‘/path/to/file/1.ext’,     ‘/path/to/file/2.ext’,     …     ‘/path/to/file/10.ext’, ]; $promises= []; // putObjectAsync foreach ($filesToUpload as $path) {     $promises []= $s3Client->putObjectAsync([         ‘Bucket’ => $bucketName,         ‘Key’ => basename($path),         ‘SourceFile’ => $path,     ]); } // promise fulfilled fulfilled allPomise $allPromise = GuzzleHttpPromiseall($promises); $allPromise->wait(); Promises 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 •  A V Q M v h t p c •  D, 5 KRMZ ) h b h h yb ) b ) v – c PZZW2((LVK I I I_V KV (I & LR&WPW( ,(O QLM(O QLM(W V Q M PZ S
  • 18.
    $sdk = newAwsSdk([‘region’ => ‘us-west-2’, ‘version’ => ‘latest’]); $elasticacheClient = $sdk->get(‘elasticache’); $s3Client = $sdk->get(‘s3’); $promises = []; // S3 bucket for ($i = 0; $i < 3; $i++) {     $promises []= $s3Client->createBucket([         ‘Bucket’ => “my-bucket-$i”,     ]); } // ElastiCache cluster $cacheClusterId = uniqid(‘cache’); $promises []= $elasticacheClient->createCacheCluster([     ‘CacheClusterId’ => $cacheClusterId, ]); // promise fulfilled fulfilled metaPomise $metaPromise = GuzzleHttpPromiseall($promises); $metaPromise->wait(); Waiters 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 •  S3 buckets ElastiCache cluster b v r b y u AWS h u p c •  $metaPromise createBucket createCacheCluster h v w c
  • 19.
    $waiterPromises = []; //S3 Waiter for ($i = 0; $i < 3; $i++) {     $waiter = $s3Client->getWaiter(‘BucketExists’, [         ‘Bucket’ => “my-bucket-$i”,     ]);     // Waiter promise     $waiterPromises []= $waiter->promise(); } // ElastiCache cluster Waiter $waiterPromises []= $elasticacheClient     ->getWaiter(‘CacheClusterAvailable’, [         ‘CacheClusterId’ => $cacheClusterId,     ])     ->promise(); // waiterPromise fulfilled fulfilled metaWaiterPomise $metaWaiterPromise = GuzzleHttpPromiseall($waiterPromises); $metaWaiterPromise->wait(); Waiters 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 •  h v w r r vp c •  s–bS3Client wailUntil s–b Bucket v v veeea $s3Client->waitUntil(‘BucketExists’, [‘Bucket’ => $bucketName]); http://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/waiters.html
  • 20.
    Command Pool •  hh Command Pool w c •  Command Pool Client Command c b6V I L b h getCommand w c // S3Client $s3Client = new AwsS3S3Client([     ‘region’ => ‘us-west-2’,     ‘version’ => ‘latest’, ]);   // Commands $commands = [     $s3Client->getCommand(‘ListObjects’, [‘Bucket’ => ‘bucket1’]),     $s3Client->getCommand(‘ListObjects’, [‘Bucket’ => ‘bucket2’]),     $s3Client->getCommand(‘ListObjects’, [‘Bucket’ => ‘bucket3’]), ];   // Command pool $pool = new AwsCommandPool($s3Client, $commands);   // Commands $promise = $pool->promise();   // pool $promise->wait(); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
  • 21.
    Command Pool zH •  g h u Promise b $s3Client- >listObjectsAsync(…) h z v c b 6V I L AVVS – $pool->promise( h b h hv v w c •  Command Pool h 25 c h h w c AWS SDK for PHP D, Command Pool http://docs.aws.amazon.com/aws-sdk-php/v3/guide/guide/commands.html#commandpool
  • 22.
    fe AWS rWi o py
  • 23.
    Amazon S3 –n k u •  h h •  h h h h h4 h h5 h h6f 95( h 2 11 111111111 h u
  • 24.
    Amazon S3 –n k u C7D 5 4G 4G C7D 66 66 D, . . . . b
  • 25.
    Amazon DynamoDB –NoSQL w •  e h •  h , 4G v •  h CMIL QZM b h v w h
  • 26.
    6 6 Amazon DynamoDB– NoSQL w 5 6 6 7 I V75 . .. .
  • 27.
    Amazon SQS –o r •  @ ZW Z 4 b W Z 5 •  4 @ ZW Z y •  5 v u •  DBD b v 4 5 4 5 Amazon SQS
  • 28.
    Amazon SQS –o r C7D 5 C7D V RMDBD66 66 . . 4G 4G . . . .
  • 29.
    4 I_V DD V QSM A P Apple APNS Google GCM Amazon ADM Amazon SNS Mobile Push – zt •  h w •  p v w
  • 30.
    k z m ki h h , h - h 6 MIZM ASIZNV LWVQ Z . LWVQ Z4 / LWVQ Z4 9 . A k 3B3 3A . . Amazon SNS Mobile Push – zt
  • 31.
    k z m ki . h LWVQ Z4 9 . A 3B3 3A : , A SQ P- h h . . Amazon SNS Mobile Push – zt
  • 32.
    •  4 DD7 NV A:A EM QV , •  % h •  h •  A V Q M % IQZM 6V I L AVVS •  y r 4 D h h •  4 I_V D,% 7 I V75% DBD D D _Wa
  • 33.