re:G
ene
rate

AWS  マイスターシリーズ  
AWS  SDK  for  PHP  &
AWS  SDK  for  Ruby  &
boto(Python)  &
JavaScript  in  Node.js

2013.12.20
アマゾン  データ  サービス  ジャパン株式会社
ソリューションアーキテクト
今井  榎並  蒋
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
ウェビナー(Webセミナー)へようこそ!
!   参加者は、⾃自動的にミュートになっています
!   質問を投げることができます!
§  GoToWebinarの仕組みを使って、書き込んでください
§  ただし環境によっては、⽇日本語の直接⼊入⼒力力ができないので、
お⼿手数ですが、テキストエディタ等に打ち込んでから、
コピペしてください

§  最後のQ&Aの時間で、できるだけ回答させて頂きます
§  書き込んだ質問は、主催者にしか⾒見見えません

!   Twitterのハッシュタグは#jawsugでどうぞ

2
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
Webセミナー
週刊AWSマイスターシリーズ  re:Generate!
!   AWSマイスターシリーズ

§  12⽉月4⽇日 AWS  re:Invent  アップデート振り返り
§  12⽉月11⽇日 AWS  SDK  for  Java/.Net
§  申し込みサイト
§  http://aws.amazon.com/jp/event_̲schedule/
§  過去資料料集
§  http://aws.amazon.com/jp/aws-‐‑‒meister/

3

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
Agenda
!   AWS  SDK  概要
!   AWS  SDK  for  PHP
! boto(Python)
!   AWS  SDK  for  Ruby
!   AWS  SDK  for  JavaScript  in  Node.js
!   まとめ

4

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
AWS  SDK概要
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
AWS  SDKの重要性
!   AWSはプログラマブルなインフラ

•  プログラムから扱うためにはSDKが必要
•  ほぼすべてのサービスはマネジメントコンソールから利利⽤用でき
るが、⾃自動化しようと思ったらプログラムから扱うのが必須
•  ⾃自動化するとAWSは何倍も便便利利になる

!   代表的な⽤用途

•  インフラ構築/運⽤用の⾃自動化

•  EC2やRDSを上げたり下げたり、CloudFormationでスタックをデ
プロイしたり

•  アプリケーション的なサービスの利利⽤用

•  S3にデータをアップしたり、DynamoDBやSQSにデータ⼊入れたり
出したり

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
AWS  SDK
!   AWSのサービスはHTTP/HTTPSでREST/SOAP形式の
APIをサポート。
例例えば
•  EC2  :  Start/Stop/Terminate
•  S3  :  GetObject/PutObject
•  DynamoDB  :  Get/Put

!   SDKはこれらのAPIを抽象化し、各⾔言語からの利利⽤用を⾮非
常に簡単にしてくれる

Your  code
AWS  SDK
API
7

EC2

S3

その他サービス

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
AWS  SDK
!   下記の⾔言語/環境で提供中

Java

Python

JavaScript
in  nodeJS

PHP

JavaScript
in  the  Browser

.NET

iOS

Ruby

Android

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
AWS  SDK
!   今回は下記についてお話します。

Java

Python

JavaScript
in  Node.js

PHP

JavaScript
in  the  Browser

.NET

iOS

Ruby

Android

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
AWS  SDKの使い⽅方
!   必要な⾔言語のSDKをインストール

•  ⾔言語ごとにインストール⽅方法が異異なります。このあとの章で⾔言語ご
とに解説します。

!   Credential(AWS  APIの認証情報)を⽤用意する
!   ファクトリーメソッドを利利⽤用してサービス(例例えばS3)のクラ
イアントオブジェクトを⽣生成
•  このときにCredentialを渡す

!   クライアントオブジェクトのメソッドを使ってオペレーショ
ン(例例えばPutObject)
!   ⾔言語によってはより⾼高度度に抽象化されているSDKもあります。
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
Credentialの取り扱い
!   Credentialの取り扱いについてはいくつかのやり⽅方があ
りますが、IAM  Roleもしくは環境変数を使うのがオス
スメです。PHPを例例にそれぞれの実装⽅方法をご案内しま
す。
!   コード内に直接埋め込むパターン(コード内に
Credentialが⼊入り込んでしまうのでやっちゃダメなパ
ターン)
<?php
$s3client  =  S3Client::factory(array(
'key'
=>
'your-‐‑‒aws-‐‑‒access-‐‑‒key-‐‑‒id',
'secretʻ‘ =>
'your-‐‑‒aws-‐‑‒secret-‐‑‒access-‐‑‒key',
));    

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
Credentialの取り扱い

! configファイルでCredentialを設定するパターン

•  PHPとbotoではこの⽅方法が提供されている
•  これもCredentialがファイル化されるので良良くないパターン

$  vim  aws-‐‑‒config.php
<?php
return  array(
        'includes'  =>  array('_̲aws'),
        'services'  =>  array(
                'default_̲settings'  =>  array(
                        'params'  =>  array(
                                'key'        =>  'your-‐‑‒access-‐‑‒key-‐‑‒id',
                                'secret'  =>  'your-‐‑‒secret-‐‑‒access-‐‑‒key',)
                )
        )
);
$  vim  sample.php
<?php
$aws  =  Aws::factory(ʻ‘aws-‐‑‒config.php');
$s3client  =  $aws-‐‑‒>get('s3');
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
Credentialの取り扱い
!   環境変数でCredentialを設定するパターン

•  AWS_̲ACCESS_̲KEY_̲ID,  AWS_̲SECRET_̲KEYという環境変数
を実⾏行行ユーザーで設定しておくことによってコードから
Credentialを追い出せる

$  export  AWS_̲ACCESS_̲KEY_̲ID="your-‐‑‒aws-‐‑‒access-‐‑‒key-‐‑‒id"
$  export  AWS_̲SECRET_̲KEY="your-‐‑‒aws-‐‑‒secret-‐‑‒access-‐‑‒key”
$vim  sample.php
<?php
$s3client  =  S3Client::factory();    

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
Credentialの取り扱い
!   IAM  RoleでCredentialを設定するパターン

•  コードが動くのがEC2上であれば、IAM  Roleを使うことによっ
て、EC2⾃自体にAWS  APIへのアクセス権限を付与できる。よっ
てコードや環境変数にCredentialを持たせずに済む。
例例えばS3フルアクセスの権限を持った
IAM  Roleを作成して・・・

EC2にそのRoleを
割り当てると・・・
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
Credentialの取り扱い
!   IAM  RoleでCredentialを設定するパターン

•  前のページのようにS3アクセス権限を持ったRoleをEC2に割り
当てると、下記を書くだけで利利⽤用可能。
$vim  sample.php
<?php
$s3client  =  S3Client::factory();    

•  これは、SDKが内部でSTS(Security  Token  Service)を利利⽤用し
ており、AccessKey,SecretAccessKey,Tokenを⾃自動的に

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
AWS  SDK  for  PHP
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
AWS  SDK  for  PHP
!
!
!
!
!
!

  Amazon提供のAWS開発⽤用のPHP向けSDK
  現在は、Version2
http://aws.amazon.com/jp/sdkforphp/
  環境:PHP5.3.3以降降
  Guzzle  HTTP  Client  framework上で構築されている
  依存Extension
•  cURL

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
操作可能サービス
Direct Connect

Elasticache

IAM

EC2

SimpleDB

OpsWorks

ELB

S3

Elastic Transcoder

Auto Scaling

Glacier

SQS

EMR

CloudFront

SNS

Route53

Storage Gateway

SES

VPC

Import/Export

SWF

DynamoDB

Elastic Beanstalk

CloudSearch

RDS

Cloud Formation

Redshift

Cloud Watch

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
利利⽤用⽅方法
!   Composerによるインストール(推奨)

•  http://docs.aws.amazon.com/aws-‐‑‒sdk-‐‑‒php/guide/latest/
installation.html#installing-‐‑‒via-‐‑‒composer

!   Pharよるインストール

•  http://docs.aws.amazon.com/aws-‐‑‒sdk-‐‑‒php/guide/latest/
installation.html#installing-‐‑‒via-‐‑‒phar

!   Zipファイルからインストール

•  http://docs.aws.amazon.com/aws-‐‑‒sdk-‐‑‒php/guide/latest/
installation.html#installing-‐‑‒via-‐‑‒zip

!   PEARによるインストール

•  http://docs.aws.amazon.com/aws-‐‑‒sdk-‐‑‒php/guide/latest/
installation.html#installing-‐‑‒via-‐‑‒pear

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
初期設定
!   インストール⽅方法により以下のとおり異異なります
インストール方法

インクルード

Composer

require '/path/to/vendor/autoload.php';

Phar

require '/path/to/aws.phar';

Zip

require '/path/to/aws-autoloader.php';

PEAR

'AWSSDKforPHP/aws.phar';

!   コンフィグレーションファイルの設定

•  クレデンシャル情報などを設定可能
•  factory()の引数として指定することも可能
•  ʻ‘src/Aws/Common/Resources/aws-‐‑‒config.phpʼ’  を参考に追加

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:S3  -‐‑‒  putObject

API仕様:  http://docs.aws.amazon.com/aws-‐‑‒sdk-‐‑‒php/latest/class-‐‑‒
Aws.S3.S3Client.html#_̲putObject

<?php
require  '/path/to/vendor/autoload.php';
use  AwsCommonAws;
use  AwsS3ExceptionS3Exception;
try  {
        $client  =  S3Client::factory; ①
        $bucket  =  ʼ’your_̲bucket';
        $file  =  'test.txt';
        $result  =  $client-‐‑‒>putObject(array(
'Bucket'  =>  $bucket,
'Key'  =>  $file,
'Body'  =>  fopen($file,  'r'),
        ));
        //  結果表⽰示
        var_̲dump($result);
}  catch  (S3Exception  $e)  {
        echo  '***  Error  ***'  .  "n";
        echo  $e-‐‑‒>getMessage();
}

①  S3クライアント作成
②  putObject実⾏行行

②

  Bucketを指定、Keyは、ファ
イル名、Bodyにアップロードす
るファイルストリームを指定

(*)  事前にBucketが作成されて
いる必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:S3  -‐‑‒  getObject

API仕様:  http://docs.aws.amazon.com/aws-‐‑‒sdk-‐‑‒php/latest/class-‐‑‒
Aws.S3.S3Client.html#_̲getObject

<?php
require  '/path/to/vendor/autoload.php';
use  AwsCommonAws;
use  AwsS3ExceptionS3Exception;
try  {
        $client  =  S3Client::factory; ①
        $bucket  =  ʼ’your_̲bucket';
        $file  =  'test.txt';
        $result  =  $client-‐‑‒>getObject(array(
'Bucket'  =>  $bucket,
'Key'  =>  $file,
));
        //  結果表⽰示
        echo  $result['Body']; ③
}  catch  (S3Exception  $e)  {
        echo  '***  Error  ***'  .  "n";
        echo  $e-‐‑‒>getMessage();
}

①  S3クライアント作成
②  getObject実⾏行行

②

  Bucketを指定、Keyは、ファイル
名
③  取得したコンテンツを表⽰示

(*)  事前にBucketが作成されて
いる必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:SQS  -‐‑‒  sendMessage

API仕様:  http://docs.aws.amazon.com/aws-‐‑‒sdk-‐‑‒php/latest/class-‐‑‒
Aws.Sqs.SqsClient.html#_̲sendMessage

<?php
require  '/path/to/vendor/autoload.php';
use  AwsCommonAws;
use  AwsSqsExceptionSqsException;
try  {
        $client  =  SqsClient::factory;        
①
        $queueUrl  =  'https://YourQueue';
        $result  =  $client-‐‑‒>sendMessage(array( ②
'QueueUrl'  =>  $queueUrl,
'MessageBody'  =>  'Send  Message!',
        ));
        //  結果表⽰示
        var_̲dump($result);
}  catch  (SqsException  $e)  {
        echo  '***  Error  ***'  .  "n";
        echo  $e-‐‑‒>getMessage();
}

①  SQSクライアント作成
②  sendMessage実⾏行行
  QueueURLを指定、送りたい
メッセージを指定

(*)  事前にQueueが作成されて
いる必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:SQS  -‐‑‒  receiveMessage

API仕様:  http://docs.aws.amazon.com/aws-‐‑‒sdk-‐‑‒php/latest/class-‐‑‒
Aws.Sqs.SqsClient.html#_̲receiveMessage

<?php
require  '/path/to/vendor/autoload.php';
use  AwsCommonAws;
use  AwsSqsExceptionSqsException;

①  コンフィグを指定してインスタン
ス作成

②  SQSクライアント作成
try  {
        $client  =  SqsClient::factory; ①
③  receiveMessage実⾏行行
        $queueUrl  =  'https://YourQueue';
        $result  =  $client-‐‑‒>receiveMessage(array( ②
  QueueURLを指定
'QueueUrl'  =>  $queueUrl,
        ));
        //  結果表⽰示
        var_̲dump($result);
}  catch  (SqsException  $e)  {
        echo  '***  Error  ***'  .  "n";
        echo  $e-‐‑‒>getMessage();
}
(*)  事前にQueueが作成されて
いる必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:DynamoDB  -‐‑‒  putItem

API仕様:  http://docs.aws.amazon.com/aws-‐‑‒sdk-‐‑‒php/latest/class-‐‑‒
Aws.DynamoDb.DynamoDbClient.html#_̲putItem
<?php
require  '/path/to/vendor/autoload.php';
use  AwsCommonAws;
use  AwsDynamoDbExceptionDynamoDbException;
try  {
        $client  =  DynamoDbClient::factory; ①
        $result  =  $client-‐‑‒>putItem(array(
                'TableName'  =>  ʼ’table_̲name',
                'Item'  =>  $client-‐‑‒>formatAttributes(array(
                        ʼ’id'                =>  100,
                        'timestamp'  =>  130699342,
                        'message'            =>  'Good  Morning.',
                )),
        ));        
        //  結果表⽰示
        var_̲dump($result);
}  catch  (DynamoDbException  $e)  {
        echo  '***  Error  ***'  .  "n";
        echo  $e-‐‑‒>getMessage();
}

①  DynamoDBクライアント作
成

②

②  putItem実⾏行行
-‐‑‒  TableName:  テーブル名
-‐‑‒  Item:  Putするアイテムを
指定
(例例)
    id  :  Hash  Key
    timestamp  :  Range  Key
    message  :  Attribute

(*)  事前にTableが作成されて
いる必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:DynamoDB  -‐‑‒  getItem

API仕様:  http://docs.aws.amazon.com/aws-‐‑‒sdk-‐‑‒php/latest/class-‐‑‒Aws.DynamoDb.DynamoDbClient.html#_̲getItem

<?php
require  '/path/to/vendor/autoload.php';
use  AwsCommonAws;
use  AwsDynamoDbExceptionDynamoDbException;
try  {
        $client  =  DynamoDbClient::factory; ①
        $result  =  $client-‐‑‒>getItem(array( ②
                'ConsistentRead'  =>  true,
                'TableName'  =>  'table_̲name',
                'Key'  =>  array(
ʼ’id'  =>  array('N'  =>  '100'),
'timestamp'  =>  array('N'  =>  '130699342')
)
        ));  
      //  結果表⽰示
        var_̲dump($result);
}  catch  (DynamoDbException  $e)  {
        echo  '***  Error  ***'  .  "n";
        echo  $e-‐‑‒>getMessage();
}

①  DynamoDBクライアント作
成
②  getItem実⾏行行
-‐‑‒  TableName:  テーブル名
-‐‑‒  Key:GetするアイテムのKey
を指定
(例例)
    id  :  Hash  Key
    timestamp  :  Range  Key

(*)  事前にTableが作成されてい
る必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
活⽤用法
!   PHPアプリのバックエンドストレージ呼び出し
•  DynamoDB,SQS,S3を簡単に呼び出し可能

!   PHPベースのアプリケーションの拡張

•  WordpressやEC-‐‑‒CubeなどのPHPベースのアプリケーションと
AWSの連携

!   独⾃自の管理理⽤用Webインターフェースの作成

•  定型作業を簡略略化したり、管理理を容易易にするための独⾃自Webイ
ンターフェースの作成

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
PHP版SDKの便便利利なクラス
! DynamoDBSessionHandler

! DynamoDBを使った、HTTPのセッション共有が可能

EC2

ELB

EC2

EC2

DynamoDB

速い
消えない
管理不要

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
DynamoDBSessionHandlerの利利⽤用
<?php
require  '/home/ec2-‐‑‒user/vendor/autoload.php';
use  AwsCommonAws;
use  AwsDynamoDbExceptionDynamoDbException;
use  AwsDynamoDbDynamoDbClient;
use  AwsDynamoDbSessionSessionHandler;
$aws  =  Aws::factory(ʼ’aws-‐‑‒config.php');
$client  =  $aws-‐‑‒>get('dynamodb');
try  {
        $sessionHandler  =  SessionHandler::factory(array(
                'dynamodb_̲client'  =>  $client,
                'table_̲name'            =>  'sessions',
        ));
        $sessionHandler-‐‑‒>register();
}  catch  (DynamoDbException  $e)  {
        echo  '***  Error  ***'  .  "n";
        echo  $e-‐‑‒>getMessage();
}
session_̲start();
//セッションにデータ登録
$_̲SESSION['username']  =  'jeremy';
$_̲SESSION['role']  =  'admin';
session_̲commit();

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
boto(Python)
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
botoとは
!   Python⽤用のAWS  SDKのデファクトスタンダード
!   最新版は2.19.0  (2013年年12⽉月現在)
!   Python  2.6.6,  2.7.3  on  Mac  OSX  and  Ubuntu  Maverick  
で動作確認している。
!   Python  2.5での互換性もなるべく考慮しているが保証外
!   Python  3.x版はDeveloper  Preview中

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
操作可能サービス
Direct Connect

Elasticache

IAM

EC2

SimpleDB

OpsWorks

ELB

S3

Elastic Transcoder

Auto Scaling

Glacier

SQS

EMR

CloudFront

SNS

Route53

Storage Gateway

SES

VPC

Import/Export

SWF

DynamoDB

Elastic Beanstalk

CloudSearch

RDS

Cloud Formation

Data Pipeline

Redshift

Cloud Watch

CloudTrail

FPS

EBS

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
botoに関するリソース
!   ソースコードリポジトリ

•  https://github.com/boto/boto

! PyPI

•  http://pypi.python.org/pypi/boto

!   オンラインドキュメント

•  http://docs.pythonboto.org/

!   IRC

•  http://webchat.freenode.net/?channels=boto

©  2012  Amazon.com,  Inc.  and  its  affiliates.    All  rights  reserved.    May  not  be  copied,  modified  or  distributed  in  whole  or  in  part  without  the  express  consent  of  
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
Amazon.com,  Inc.
インストール

  $  pip  install  boto

©  2012  Amazon.com,  Inc.  and  its  affiliates.    All  rights  reserved.    May  not  be  copied,  modified  or  distributed  in  whole  or  in  part  without  the  express  consent  of  
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
Amazon.com,  Inc.
初期設定
!   設定ファイルの作成
!   /etc/boto.cfg  -‐‑‒  グローバルな設定を記述
!   ~∼/.boto  ユーザー毎の設定を記述
[Boto]
debug  =  0
num_̲retries  =  10

[DynamoDB]
region  =  ap-‐‑‒northeast-‐‑‒1

©  2012  Amazon.com,  Inc.  and  its  affiliates.    All  rights  reserved.    May  not  be  copied,  modified  or  distributed  in  whole  or  in  part  without  the  express  consent  of  
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
Amazon.com,  Inc.
サンプル:S3  –  set_̲contents_̲from_̲string
>>>  from  boto.s3.connection  import  S3Connection
>>>  conn  =  S3Connection()
>>>  bucket_̲name  =  ”yourBucket"
>>>  bucket  =    conn.get_̲bucket(bucket_̲name)
>>>  from  boto.s3.key  import  Key
>>>  k  =  Key(bucket)
>>>  k.key  =  "test.txt"
>>>  k.set_̲contents_̲from_̲string("Hello  World!")

(*)  事前にBucketが作成されて
いる必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:S3  –  get_̲contents_̲as_̲string
>>>  from  boto.s3.connection  import  S3Connection
>>>  conn  =  S3Connection()
>>>  bucket_̲name  =  "yourBucket"
>>>  bucket  =    conn.get_̲bucket(bucket_̲name)
>>>  from  boto.s3.key  import  Key
>>>  k  =  Key(bucket)
>>>  k.key  =  "test.txt"
>>>  k.get_̲contents_̲as_̲string()

(*)  事前にBucketが作成されて
いる必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:SQS  –  write
>>>  import  boto.sqs
>>>  conn  =  boto.sqs.connect_̲to_̲region("ap-‐‑‒northeast-‐‑‒1")
>>>  q  =  conn.get_̲queue('YourQueue')
>>>  from  boto.sqs.message  import  Message
>>>  m  =  Message()
>>>  m.set_̲body('Hello  World!!')
>>>  q.write(m)

(*)  事前にQueueが作成されて
いる必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:SQS  –  get_̲messages
>>>  import  boto.sqs
>>>  conn  =  boto.sqs.connect_̲to_̲region("ap-‐‑‒northeast-‐‑‒1")
>>>  q  =  conn.get_̲queue('YourQueue')
>>>  rs  =  q.get_̲messages()
>>>  m  =  rs[0]
>>>  m.get_̲body()
'Hello  World!!'

(*)  事前にQueueが作成されて
いる必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:DynamoDB  –  putItem
>>>  from  boto.dynamodb2.items  import  Item
>>>  from  boto.dynamodb2.table  import  Table
>>>  table  =  Table('table_̲name')
>>>  item  =  Item(table  ,  data={
...  ʼ’id':  100,
...  'timestamp':  130699342,
...  'message'  :  'Good  Morning.',
...  })
>>>  item.save()
True

(*)  事前にTableが作成されてい
る必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:DynamoDB  –  getItem
>>>  from  boto.dynamodb2.table  import  Table
>>>  table=  Table('table_̲name')
>>>  item  =  table.get_̲item(id=100,timestamp=130699342)
>>>  print  item['message']
Good  Morning.

(*)  事前にTableが作成されてい
る必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
その他Tips
!   AWSコマンドラインインタフェース(CLI)もbotoを⼀一部
利利⽤用している

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
AWS  SDK  for  Ruby
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
AWS  SDK  for  Rubyとは
!   Amazon提供のAWS開発⽤用のRuby向けSDK
!   現在のバージョン
•  Version  1  (stable)
•  Version  2  (developer  preview)

! http://aws.amazon.com/sdkforruby/  
!   環境:Ruby  1.8.7  以降降

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
操作可能サービス
Direct Connect

Elasticache

IAM

EC2

SimpleDB

OpsWorks

ELB

S3

Elastic Transcoder

Auto Scaling

Glacier

SQS

EMR

CloudFront

SNS

Route53

Storage Gateway

SES

VPC

Import/Export

SWF

DynamoDB

Elastic Beanstalk

CloudSearch

RDS

Cloud Formation

Data Pipeline

Redshift

Cloud Watch

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
利利⽤用⽅方法
!   Ruby環境のセットアップ
•  Rbenvを利利⽤用する
•  https://github.com/sstephenson/rbenv  
•  RVMを利利⽤用する
•  https://rvm.io/  
! aws-‐‑‒sdk  gemをインストール
$  gem  install  aws-‐‑‒sdk

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
設定の初期化
!   デフォルトのリージョンをセッティング
require  'awsʼ’
AWS.config({
:region  =>  ʻ‘YOUR_̲DEFAULT_̲REGIONʼ’
})

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:S3  -‐‑‒  write
API仕様:  http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/S3/S3Object.html#write-‐‑‒
instance_̲method

#!/usr/bin/env  ruby
require  'aws'
AWS.config({
    :access_̲key_̲id  =>  
ENV['AWS_̲ACCESS_̲KEY_̲ID'],
    :secret_̲access_̲key  =>  
ENV['AWS_̲SECRET_̲ACCESS_̲KEY'],
    :region  =>  ENV['AWS_̲REGION']
})

①  コンフィグを指定

①

②  S3クライアント作成し、
bucketやkeyを指定
③  putするfile名を指定し、s3に
put

②

s3  =  AWS::S3.new
bucket  =  s3.buckets['your-‐‑‒bucket']
obj  =  bucket.objects['key']
obj.write(Pathname.new('/path/to/file.txt')) ③

(*)  事前にBucketが作成されて
いる必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:S3  -‐‑‒  read
API仕様:  http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/S3/S3Object.html#read-‐‑‒
instance_̲method

#!/usr/bin/env  ruby
require  'aws'

①  コンフィグを指定

AWS.config({
    :access_̲key_̲id  =>  
ENV['AWS_̲ACCESS_̲KEY_̲ID'],
    :secret_̲access_̲key  =>  
ENV['AWS_̲SECRET_̲ACCESS_̲KEY'],
    :region  =>  ENV['AWS_̲REGION']
})

①

s3  =  AWS::S3.new
bucket  =  s3.buckets['your-‐‑‒bucket']
obj  =  bucket.objects['key']

②

obj.read  do  |chunk|
    puts  chunk
    #=>  data  of  s3://your-‐‑‒bucket/key
end

③

②  S3クライアント作成し、
bucketやkeyを指定
③  s3にあるobjectのデータを取
得

(*)  事前にBucketが作成されて
いる必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:SQS  –  send_̲message

API仕様:  http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/SQS/
Client.html#send_̲message-‐‑‒instance_̲method

#!/usr/bin/env  ruby
require  'aws'
require  'pp'

①  コンフィグを指定

AWS.config({
    :access_̲key_̲id  =>  
ENV['AWS_̲ACCESS_̲KEY_̲ID'],
    :secret_̲access_̲key  =>  
ENV['AWS_̲SECRET_̲ACCESS_̲KEY'],
    :region  =>  ENV['AWS_̲REGION']
})

①

sqs  =  AWS::SQS.new

②

queue_̲url  =  'https://your_̲queue'
result  =  sqs.client.send_̲message({
    :queue_̲url        =>  queue_̲url,
    :message_̲body  =>  'Send  Message!'
})
pp  result

②  SQSクライアント作成

③

③  send_̲message実⾏行行
  queue_̲urlを指定、送りたい
メッセージを指定

(*)  事前にQueueが作成されて
いる必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:SQS  –  receive_̲message

API仕様:  http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/SQS/
Client.html#receive_̲message-‐‑‒instance_̲method

#!/usr/bin/env  ruby
require  'aws'
require  'pp'

①  コンフィグを指定

AWS.config({
    :access_̲key_̲id  =>  
ENV['AWS_̲ACCESS_̲KEY_̲ID'],
    :secret_̲access_̲key  =>  
ENV['AWS_̲SECRET_̲ACCESS_̲KEY'],
    :region  =>  ENV['AWS_̲REGION']
})

①

sqs  =  AWS::SQS.new

②

queue_̲url  =  'https://your_̲queue'
result  =  sqs.client.receive_̲message({
    :queue_̲url        =>  queue_̲url
})
pp  result

②  SQSクライアント作成

③

③  receive_̲message実⾏行行
  queue_̲urlを指定

(*)  事前にQueueが作成されて
いる必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:DynamoDB  –  put_̲item

API仕様:  http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/DynamoDB/Client.html#put_̲item-‐‑‒
instance_̲method
#!/usr/bin/env  ruby
require  'aws'
require  'pp'

①  コンフィグを指定

AWS.config({
①
    :access_̲key_̲id  =>  ENV['AWS_̲ACCESS_̲KEY_̲ID'],
    :secret_̲access_̲key  =>  ENV['AWS_̲SECRET_̲ACCESS_̲KEY'],
    :region  =>  ENV['AWS_̲REGION']
})
client  =  AWS::DynamoDB::Client.new
result  =  client.put_̲item({
    :table_̲name  =>  'table_̲nameʼ’,
    :item  =>  {
        'id'                              =>  {:n  =>  '100'},
        'timestamp'  =>  {:n  =>  '130699342'},
        'message'        =>  {:s  =>  'Good  Morning'}
    }
})
pp  result
#=>  {"ConsumedCapacityUnits"=>1.0}

②
③

②  DynamoDBクライアント作成
③  put_̲item実⾏行行
-‐‑‒  :table_̲name:  テーブル名
-‐‑‒  :item:  Putするアイテムを指
定
(例例)
    id  :  Hash  Key
    timestamp  :  Range  Key
    message  :  Attribute

(*)  事前にTableが作成されてい
る必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:DynamoDB  –  get_̲item

API仕様:  http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/DynamoDB/Client.html#get_̲item-‐‑‒
instance_̲method
#!/usr/bin/env  ruby
require  'aws'
require  'pp'

①  コンフィグを指定

AWS.config({
①
    :access_̲key_̲id  =>  ENV['AWS_̲ACCESS_̲KEY_̲ID'],
    :secret_̲access_̲key  =>  ENV['AWS_̲SECRET_̲ACCESS_̲KEY'],
    :region  =>  ENV['AWS_̲REGION']
})
client  =  AWS::DynamoDB::Client.new
result  =  client.get_̲item({
    :consistent_̲read  =>  true,
    :table_̲name  =>  'table_̲name',
    :key  =>  {
        :hash_̲key_̲element  =>  {:n  =>  '100'},
        :range_̲key_̲element  =>  {:n  =>  '130699342'}
    }
})
pp  result
#=>  {"Item"=>{"id"=>{"N"=>"100"},  
"message"=>{"S"=>"Good  Morning"},  
"timestamp"=>{"N"=>"130699342"}},  
"ConsumedCapacityUnits"=>1.0}

②
③

②  DynamoDBクライアント
作成
③  get_̲item実⾏行行
-‐‑‒  :table_̲name  テーブル名
-‐‑‒  :consistent_̲read  ⼀一貫性
-‐‑‒  :key  GutするKeyを指定
(例例)
    id  :  Hash  Key
    timestamp  :  Range  Key

(*)  事前にTableが作成され
ている必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
その他Tips
!   新しいRuby  SDK  (version  2)
• 
• 
• 
• 

Developer  Preview
バリデーション、ドキュメント、拡張性など改善
プラグイン⽅方式をとっている
Ruby  1.9以降降が必須

!   以下のように利利⽤用可能
$  gem  install  aws-‐‑‒sdk-‐‑‒core

AWS::DynamoDB.new(api_̲version:  ʻ‘2012-‐‑‒08-‐‑‒10ʼ’)
Re:Inventで発表された新Ruby  SDKのスライドはおすすめ

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
その他Tips
!   RailsのセッションをDynamoDBに保存

•  スケーラブルかつ耐障害に優れたセッションストア
•  Rails  3.x  か  4.x  から利利⽤用可能
•  Rackベースのアプリも利利⽤用可能

!   以下の⼿手順で利利⽤用可能
gem  ʻ‘aws-‐‑‒sessionstore-‐‑‒dynamodbʼ’
$  bundle  install
参考:https://github.com/aws/aws-‐‑‒sessionstore-‐‑‒dynamodb-‐‑‒ruby  

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
AWS  SDK  for  JavaScript
in  Node.js
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
AWS  SDK  for  JavaScript  in  Node.jsとは
!   Amazon提供のAWS開発⽤用のnode.js環境で動くJavaScript
向けSDK
•  http://aws.amazon.com/sdkfornodejs/

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
操作可能サービス
Direct Connect

Elasticache

IAM

EC2

SimpleDB

OpsWorks

ELB

S3

Elastic Transcoder

Auto Scaling

Glacier

SQS

EMR

CloudFront

SNS

Route53

Storage Gateway

SES

VPC

Import/Export

SWF

DynamoDB

Elastic Beanstalk

CloudSearch

RDS

Cloud Formation

Data Pipeline

Redshift

Cloud Watch

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
利利⽤用⽅方法
! Node.js環境のセットアップ
•  OSのパッケージマネージャーでnpmをインストール
•  Mac  の場合:
$  brew  install  npm

! aws-‐‑‒sdk  packageをインストール
$  npm  install  aws-‐‑‒sdk

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:S3  -‐‑‒  putObject
var  AWS  =  require('aws-‐‑‒sdk');
var  fs  =  require('fs');
var  s3  =  new  AWS.S3();

①

var  bodyStream  =  fs.createReadStream(  '/path/ ②
to/file.txt'  );
var  params  =  {
    Bucket:  'your-‐‑‒bucket',
    Key:  'key',
    Body:  bodyStream
};
s3.putObject(params,  function(err,  data)  {
    if  (err)
        console.log(err);
    else
        console.log("Successfully  uploaded.");
});

①  S3クライアント作成

②  put時の引数
•  Bucket:  s3  bucket
•  Key:  s3  key
•  Body:  実際のデータ

③ ③  S3へPUT

(*)  事前にBucketが作成されて
いる必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:S3  -‐‑‒  getObject
var  AWS  =  require('aws-‐‑‒sdk');
var  fs  =  require('fs');
var  s3  =  new  AWS.S3();

①

var  params  =  {
    Bucket:  ʼ’your-‐‑‒bucket',
    Key:  ʼ’key'
};

②

s3.getObject(params,  function(err,  data)  {
    if  (err)
        console.log(err);
    else
        console.log(data.Body.toString());
});

③

①  S3クライアント作成

②  gut時の引数
•  Bucket:  s3  bucket
•  Key:  s3  key
③  S3からデータ取得

(*)  事前にBucketが作成されて
いる必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:SQS  -‐‑‒  sendMessage
var  AWS  =  require('aws-‐‑‒sdk');
var  fs  =  require('fs');
var  s3  =  new  AWS.SQS();

①

var  params  =  {
②
    QueueUrl:  'https://your_̲queue',
    MessageBody:  'Send  Message!'
}
sqs.sendMessage(params,  function(err,  data)  {
    if  (err)
        console.log(err);
    else
        console.log("Successfully  sent.");
})

①  SQSクライアント作成

②  queue_̲urlを指定、送りたい
メッセージを指定
③  sendMessage実⾏行行

(*)  事前にQueueが作成されて
いる必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:SQS  -‐‑‒  receiveMessage
var  AWS  =  require('aws-‐‑‒sdk');
var  fs  =  require('fs');
var  s3  =  new  AWS.SQS();

①

①  SQSクライアント作成

var  params  =  {
② ②  queue_̲urlを指定
    QueueUrl:  'https://your_̲queueʼ’
}
sqs.receiveMessage(params,  function(err,  data)   ③  receiveMessage実⾏行行
{
③
  
    if  (err)
        console.log(err);
    else
        console.log(data);
})
(*)  事前にQueueが作成されて
いる必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:DynamoDB  –  putItem
var  AWS  =  require('aws-‐‑‒sdk');
var  fs  =  require('fs');
var  ddb  =  new  AWS.DynamoDB();

①

var  params  =  {
    TableName:  'my_̲table',
    Item:  {
        'id':  {N:  ʻ‘100'},
        'timestamp':  {N:  '130699342'},
        'message':  {S:  'Good  Morning'}
    }
};

②

ddb.putItem(params,  function(err,  data)  {
    if  (err)
        console.log(err);
    else
        console.log(data);
});

③

①  DynamoDBクライアント
作成
②  パラメータ指定
-‐‑‒  :TableName:  テーブル名
-‐‑‒  :Item:  Putするアイテムを指
定
(例例)
    id  :  Hash  Key
    timestamp  :  Range  Key
    message  :  Attribute
③  Put実⾏行行

(*)  事前にTableが作成され
ている必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
サンプル:DynamoDB  –  getItem
var  AWS  =  require('aws-‐‑‒sdk');
var  fs  =  require('fs');
var  ddb  =  new  AWS.DynamoDB();

①

var  params  =  {
    TableName:  'my_̲table',
    Key:  {
        'id':  {N:  ʻ‘100'},
        'timestamp':  {N:  '130699342'}
    }
};

②

ddb.getItem(params,  function(err,  data)  {
    if  (err)
        console.log(err);
    else
        console.log(data);
});

③

①  DynamoDBクライアント作
成
②  パラメータ指定
-‐‑‒  :TableName:  テーブル名
-‐‑‒  :Item:  Putするアイテムを
指定
(例例)
    id  :  Hash  Key
    timestamp  :  Range  Key
    message  :  Attribute
③  Get実⾏行行

(*)  事前にTableが作成されてい
る必要があります。

© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
まとめ
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
今回のまとめ
!   AWSはプログラマブルなインフラ

•  ほぼすべてのサービスはマネジメントコンソールから利利⽤用でき
るが、⾃自動化しようと思ったらプログラムから扱うのが必須
•  ⾃自動化するとAWSは何倍も便便利利になる

!   AWSのサービスはHTTP/HTTPSでREST/SOAP形式の
APIをサポート。
例例えば
•  EC2  :  Start/Stop/Terminate
•  S3  :  GetObject/PutObject
•  DynamoDB  :  Get/Put

!   SDKはこれらのAPIを抽象化し、各⾔言語からの利利⽤用を⾮非
常に簡単にしてくれる
© 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
Q&A

68
© 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.

More Related Content

PPTX
今だから!Amazon CloudFront 徹底活用
PPTX
分散トレーシングAWS:X-Rayとの上手い付き合い方
PDF
AWSの課金体系
PDF
[AWSマイスターシリーズ] AWS Elastic Beanstalk
PDF
[JAWS DAYS 2019] Amazon DocumentDB(with MongoDB Compatibility)入門
PDF
AWS Black Belt Online Seminar コストの観点から見るアカウント管理
PDF
AWS EC2 Eメール制限解除 - 逆引き(rDNS)設定 申請手順
PDF
AWS Black Belt Online Seminar 2017 Amazon Aurora
今だから!Amazon CloudFront 徹底活用
分散トレーシングAWS:X-Rayとの上手い付き合い方
AWSの課金体系
[AWSマイスターシリーズ] AWS Elastic Beanstalk
[JAWS DAYS 2019] Amazon DocumentDB(with MongoDB Compatibility)入門
AWS Black Belt Online Seminar コストの観点から見るアカウント管理
AWS EC2 Eメール制限解除 - 逆引き(rDNS)設定 申請手順
AWS Black Belt Online Seminar 2017 Amazon Aurora

What's hot (20)

PDF
20170725 black belt_monitoring_on_aws
PDF
DevOps with Database on AWS
PDF
20210309 AWS Black Belt Online Seminar AWS Audit Manager
PDF
Usage Report(利用レポート)のダウンロード・開き方
PDF
Jmespathをもっと広めたい
PDF
Dockerクイックツアー
PPTX
backlogsでもCI/CDする夢を見る
PDF
Amazon Redshift パフォーマンスチューニングテクニックと最新アップデート
PDF
AWS Black Belt Techシリーズ Amazon WorkDocs / Amazon WorkMail
PDF
AWS Black Belt Online Seminar 2017 Amazon DynamoDB
PDF
20190424 AWS Black Belt Online Seminar Amazon Aurora MySQL
PPTX
20220409 AWS BLEA 開発にあたって検討したこと
PDF
Infrastructure as Code (IaC) 談義 2022
PDF
機密データとSaaSは共存しうるのか!?セキュリティー重視のユーザー層を取り込む為のネットワーク通信のアプローチ
PDF
20200826 AWS Black Belt Online Seminar AWS CloudFormation
PPTX
Awsをオンプレドメコンに連携させる
PDF
【JAWS DAYS 2016】ランサーズを支えるAurora
PDF
20210216 AWS Black Belt Online Seminar AWS Database Migration Service
PDF
AWS Black Belt Online Seminar 2016 AWS CloudFormation
PDF
AWSのログ管理ベストプラクティス
20170725 black belt_monitoring_on_aws
DevOps with Database on AWS
20210309 AWS Black Belt Online Seminar AWS Audit Manager
Usage Report(利用レポート)のダウンロード・開き方
Jmespathをもっと広めたい
Dockerクイックツアー
backlogsでもCI/CDする夢を見る
Amazon Redshift パフォーマンスチューニングテクニックと最新アップデート
AWS Black Belt Techシリーズ Amazon WorkDocs / Amazon WorkMail
AWS Black Belt Online Seminar 2017 Amazon DynamoDB
20190424 AWS Black Belt Online Seminar Amazon Aurora MySQL
20220409 AWS BLEA 開発にあたって検討したこと
Infrastructure as Code (IaC) 談義 2022
機密データとSaaSは共存しうるのか!?セキュリティー重視のユーザー層を取り込む為のネットワーク通信のアプローチ
20200826 AWS Black Belt Online Seminar AWS CloudFormation
Awsをオンプレドメコンに連携させる
【JAWS DAYS 2016】ランサーズを支えるAurora
20210216 AWS Black Belt Online Seminar AWS Database Migration Service
AWS Black Belt Online Seminar 2016 AWS CloudFormation
AWSのログ管理ベストプラクティス
Ad

Similar to [AWSマイスターシリーズ] AWS SDK for PHP / Ruby / boto(Python) / JavaScript in Node.js (20)

PDF
20120528 aws meister-reloaded-awssd-kforjava-public
PDF
[AWSマイスターシリーズ] AWS SDK for Java / .NET
PDF
[AWS Developers Meetup 2017] Developerのための ライブAWSウォークスルー 〜 AWS SDKの使い方 〜
PDF
AWS Black Belt Techシリーズ AWS SDK
PDF
AWS Black Belt Online Seminar AWSサービスを利用したアプリケーション開発を始めよう
PDF
[AWSマイスターシリーズ] AWS Client Side SDK -Android,iOS & JavaScript-
PDF
Programming AWS with Perl at YAPC::Asia 2013
PDF
AWS Black Belt Techシリーズ Amazon Elastic Compute Cloud (Amazon EC2)
PDF
Cloud Programing for beginner
PDF
AWS SDK for Android and iOS
PDF
AWS SDK for Haskell開発
PDF
AWSマイスターシリーズReloaded -AWS SDK for .NET-
PDF
AWS初心者向けWebinar AWSクラウドでのWindowsの実行
PDF
Aws tools for power shellをつかいこなそう
PPTX
Awsについて
PDF
Windows 開発者のための Dev&Ops on AWS
PDF
AWS Update 2011/10
PDF
AWS meets Android - "AWS SDK for Android"で開発を楽にしよう!
PDF
Awsビギナー向け資料 ec2 20111124
PDF
AWS初心者向けWebinar .NET開発者のためのAWS超入門
20120528 aws meister-reloaded-awssd-kforjava-public
[AWSマイスターシリーズ] AWS SDK for Java / .NET
[AWS Developers Meetup 2017] Developerのための ライブAWSウォークスルー 〜 AWS SDKの使い方 〜
AWS Black Belt Techシリーズ AWS SDK
AWS Black Belt Online Seminar AWSサービスを利用したアプリケーション開発を始めよう
[AWSマイスターシリーズ] AWS Client Side SDK -Android,iOS & JavaScript-
Programming AWS with Perl at YAPC::Asia 2013
AWS Black Belt Techシリーズ Amazon Elastic Compute Cloud (Amazon EC2)
Cloud Programing for beginner
AWS SDK for Android and iOS
AWS SDK for Haskell開発
AWSマイスターシリーズReloaded -AWS SDK for .NET-
AWS初心者向けWebinar AWSクラウドでのWindowsの実行
Aws tools for power shellをつかいこなそう
Awsについて
Windows 開発者のための Dev&Ops on AWS
AWS Update 2011/10
AWS meets Android - "AWS SDK for Android"で開発を楽にしよう!
Awsビギナー向け資料 ec2 20111124
AWS初心者向けWebinar .NET開発者のためのAWS超入門
Ad

More from Amazon Web Services Japan (20)

PDF
202205 AWS Black Belt Online Seminar Amazon VPC IP Address Manager (IPAM)
PDF
202205 AWS Black Belt Online Seminar Amazon FSx for OpenZFS
PDF
202204 AWS Black Belt Online Seminar AWS IoT Device Defender
PDF
202204 AWS Black Belt Online Seminar Amazon Connect を活用したオンコール対応の実現
PDF
202204 AWS Black Belt Online Seminar Amazon Connect Salesforce連携(第1回 CTI Adap...
PDF
Amazon Game Tech Night #25 ゲーム業界向け機械学習最新状況アップデート
PDF
202202 AWS Black Belt Online Seminar AWS Managed Rules for AWS WAF の活用
PDF
202203 AWS Black Belt Online Seminar Amazon Connect Tasks.pdf
PDF
SaaS テナント毎のコストを把握するための「AWS Application Cost Profiler」のご紹介
PDF
Amazon QuickSight の組み込み方法をちょっぴりDD
PDF
マルチテナント化で知っておきたいデータベースのこと
PDF
パッケージソフトウェアを簡単にSaaS化!?既存の資産を使ったSaaS化手法のご紹介
PDF
202202 AWS Black Belt Online Seminar Amazon Connect Customer Profiles
PDF
Amazon Game Tech Night #24 KPIダッシュボードを最速で用意するために
PDF
202202 AWS Black Belt Online Seminar AWS SaaS Boost で始めるSaaS開発⼊⾨
PPTX
[20220126] JAWS-UG 2022初頭までに葬ったAWSアンチパターン大紹介
PDF
202111 AWS Black Belt Online Seminar AWSで構築するSmart Mirrorのご紹介
PDF
202201 AWS Black Belt Online Seminar Apache Spark Performnace Tuning for AWS ...
PDF
202112 AWS Black Belt Online Seminar 店内の「今」をお届けする小売業向けリアルタイム配信基盤のレシピ
PDF
20211209 Ops-JAWS Re invent2021re-cap-cloud operations
202205 AWS Black Belt Online Seminar Amazon VPC IP Address Manager (IPAM)
202205 AWS Black Belt Online Seminar Amazon FSx for OpenZFS
202204 AWS Black Belt Online Seminar AWS IoT Device Defender
202204 AWS Black Belt Online Seminar Amazon Connect を活用したオンコール対応の実現
202204 AWS Black Belt Online Seminar Amazon Connect Salesforce連携(第1回 CTI Adap...
Amazon Game Tech Night #25 ゲーム業界向け機械学習最新状況アップデート
202202 AWS Black Belt Online Seminar AWS Managed Rules for AWS WAF の活用
202203 AWS Black Belt Online Seminar Amazon Connect Tasks.pdf
SaaS テナント毎のコストを把握するための「AWS Application Cost Profiler」のご紹介
Amazon QuickSight の組み込み方法をちょっぴりDD
マルチテナント化で知っておきたいデータベースのこと
パッケージソフトウェアを簡単にSaaS化!?既存の資産を使ったSaaS化手法のご紹介
202202 AWS Black Belt Online Seminar Amazon Connect Customer Profiles
Amazon Game Tech Night #24 KPIダッシュボードを最速で用意するために
202202 AWS Black Belt Online Seminar AWS SaaS Boost で始めるSaaS開発⼊⾨
[20220126] JAWS-UG 2022初頭までに葬ったAWSアンチパターン大紹介
202111 AWS Black Belt Online Seminar AWSで構築するSmart Mirrorのご紹介
202201 AWS Black Belt Online Seminar Apache Spark Performnace Tuning for AWS ...
202112 AWS Black Belt Online Seminar 店内の「今」をお届けする小売業向けリアルタイム配信基盤のレシピ
20211209 Ops-JAWS Re invent2021re-cap-cloud operations

[AWSマイスターシリーズ] AWS SDK for PHP / Ruby / boto(Python) / JavaScript in Node.js

  • 1. re:G ene rate AWS  マイスターシリーズ   AWS  SDK  for  PHP  & AWS  SDK  for  Ruby  & boto(Python)  & JavaScript  in  Node.js 2013.12.20 アマゾン  データ  サービス  ジャパン株式会社 ソリューションアーキテクト 今井  榎並  蒋 © 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 2. ウェビナー(Webセミナー)へようこそ! !   参加者は、⾃自動的にミュートになっています !   質問を投げることができます! §  GoToWebinarの仕組みを使って、書き込んでください §  ただし環境によっては、⽇日本語の直接⼊入⼒力力ができないので、 お⼿手数ですが、テキストエディタ等に打ち込んでから、 コピペしてください §  最後のQ&Aの時間で、できるだけ回答させて頂きます §  書き込んだ質問は、主催者にしか⾒見見えません !   Twitterのハッシュタグは#jawsugでどうぞ 2 © 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 3. Webセミナー 週刊AWSマイスターシリーズ  re:Generate! !   AWSマイスターシリーズ §  12⽉月4⽇日 AWS  re:Invent  アップデート振り返り §  12⽉月11⽇日 AWS  SDK  for  Java/.Net §  申し込みサイト §  http://aws.amazon.com/jp/event_̲schedule/ §  過去資料料集 §  http://aws.amazon.com/jp/aws-‐‑‒meister/ 3 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 4. Agenda !   AWS  SDK  概要 !   AWS  SDK  for  PHP ! boto(Python) !   AWS  SDK  for  Ruby !   AWS  SDK  for  JavaScript  in  Node.js !   まとめ 4 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 5. AWS  SDK概要 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 6. AWS  SDKの重要性 !   AWSはプログラマブルなインフラ •  プログラムから扱うためにはSDKが必要 •  ほぼすべてのサービスはマネジメントコンソールから利利⽤用でき るが、⾃自動化しようと思ったらプログラムから扱うのが必須 •  ⾃自動化するとAWSは何倍も便便利利になる !   代表的な⽤用途 •  インフラ構築/運⽤用の⾃自動化 •  EC2やRDSを上げたり下げたり、CloudFormationでスタックをデ プロイしたり •  アプリケーション的なサービスの利利⽤用 •  S3にデータをアップしたり、DynamoDBやSQSにデータ⼊入れたり 出したり © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 7. AWS  SDK !   AWSのサービスはHTTP/HTTPSでREST/SOAP形式の APIをサポート。 例例えば •  EC2  :  Start/Stop/Terminate •  S3  :  GetObject/PutObject •  DynamoDB  :  Get/Put !   SDKはこれらのAPIを抽象化し、各⾔言語からの利利⽤用を⾮非 常に簡単にしてくれる Your  code AWS  SDK API 7 EC2 S3 その他サービス © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 8. AWS  SDK !   下記の⾔言語/環境で提供中 Java Python JavaScript in  nodeJS PHP JavaScript in  the  Browser .NET iOS Ruby Android © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 9. AWS  SDK !   今回は下記についてお話します。 Java Python JavaScript in  Node.js PHP JavaScript in  the  Browser .NET iOS Ruby Android © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 10. AWS  SDKの使い⽅方 !   必要な⾔言語のSDKをインストール •  ⾔言語ごとにインストール⽅方法が異異なります。このあとの章で⾔言語ご とに解説します。 !   Credential(AWS  APIの認証情報)を⽤用意する !   ファクトリーメソッドを利利⽤用してサービス(例例えばS3)のクラ イアントオブジェクトを⽣生成 •  このときにCredentialを渡す !   クライアントオブジェクトのメソッドを使ってオペレーショ ン(例例えばPutObject) !   ⾔言語によってはより⾼高度度に抽象化されているSDKもあります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 11. Credentialの取り扱い !   Credentialの取り扱いについてはいくつかのやり⽅方があ りますが、IAM  Roleもしくは環境変数を使うのがオス スメです。PHPを例例にそれぞれの実装⽅方法をご案内しま す。 !   コード内に直接埋め込むパターン(コード内に Credentialが⼊入り込んでしまうのでやっちゃダメなパ ターン) <?php $s3client  =  S3Client::factory(array( 'key' => 'your-‐‑‒aws-‐‑‒access-‐‑‒key-‐‑‒id', 'secretʻ‘ => 'your-‐‑‒aws-‐‑‒secret-‐‑‒access-‐‑‒key', ));     © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 12. Credentialの取り扱い ! configファイルでCredentialを設定するパターン •  PHPとbotoではこの⽅方法が提供されている •  これもCredentialがファイル化されるので良良くないパターン $  vim  aws-‐‑‒config.php <?php return  array(        'includes'  =>  array('_̲aws'),        'services'  =>  array(                'default_̲settings'  =>  array(                        'params'  =>  array(                                'key'        =>  'your-‐‑‒access-‐‑‒key-‐‑‒id',                                'secret'  =>  'your-‐‑‒secret-‐‑‒access-‐‑‒key',)                )        ) ); $  vim  sample.php <?php $aws  =  Aws::factory(ʻ‘aws-‐‑‒config.php'); $s3client  =  $aws-‐‑‒>get('s3'); © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 13. Credentialの取り扱い !   環境変数でCredentialを設定するパターン •  AWS_̲ACCESS_̲KEY_̲ID,  AWS_̲SECRET_̲KEYという環境変数 を実⾏行行ユーザーで設定しておくことによってコードから Credentialを追い出せる $  export  AWS_̲ACCESS_̲KEY_̲ID="your-‐‑‒aws-‐‑‒access-‐‑‒key-‐‑‒id" $  export  AWS_̲SECRET_̲KEY="your-‐‑‒aws-‐‑‒secret-‐‑‒access-‐‑‒key” $vim  sample.php <?php $s3client  =  S3Client::factory();     © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 14. Credentialの取り扱い !   IAM  RoleでCredentialを設定するパターン •  コードが動くのがEC2上であれば、IAM  Roleを使うことによっ て、EC2⾃自体にAWS  APIへのアクセス権限を付与できる。よっ てコードや環境変数にCredentialを持たせずに済む。 例例えばS3フルアクセスの権限を持った IAM  Roleを作成して・・・ EC2にそのRoleを 割り当てると・・・ © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 15. Credentialの取り扱い !   IAM  RoleでCredentialを設定するパターン •  前のページのようにS3アクセス権限を持ったRoleをEC2に割り 当てると、下記を書くだけで利利⽤用可能。 $vim  sample.php <?php $s3client  =  S3Client::factory();     •  これは、SDKが内部でSTS(Security  Token  Service)を利利⽤用し ており、AccessKey,SecretAccessKey,Tokenを⾃自動的に © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 16. AWS  SDK  for  PHP © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 17. AWS  SDK  for  PHP ! ! ! ! ! !   Amazon提供のAWS開発⽤用のPHP向けSDK   現在は、Version2 http://aws.amazon.com/jp/sdkforphp/   環境:PHP5.3.3以降降   Guzzle  HTTP  Client  framework上で構築されている   依存Extension •  cURL © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 18. 操作可能サービス Direct Connect Elasticache IAM EC2 SimpleDB OpsWorks ELB S3 Elastic Transcoder Auto Scaling Glacier SQS EMR CloudFront SNS Route53 Storage Gateway SES VPC Import/Export SWF DynamoDB Elastic Beanstalk CloudSearch RDS Cloud Formation Redshift Cloud Watch © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 19. 利利⽤用⽅方法 !   Composerによるインストール(推奨) •  http://docs.aws.amazon.com/aws-‐‑‒sdk-‐‑‒php/guide/latest/ installation.html#installing-‐‑‒via-‐‑‒composer !   Pharよるインストール •  http://docs.aws.amazon.com/aws-‐‑‒sdk-‐‑‒php/guide/latest/ installation.html#installing-‐‑‒via-‐‑‒phar !   Zipファイルからインストール •  http://docs.aws.amazon.com/aws-‐‑‒sdk-‐‑‒php/guide/latest/ installation.html#installing-‐‑‒via-‐‑‒zip !   PEARによるインストール •  http://docs.aws.amazon.com/aws-‐‑‒sdk-‐‑‒php/guide/latest/ installation.html#installing-‐‑‒via-‐‑‒pear © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 20. 初期設定 !   インストール⽅方法により以下のとおり異異なります インストール方法 インクルード Composer require '/path/to/vendor/autoload.php'; Phar require '/path/to/aws.phar'; Zip require '/path/to/aws-autoloader.php'; PEAR 'AWSSDKforPHP/aws.phar'; !   コンフィグレーションファイルの設定 •  クレデンシャル情報などを設定可能 •  factory()の引数として指定することも可能 •  ʻ‘src/Aws/Common/Resources/aws-‐‑‒config.phpʼ’  を参考に追加 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 21. サンプル:S3  -‐‑‒  putObject API仕様:  http://docs.aws.amazon.com/aws-‐‑‒sdk-‐‑‒php/latest/class-‐‑‒ Aws.S3.S3Client.html#_̲putObject <?php require  '/path/to/vendor/autoload.php'; use  AwsCommonAws; use  AwsS3ExceptionS3Exception; try  {        $client  =  S3Client::factory; ①        $bucket  =  ʼ’your_̲bucket';        $file  =  'test.txt';        $result  =  $client-‐‑‒>putObject(array( 'Bucket'  =>  $bucket, 'Key'  =>  $file, 'Body'  =>  fopen($file,  'r'),        ));        //  結果表⽰示        var_̲dump($result); }  catch  (S3Exception  $e)  {        echo  '***  Error  ***'  .  "n";        echo  $e-‐‑‒>getMessage(); } ①  S3クライアント作成 ②  putObject実⾏行行 ②   Bucketを指定、Keyは、ファ イル名、Bodyにアップロードす るファイルストリームを指定 (*)  事前にBucketが作成されて いる必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 22. サンプル:S3  -‐‑‒  getObject API仕様:  http://docs.aws.amazon.com/aws-‐‑‒sdk-‐‑‒php/latest/class-‐‑‒ Aws.S3.S3Client.html#_̲getObject <?php require  '/path/to/vendor/autoload.php'; use  AwsCommonAws; use  AwsS3ExceptionS3Exception; try  {        $client  =  S3Client::factory; ①        $bucket  =  ʼ’your_̲bucket';        $file  =  'test.txt';        $result  =  $client-‐‑‒>getObject(array( 'Bucket'  =>  $bucket, 'Key'  =>  $file, ));        //  結果表⽰示        echo  $result['Body']; ③ }  catch  (S3Exception  $e)  {        echo  '***  Error  ***'  .  "n";        echo  $e-‐‑‒>getMessage(); } ①  S3クライアント作成 ②  getObject実⾏行行 ②   Bucketを指定、Keyは、ファイル 名 ③  取得したコンテンツを表⽰示 (*)  事前にBucketが作成されて いる必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 23. サンプル:SQS  -‐‑‒  sendMessage API仕様:  http://docs.aws.amazon.com/aws-‐‑‒sdk-‐‑‒php/latest/class-‐‑‒ Aws.Sqs.SqsClient.html#_̲sendMessage <?php require  '/path/to/vendor/autoload.php'; use  AwsCommonAws; use  AwsSqsExceptionSqsException; try  {        $client  =  SqsClient::factory;         ①        $queueUrl  =  'https://YourQueue';        $result  =  $client-‐‑‒>sendMessage(array( ② 'QueueUrl'  =>  $queueUrl, 'MessageBody'  =>  'Send  Message!',        ));        //  結果表⽰示        var_̲dump($result); }  catch  (SqsException  $e)  {        echo  '***  Error  ***'  .  "n";        echo  $e-‐‑‒>getMessage(); } ①  SQSクライアント作成 ②  sendMessage実⾏行行   QueueURLを指定、送りたい メッセージを指定 (*)  事前にQueueが作成されて いる必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 24. サンプル:SQS  -‐‑‒  receiveMessage API仕様:  http://docs.aws.amazon.com/aws-‐‑‒sdk-‐‑‒php/latest/class-‐‑‒ Aws.Sqs.SqsClient.html#_̲receiveMessage <?php require  '/path/to/vendor/autoload.php'; use  AwsCommonAws; use  AwsSqsExceptionSqsException; ①  コンフィグを指定してインスタン ス作成 ②  SQSクライアント作成 try  {        $client  =  SqsClient::factory; ① ③  receiveMessage実⾏行行        $queueUrl  =  'https://YourQueue';        $result  =  $client-‐‑‒>receiveMessage(array( ②   QueueURLを指定 'QueueUrl'  =>  $queueUrl,        ));        //  結果表⽰示        var_̲dump($result); }  catch  (SqsException  $e)  {        echo  '***  Error  ***'  .  "n";        echo  $e-‐‑‒>getMessage(); } (*)  事前にQueueが作成されて いる必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 25. サンプル:DynamoDB  -‐‑‒  putItem API仕様:  http://docs.aws.amazon.com/aws-‐‑‒sdk-‐‑‒php/latest/class-‐‑‒ Aws.DynamoDb.DynamoDbClient.html#_̲putItem <?php require  '/path/to/vendor/autoload.php'; use  AwsCommonAws; use  AwsDynamoDbExceptionDynamoDbException; try  {        $client  =  DynamoDbClient::factory; ①        $result  =  $client-‐‑‒>putItem(array(                'TableName'  =>  ʼ’table_̲name',                'Item'  =>  $client-‐‑‒>formatAttributes(array(                        ʼ’id'                =>  100,                        'timestamp'  =>  130699342,                        'message'            =>  'Good  Morning.',                )),        ));                //  結果表⽰示        var_̲dump($result); }  catch  (DynamoDbException  $e)  {        echo  '***  Error  ***'  .  "n";        echo  $e-‐‑‒>getMessage(); } ①  DynamoDBクライアント作 成 ② ②  putItem実⾏行行 -‐‑‒  TableName:  テーブル名 -‐‑‒  Item:  Putするアイテムを 指定 (例例)    id  :  Hash  Key    timestamp  :  Range  Key    message  :  Attribute (*)  事前にTableが作成されて いる必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 26. サンプル:DynamoDB  -‐‑‒  getItem API仕様:  http://docs.aws.amazon.com/aws-‐‑‒sdk-‐‑‒php/latest/class-‐‑‒Aws.DynamoDb.DynamoDbClient.html#_̲getItem <?php require  '/path/to/vendor/autoload.php'; use  AwsCommonAws; use  AwsDynamoDbExceptionDynamoDbException; try  {        $client  =  DynamoDbClient::factory; ①        $result  =  $client-‐‑‒>getItem(array( ②                'ConsistentRead'  =>  true,                'TableName'  =>  'table_̲name',                'Key'  =>  array( ʼ’id'  =>  array('N'  =>  '100'), 'timestamp'  =>  array('N'  =>  '130699342') )        ));        //  結果表⽰示        var_̲dump($result); }  catch  (DynamoDbException  $e)  {        echo  '***  Error  ***'  .  "n";        echo  $e-‐‑‒>getMessage(); } ①  DynamoDBクライアント作 成 ②  getItem実⾏行行 -‐‑‒  TableName:  テーブル名 -‐‑‒  Key:GetするアイテムのKey を指定 (例例)    id  :  Hash  Key    timestamp  :  Range  Key (*)  事前にTableが作成されてい る必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 27. 活⽤用法 !   PHPアプリのバックエンドストレージ呼び出し •  DynamoDB,SQS,S3を簡単に呼び出し可能 !   PHPベースのアプリケーションの拡張 •  WordpressやEC-‐‑‒CubeなどのPHPベースのアプリケーションと AWSの連携 !   独⾃自の管理理⽤用Webインターフェースの作成 •  定型作業を簡略略化したり、管理理を容易易にするための独⾃自Webイ ンターフェースの作成 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 28. PHP版SDKの便便利利なクラス ! DynamoDBSessionHandler ! DynamoDBを使った、HTTPのセッション共有が可能 EC2 ELB EC2 EC2 DynamoDB 速い 消えない 管理不要 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 29. DynamoDBSessionHandlerの利利⽤用 <?php require  '/home/ec2-‐‑‒user/vendor/autoload.php'; use  AwsCommonAws; use  AwsDynamoDbExceptionDynamoDbException; use  AwsDynamoDbDynamoDbClient; use  AwsDynamoDbSessionSessionHandler; $aws  =  Aws::factory(ʼ’aws-‐‑‒config.php'); $client  =  $aws-‐‑‒>get('dynamodb'); try  {        $sessionHandler  =  SessionHandler::factory(array(                'dynamodb_̲client'  =>  $client,                'table_̲name'            =>  'sessions',        ));        $sessionHandler-‐‑‒>register(); }  catch  (DynamoDbException  $e)  {        echo  '***  Error  ***'  .  "n";        echo  $e-‐‑‒>getMessage(); } session_̲start(); //セッションにデータ登録 $_̲SESSION['username']  =  'jeremy'; $_̲SESSION['role']  =  'admin'; session_̲commit(); © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 30. boto(Python) © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 31. botoとは !   Python⽤用のAWS  SDKのデファクトスタンダード !   最新版は2.19.0  (2013年年12⽉月現在) !   Python  2.6.6,  2.7.3  on  Mac  OSX  and  Ubuntu  Maverick   で動作確認している。 !   Python  2.5での互換性もなるべく考慮しているが保証外 !   Python  3.x版はDeveloper  Preview中 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 32. 操作可能サービス Direct Connect Elasticache IAM EC2 SimpleDB OpsWorks ELB S3 Elastic Transcoder Auto Scaling Glacier SQS EMR CloudFront SNS Route53 Storage Gateway SES VPC Import/Export SWF DynamoDB Elastic Beanstalk CloudSearch RDS Cloud Formation Data Pipeline Redshift Cloud Watch CloudTrail FPS EBS © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 33. botoに関するリソース !   ソースコードリポジトリ •  https://github.com/boto/boto ! PyPI •  http://pypi.python.org/pypi/boto !   オンラインドキュメント •  http://docs.pythonboto.org/ !   IRC •  http://webchat.freenode.net/?channels=boto ©  2012  Amazon.com,  Inc.  and  its  affiliates.    All  rights  reserved.    May  not  be  copied,  modified  or  distributed  in  whole  or  in  part  without  the  express  consent  of   © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc. Amazon.com,  Inc.
  • 34. インストール  $  pip  install  boto ©  2012  Amazon.com,  Inc.  and  its  affiliates.    All  rights  reserved.    May  not  be  copied,  modified  or  distributed  in  whole  or  in  part  without  the  express  consent  of   © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc. Amazon.com,  Inc.
  • 35. 初期設定 !   設定ファイルの作成 !   /etc/boto.cfg  -‐‑‒  グローバルな設定を記述 !   ~∼/.boto  ユーザー毎の設定を記述 [Boto] debug  =  0 num_̲retries  =  10 [DynamoDB] region  =  ap-‐‑‒northeast-‐‑‒1 ©  2012  Amazon.com,  Inc.  and  its  affiliates.    All  rights  reserved.    May  not  be  copied,  modified  or  distributed  in  whole  or  in  part  without  the  express  consent  of   © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc. Amazon.com,  Inc.
  • 36. サンプル:S3  –  set_̲contents_̲from_̲string >>>  from  boto.s3.connection  import  S3Connection >>>  conn  =  S3Connection() >>>  bucket_̲name  =  ”yourBucket" >>>  bucket  =    conn.get_̲bucket(bucket_̲name) >>>  from  boto.s3.key  import  Key >>>  k  =  Key(bucket) >>>  k.key  =  "test.txt" >>>  k.set_̲contents_̲from_̲string("Hello  World!") (*)  事前にBucketが作成されて いる必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 37. サンプル:S3  –  get_̲contents_̲as_̲string >>>  from  boto.s3.connection  import  S3Connection >>>  conn  =  S3Connection() >>>  bucket_̲name  =  "yourBucket" >>>  bucket  =    conn.get_̲bucket(bucket_̲name) >>>  from  boto.s3.key  import  Key >>>  k  =  Key(bucket) >>>  k.key  =  "test.txt" >>>  k.get_̲contents_̲as_̲string() (*)  事前にBucketが作成されて いる必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 38. サンプル:SQS  –  write >>>  import  boto.sqs >>>  conn  =  boto.sqs.connect_̲to_̲region("ap-‐‑‒northeast-‐‑‒1") >>>  q  =  conn.get_̲queue('YourQueue') >>>  from  boto.sqs.message  import  Message >>>  m  =  Message() >>>  m.set_̲body('Hello  World!!') >>>  q.write(m) (*)  事前にQueueが作成されて いる必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 39. サンプル:SQS  –  get_̲messages >>>  import  boto.sqs >>>  conn  =  boto.sqs.connect_̲to_̲region("ap-‐‑‒northeast-‐‑‒1") >>>  q  =  conn.get_̲queue('YourQueue') >>>  rs  =  q.get_̲messages() >>>  m  =  rs[0] >>>  m.get_̲body() 'Hello  World!!' (*)  事前にQueueが作成されて いる必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 40. サンプル:DynamoDB  –  putItem >>>  from  boto.dynamodb2.items  import  Item >>>  from  boto.dynamodb2.table  import  Table >>>  table  =  Table('table_̲name') >>>  item  =  Item(table  ,  data={ ...  ʼ’id':  100, ...  'timestamp':  130699342, ...  'message'  :  'Good  Morning.', ...  }) >>>  item.save() True (*)  事前にTableが作成されてい る必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 41. サンプル:DynamoDB  –  getItem >>>  from  boto.dynamodb2.table  import  Table >>>  table=  Table('table_̲name') >>>  item  =  table.get_̲item(id=100,timestamp=130699342) >>>  print  item['message'] Good  Morning. (*)  事前にTableが作成されてい る必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 42. その他Tips !   AWSコマンドラインインタフェース(CLI)もbotoを⼀一部 利利⽤用している © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 43. AWS  SDK  for  Ruby © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 44. AWS  SDK  for  Rubyとは !   Amazon提供のAWS開発⽤用のRuby向けSDK !   現在のバージョン •  Version  1  (stable) •  Version  2  (developer  preview) ! http://aws.amazon.com/sdkforruby/   !   環境:Ruby  1.8.7  以降降 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 45. 操作可能サービス Direct Connect Elasticache IAM EC2 SimpleDB OpsWorks ELB S3 Elastic Transcoder Auto Scaling Glacier SQS EMR CloudFront SNS Route53 Storage Gateway SES VPC Import/Export SWF DynamoDB Elastic Beanstalk CloudSearch RDS Cloud Formation Data Pipeline Redshift Cloud Watch © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 46. 利利⽤用⽅方法 !   Ruby環境のセットアップ •  Rbenvを利利⽤用する •  https://github.com/sstephenson/rbenv   •  RVMを利利⽤用する •  https://rvm.io/   ! aws-‐‑‒sdk  gemをインストール $  gem  install  aws-‐‑‒sdk © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 47. 設定の初期化 !   デフォルトのリージョンをセッティング require  'awsʼ’ AWS.config({ :region  =>  ʻ‘YOUR_̲DEFAULT_̲REGIONʼ’ }) © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 48. サンプル:S3  -‐‑‒  write API仕様:  http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/S3/S3Object.html#write-‐‑‒ instance_̲method #!/usr/bin/env  ruby require  'aws' AWS.config({    :access_̲key_̲id  =>   ENV['AWS_̲ACCESS_̲KEY_̲ID'],    :secret_̲access_̲key  =>   ENV['AWS_̲SECRET_̲ACCESS_̲KEY'],    :region  =>  ENV['AWS_̲REGION'] }) ①  コンフィグを指定 ① ②  S3クライアント作成し、 bucketやkeyを指定 ③  putするfile名を指定し、s3に put ② s3  =  AWS::S3.new bucket  =  s3.buckets['your-‐‑‒bucket'] obj  =  bucket.objects['key'] obj.write(Pathname.new('/path/to/file.txt')) ③ (*)  事前にBucketが作成されて いる必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 49. サンプル:S3  -‐‑‒  read API仕様:  http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/S3/S3Object.html#read-‐‑‒ instance_̲method #!/usr/bin/env  ruby require  'aws' ①  コンフィグを指定 AWS.config({    :access_̲key_̲id  =>   ENV['AWS_̲ACCESS_̲KEY_̲ID'],    :secret_̲access_̲key  =>   ENV['AWS_̲SECRET_̲ACCESS_̲KEY'],    :region  =>  ENV['AWS_̲REGION'] }) ① s3  =  AWS::S3.new bucket  =  s3.buckets['your-‐‑‒bucket'] obj  =  bucket.objects['key'] ② obj.read  do  |chunk|    puts  chunk    #=>  data  of  s3://your-‐‑‒bucket/key end ③ ②  S3クライアント作成し、 bucketやkeyを指定 ③  s3にあるobjectのデータを取 得 (*)  事前にBucketが作成されて いる必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 50. サンプル:SQS  –  send_̲message API仕様:  http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/SQS/ Client.html#send_̲message-‐‑‒instance_̲method #!/usr/bin/env  ruby require  'aws' require  'pp' ①  コンフィグを指定 AWS.config({    :access_̲key_̲id  =>   ENV['AWS_̲ACCESS_̲KEY_̲ID'],    :secret_̲access_̲key  =>   ENV['AWS_̲SECRET_̲ACCESS_̲KEY'],    :region  =>  ENV['AWS_̲REGION'] }) ① sqs  =  AWS::SQS.new ② queue_̲url  =  'https://your_̲queue' result  =  sqs.client.send_̲message({    :queue_̲url        =>  queue_̲url,    :message_̲body  =>  'Send  Message!' }) pp  result ②  SQSクライアント作成 ③ ③  send_̲message実⾏行行   queue_̲urlを指定、送りたい メッセージを指定 (*)  事前にQueueが作成されて いる必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 51. サンプル:SQS  –  receive_̲message API仕様:  http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/SQS/ Client.html#receive_̲message-‐‑‒instance_̲method #!/usr/bin/env  ruby require  'aws' require  'pp' ①  コンフィグを指定 AWS.config({    :access_̲key_̲id  =>   ENV['AWS_̲ACCESS_̲KEY_̲ID'],    :secret_̲access_̲key  =>   ENV['AWS_̲SECRET_̲ACCESS_̲KEY'],    :region  =>  ENV['AWS_̲REGION'] }) ① sqs  =  AWS::SQS.new ② queue_̲url  =  'https://your_̲queue' result  =  sqs.client.receive_̲message({    :queue_̲url        =>  queue_̲url }) pp  result ②  SQSクライアント作成 ③ ③  receive_̲message実⾏行行   queue_̲urlを指定 (*)  事前にQueueが作成されて いる必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 52. サンプル:DynamoDB  –  put_̲item API仕様:  http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/DynamoDB/Client.html#put_̲item-‐‑‒ instance_̲method #!/usr/bin/env  ruby require  'aws' require  'pp' ①  コンフィグを指定 AWS.config({ ①    :access_̲key_̲id  =>  ENV['AWS_̲ACCESS_̲KEY_̲ID'],    :secret_̲access_̲key  =>  ENV['AWS_̲SECRET_̲ACCESS_̲KEY'],    :region  =>  ENV['AWS_̲REGION'] }) client  =  AWS::DynamoDB::Client.new result  =  client.put_̲item({    :table_̲name  =>  'table_̲nameʼ’,    :item  =>  {        'id'                              =>  {:n  =>  '100'},        'timestamp'  =>  {:n  =>  '130699342'},        'message'        =>  {:s  =>  'Good  Morning'}    } }) pp  result #=>  {"ConsumedCapacityUnits"=>1.0} ② ③ ②  DynamoDBクライアント作成 ③  put_̲item実⾏行行 -‐‑‒  :table_̲name:  テーブル名 -‐‑‒  :item:  Putするアイテムを指 定 (例例)    id  :  Hash  Key    timestamp  :  Range  Key    message  :  Attribute (*)  事前にTableが作成されてい る必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 53. サンプル:DynamoDB  –  get_̲item API仕様:  http://docs.aws.amazon.com/AWSRubySDK/latest/AWS/DynamoDB/Client.html#get_̲item-‐‑‒ instance_̲method #!/usr/bin/env  ruby require  'aws' require  'pp' ①  コンフィグを指定 AWS.config({ ①    :access_̲key_̲id  =>  ENV['AWS_̲ACCESS_̲KEY_̲ID'],    :secret_̲access_̲key  =>  ENV['AWS_̲SECRET_̲ACCESS_̲KEY'],    :region  =>  ENV['AWS_̲REGION'] }) client  =  AWS::DynamoDB::Client.new result  =  client.get_̲item({    :consistent_̲read  =>  true,    :table_̲name  =>  'table_̲name',    :key  =>  {        :hash_̲key_̲element  =>  {:n  =>  '100'},        :range_̲key_̲element  =>  {:n  =>  '130699342'}    } }) pp  result #=>  {"Item"=>{"id"=>{"N"=>"100"},   "message"=>{"S"=>"Good  Morning"},   "timestamp"=>{"N"=>"130699342"}},   "ConsumedCapacityUnits"=>1.0} ② ③ ②  DynamoDBクライアント 作成 ③  get_̲item実⾏行行 -‐‑‒  :table_̲name  テーブル名 -‐‑‒  :consistent_̲read  ⼀一貫性 -‐‑‒  :key  GutするKeyを指定 (例例)    id  :  Hash  Key    timestamp  :  Range  Key (*)  事前にTableが作成され ている必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 54. その他Tips !   新しいRuby  SDK  (version  2) •  •  •  •  Developer  Preview バリデーション、ドキュメント、拡張性など改善 プラグイン⽅方式をとっている Ruby  1.9以降降が必須 !   以下のように利利⽤用可能 $  gem  install  aws-‐‑‒sdk-‐‑‒core AWS::DynamoDB.new(api_̲version:  ʻ‘2012-‐‑‒08-‐‑‒10ʼ’) Re:Inventで発表された新Ruby  SDKのスライドはおすすめ © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 55. その他Tips !   RailsのセッションをDynamoDBに保存 •  スケーラブルかつ耐障害に優れたセッションストア •  Rails  3.x  か  4.x  から利利⽤用可能 •  Rackベースのアプリも利利⽤用可能 !   以下の⼿手順で利利⽤用可能 gem  ʻ‘aws-‐‑‒sessionstore-‐‑‒dynamodbʼ’ $  bundle  install 参考:https://github.com/aws/aws-‐‑‒sessionstore-‐‑‒dynamodb-‐‑‒ruby   © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 56. AWS  SDK  for  JavaScript in  Node.js © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 57. AWS  SDK  for  JavaScript  in  Node.jsとは !   Amazon提供のAWS開発⽤用のnode.js環境で動くJavaScript 向けSDK •  http://aws.amazon.com/sdkfornodejs/ © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 58. 操作可能サービス Direct Connect Elasticache IAM EC2 SimpleDB OpsWorks ELB S3 Elastic Transcoder Auto Scaling Glacier SQS EMR CloudFront SNS Route53 Storage Gateway SES VPC Import/Export SWF DynamoDB Elastic Beanstalk CloudSearch RDS Cloud Formation Data Pipeline Redshift Cloud Watch © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 59. 利利⽤用⽅方法 ! Node.js環境のセットアップ •  OSのパッケージマネージャーでnpmをインストール •  Mac  の場合: $  brew  install  npm ! aws-‐‑‒sdk  packageをインストール $  npm  install  aws-‐‑‒sdk © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 60. サンプル:S3  -‐‑‒  putObject var  AWS  =  require('aws-‐‑‒sdk'); var  fs  =  require('fs'); var  s3  =  new  AWS.S3(); ① var  bodyStream  =  fs.createReadStream(  '/path/ ② to/file.txt'  ); var  params  =  {    Bucket:  'your-‐‑‒bucket',    Key:  'key',    Body:  bodyStream }; s3.putObject(params,  function(err,  data)  {    if  (err)        console.log(err);    else        console.log("Successfully  uploaded."); }); ①  S3クライアント作成 ②  put時の引数 •  Bucket:  s3  bucket •  Key:  s3  key •  Body:  実際のデータ ③ ③  S3へPUT (*)  事前にBucketが作成されて いる必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 61. サンプル:S3  -‐‑‒  getObject var  AWS  =  require('aws-‐‑‒sdk'); var  fs  =  require('fs'); var  s3  =  new  AWS.S3(); ① var  params  =  {    Bucket:  ʼ’your-‐‑‒bucket',    Key:  ʼ’key' }; ② s3.getObject(params,  function(err,  data)  {    if  (err)        console.log(err);    else        console.log(data.Body.toString()); }); ③ ①  S3クライアント作成 ②  gut時の引数 •  Bucket:  s3  bucket •  Key:  s3  key ③  S3からデータ取得 (*)  事前にBucketが作成されて いる必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 62. サンプル:SQS  -‐‑‒  sendMessage var  AWS  =  require('aws-‐‑‒sdk'); var  fs  =  require('fs'); var  s3  =  new  AWS.SQS(); ① var  params  =  { ②    QueueUrl:  'https://your_̲queue',    MessageBody:  'Send  Message!' } sqs.sendMessage(params,  function(err,  data)  {    if  (err)        console.log(err);    else        console.log("Successfully  sent."); }) ①  SQSクライアント作成 ②  queue_̲urlを指定、送りたい メッセージを指定 ③  sendMessage実⾏行行 (*)  事前にQueueが作成されて いる必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 63. サンプル:SQS  -‐‑‒  receiveMessage var  AWS  =  require('aws-‐‑‒sdk'); var  fs  =  require('fs'); var  s3  =  new  AWS.SQS(); ① ①  SQSクライアント作成 var  params  =  { ② ②  queue_̲urlを指定    QueueUrl:  'https://your_̲queueʼ’ } sqs.receiveMessage(params,  function(err,  data)   ③  receiveMessage実⾏行行 { ③       if  (err)        console.log(err);    else        console.log(data); }) (*)  事前にQueueが作成されて いる必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 64. サンプル:DynamoDB  –  putItem var  AWS  =  require('aws-‐‑‒sdk'); var  fs  =  require('fs'); var  ddb  =  new  AWS.DynamoDB(); ① var  params  =  {    TableName:  'my_̲table',    Item:  {        'id':  {N:  ʻ‘100'},        'timestamp':  {N:  '130699342'},        'message':  {S:  'Good  Morning'}    } }; ② ddb.putItem(params,  function(err,  data)  {    if  (err)        console.log(err);    else        console.log(data); }); ③ ①  DynamoDBクライアント 作成 ②  パラメータ指定 -‐‑‒  :TableName:  テーブル名 -‐‑‒  :Item:  Putするアイテムを指 定 (例例)    id  :  Hash  Key    timestamp  :  Range  Key    message  :  Attribute ③  Put実⾏行行 (*)  事前にTableが作成され ている必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 65. サンプル:DynamoDB  –  getItem var  AWS  =  require('aws-‐‑‒sdk'); var  fs  =  require('fs'); var  ddb  =  new  AWS.DynamoDB(); ① var  params  =  {    TableName:  'my_̲table',    Key:  {        'id':  {N:  ʻ‘100'},        'timestamp':  {N:  '130699342'}    } }; ② ddb.getItem(params,  function(err,  data)  {    if  (err)        console.log(err);    else        console.log(data); }); ③ ①  DynamoDBクライアント作 成 ②  パラメータ指定 -‐‑‒  :TableName:  テーブル名 -‐‑‒  :Item:  Putするアイテムを 指定 (例例)    id  :  Hash  Key    timestamp  :  Range  Key    message  :  Attribute ③  Get実⾏行行 (*)  事前にTableが作成されてい る必要があります。 © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 66. まとめ © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 67. 今回のまとめ !   AWSはプログラマブルなインフラ •  ほぼすべてのサービスはマネジメントコンソールから利利⽤用でき るが、⾃自動化しようと思ったらプログラムから扱うのが必須 •  ⾃自動化するとAWSは何倍も便便利利になる !   AWSのサービスはHTTP/HTTPSでREST/SOAP形式の APIをサポート。 例例えば •  EC2  :  Start/Stop/Terminate •  S3  :  GetObject/PutObject •  DynamoDB  :  Get/Put !   SDKはこれらのAPIを抽象化し、各⾔言語からの利利⽤用を⾮非 常に簡単にしてくれる © 2013 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.
  • 68. Q&A 68 © 2012 Amazon.com, Inc. and its affiliates. All rights reserved. May not be copied, modified or distributed in whole or in part without the express consent of Amazon.com, Inc.