DynamoDBにアクセスして書き込み等を行う
Perlからアクセスするのは面倒この上ない Perl5系でDynamoDBを良い感じに扱えるツールが無かったので、低レベルAPIを使ったアクセスを行った。
AWSのドキュメント
エンドポイント
Perl
必要なもの
- DynamoDBのIAM情報(アクセスキー、シークレットキー)
- Net::Amazon::Signature::V4 - Implements the Amazon Web Services signature version 4, AWS4-HMAC-SHA256 - metacpan.org
コード
use Net::Amazon::Signature::V4; use HTTP::Request; use Furl; my $access_key = 'hogehoge'; my $secret_key = 'gehogeho'; my $signature = Net::Amazon::Signature::V4->new( $access_key, $secret_key, 'ap-northeast-1', 'dynamodb' ); my $furl = Furl->new( timeout => 2 ); my $uri = 'https://dynamodb.ap-northeast-1.amazonaws.com/'; my $request = HTTP::Request->new( 'POST', $uri ); $request->header( 'X-Amz-Target' => 'DynamoDB_20120810.PutItem' ); $request->header( 'Content-Type' => 'application/x-amz-json-1.0' ); my $content_string = <<'EOS'; { "TableName": "hchuno_table", "Item": { "id": {"N":"1"}, "hchuno_ids" : { "L":[ {"N":"1"}, {"N":"2"}, {"N":"3"} ] } } } EOS $request->content($content_string); my $signed_request = $signature->sign($request); my $response = $furl->request($signed_request);
↑最後の$responseをダンプすると、処理が成功した時に
'message' => 'OK',
という文字列が入っている。