aws小ネタ集
DESCRIPTION
第五回JAWS-UG札幌勉強会で発表したスライドです。詳細はブログ(http://blog.dateofrock.com/)に書きますwTRANSCRIPT
![Page 1: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/1.jpg)
第5回 AWS User Group - Sapporo 勉強会
AWS小ネタ集欧文印刷株式会社 田名辺健人2012.02.14
http://www.flickr.com/photos/mendhak/4271441910/
![Page 2: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/2.jpg)
田名辺 健人(たなべたけひと)
ソフトウェアエンジニア欧文印刷株式会社(東京都)2011年11月から札幌でテレワーク
@dateofrock
札幌・東京コアメンバーUstream担当好きなサービスはSQS
![Page 3: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/3.jpg)
![Page 4: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/4.jpg)
![Page 5: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/5.jpg)
![Page 6: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/6.jpg)
http://www.flickr.com/photos/pinksherbet/1483193019/
Happy Valentine
![Page 7: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/7.jpg)
http://www.flickr.com/photos/pinksherbet/1483193019/
Happy ValentineLT(Lady's Talk)してくれた貴女へ…
![Page 8: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/8.jpg)
http://www.flickr.com/photos/pinksherbet/1483193019/
Happy ValentineNUboardプレゼント♡
from 欧文印刷
![Page 9: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/9.jpg)
たまには技術の話を
しようと思いますwww
![Page 10: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/10.jpg)
• S3 小ネタ• 読み書きのスループットを上げる方法• バケット名にピリオドでハマる方法
• S3実践ネタ• 設定ファイル置き場• Pre-signed URLの使い道
• Simpleシリーズ2連発• アプリ内エラー通知はSNSが便利• SimpleDB APIがシンプルじゃない件
http://www.flickr.com/photos/mendhak/4271441910/
![Page 11: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/11.jpg)
http://www.flickr.com/photos/donsolo/2435421847/
ネタ作りすぎたw
![Page 12: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/12.jpg)
• S3 小ネタ• 読み書きのスループットを上げる方法• バケット名にピリオドでハマる方法
• S3実践ネタ• 設定ファイル置き場• Pre-signed URLの使い道
• Simpleシリーズ2連発• アプリ内エラー通知はSNSが便利• SimpleDB APIがシンプルじゃない件
http://www.flickr.com/photos/mendhak/4271441910/
![Page 13: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/13.jpg)
• S3 小ネタ• 読み書きのスループットを上げる方法• バケット名にピリオドでハマる方法
• S3実践ネタ• 設定ファイル置き場• Pre-signed URLの使い道
• Simpleシリーズ2連発• アプリ内エラー通知はSNSが便利• SimpleDB APIがシンプルじゃない件
http://www.flickr.com/photos/mendhak/4271441910/
半分カット
orz
![Page 14: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/14.jpg)
http://www.flickr.com/photos/mendhak/4271441910/
• S3 小ネタ• 読み書きのスループットを上げる方法• バケット名にピリオドでハマる方法
• S3実践ネタ• 設定ファイル置き場• Pre-signed URLの使い道
• Simpleシリーズ2連発• アプリ内エラー通知はSNSが便利• SimpleDB APIがシンプルじゃない件
![Page 15: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/15.jpg)
Pre-signed URLの使い道
• S3オブジェクトに対して有効期限付きのパブリックなURLを生成できる
• Query String Request Authenticationhttp://docs.amazonwebservices.com/AmazonS3/latest/dev/RESTAuthentication.html#RESTAuthenticationQueryStringAuth
![Page 16: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/16.jpg)
Pre-signed URLどんなもの?
![Page 17: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/17.jpg)
GET /images/photo.jpg?AWSAccessKeyId=0PN5J17HBGZHT7JJ3X82&Expires=1141889120&Signature=vjbyPxybdZaNmGa%2ByT272YEAiv4%3D
Signature = URL-Encode( Base64( HMAC-SHA1( YourSecretAccessKeyID, UTF-8-Encoding-Of(StringToSign))));
StringToSign = HTTP-VERB + "\n" + Content-MD5 + "\n" + Content-Type + "\n" + Expires + "\n" + CanonicalizedAmzHeaders + CanonicalizedResource;
![Page 18: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/18.jpg)
こんなマンドクサイものはSDKに頼ろう
![Page 19: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/19.jpg)
Java
Interface AmazonS3
java.net.URL generatePresignedUrl(java.lang.String bucketName, java.lang.String key, java.util.Date expiration, HttpMethod method) throws AmazonClientException
Date after5Seconds = new Date();after5Seconds.setTime(System.currentTimeMillis() + 5 * 1000);
AmazonS3 s3 = new AmazonS3Client(cred);Url url = s3.generatePresignedUrl(“mybucket”, “images/photo.jpg”, after5Seconds, HttpMethod.GET);
![Page 20: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/20.jpg)
Ruby
Class: AWS::S3::S3Object- (URI::HTTP, URI::HTTPS) url_for(method, options = {})
url = s3.buckets['mybucket'].objects['images/photo.jpg']. url_for(:read, :expires => 5)
![Page 21: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/21.jpg)
Ruby
Class: AWS::S3::S3Object- (URI::HTTP, URI::HTTPS) url_for(method, options = {})
url = s3.buckets['mybucket'].objects['images/photo.jpg']. url_for(:read, :expires => 5)
Rubyステキ!
![Page 22: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/22.jpg)
Pre-signed URL実 例
![Page 23: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/23.jpg)
![Page 24: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/24.jpg)
![Page 25: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/25.jpg)
![Page 26: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/26.jpg)
![Page 27: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/27.jpg)
Pre-signed URLの使い道
• Webサーバーの負荷が劇的に減る!
• アプリはPre-signed URLを生成してS3
にリダイレクトするだけ
• 重たいデータ送出はS3にお任せ!
![Page 28: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/28.jpg)
http://www.flickr.com/photos/mendhak/4271441910/
• S3 小ネタ• 読み書きのスループットを上げる方法• バケット名にピリオドでハマる方法
• S3実践ネタ• 設定ファイル置き場• Pre-signed URLの使い道
• Simpleシリーズ2連発• アプリ内エラー通知はSNSが便利• SimpleDB APIがシンプルじゃない件
![Page 29: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/29.jpg)
アプリ内の通知はSNSが便利
たとえば例外が投げられたら
メールでお知らせとか
![Page 30: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/30.jpg)
Java
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {" AWSCredentials cred = new BasicAWSCredentials(
"アクセスキー","シークレットキー");" AmazonSNS sns = new AmazonSNSClient(cred);" String stackTrace = getStackTraceString(e);" sns.publish(new PublishRequest(
" " " "arn:aws:sns:ap-northeast-1:xxxxx:sns-test"," " " stackTrace, "Java Exception"));
" }" });
例えばUncaughtExceptionHandlerを使う
![Page 31: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/31.jpg)
Ruby on Rails
require 'rubygems'require 'aws-sdk'
class ApplicationController < ActionController::Base rescue_from StandardError do |exception| topic = AWS::SNS::Topic.new( 'arn:aws:sns:ap-northeast-1:xxxxx:sns-test' )
topic.publish(exception.backtrace.to_s, :subject => 'Rails exception'); raise exception endend
例えばApplicationController#rescue_fromを使う
![Page 32: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/32.jpg)
ただし、メッセージは
8kB以内に限るorz
![Page 33: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/33.jpg)
SimpleDB APIがシンプルじゃない件
• いや、シンプルすぎて冗長なコードになってしまう
• 素のJDBCはチョット…的な。
• データの型は全部文字列だよ!
![Page 34: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/34.jpg)
SimpleDBのData Mapperがホスィ
Hibernate的なActiveRecord的な
WOのEOF的な
![Page 35: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/35.jpg)
Ruby
SDKにSimpleDBのData Mapperが!
![Page 36: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/36.jpg)
class Book < AWS::Record::Base string_attr :titleend
$ rails generate scaffold_controller Book title:string
config/routes.rb:Myapp::Application.routes.draw do resources :booksend
http://aws.amazon.com/articles/8621639827664165 より
Book.new(:title => 'スベらないプレゼン').save
Book.where('title like ?', '%プレゼン%').count
![Page 37: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/37.jpg)
class Book < AWS::Record::Base string_attr :titleend
$ rails generate scaffold_controller Book title:string
config/routes.rb:Myapp::Application.routes.draw do resources :booksend
http://aws.amazon.com/articles/8621639827664165 より
Book.new(:title => 'スベらないプレゼン').save
Book.where('title like ?', '%プレゼン%').countRailsと違和感なしに統合できる!
![Page 38: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/38.jpg)
Java
• AWS SDK for Javaには低レベルAPIしかないのだよ。
• JPA実装とかはあるけど…
https://github.com/appoxy/simplejpa
![Page 39: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/39.jpg)
でも実はね…
![Page 40: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/40.jpg)
DynamoDBにはDataMapperがあるのだよ(;́Д`)
![Page 41: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/41.jpg)
@DynamoDBTable(tableName = "BookTable")public class Book { private Long id; private String title;
@DynamoDBHashKey public Long getId() {
" return this.id; }
@DynamoDBAttribute(attributeName = "title") public String getTitle() {
" return this.title; }
// 以下、setter}
![Page 42: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/42.jpg)
使用例(DynamoDBMapper)
AWSCredentials cred = new BasicAWSCredentials("アクセスキ", "シークレットキー");
AmazonDynamoDB db = new AmazonDynamoDBClient(cred);
DynamoDBMapper mapper = new DynamoDBMapper(db);
Long key = 105L;Book book = mapper.load(Book.class, key);book.setTitle("スベらないプレゼン");mapper.save(book);mapper.delete(book);
![Page 43: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/43.jpg)
なぜSimpleDBには無いのだ!
![Page 44: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/44.jpg)
ついカッとなってつくってみたw
![Page 45: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/45.jpg)
![Page 46: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/46.jpg)
SimpleDBMapper(笑)
![Page 47: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/47.jpg)
@SimpleDBDomain(domainName = "BookDomain")public class Book {
@SimpleDBItemName public Long id;
@SimpleDBAttribute(attributeName = "title") public String title;
}
![Page 48: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/48.jpg)
使用例(SimpleDBMapper)
AWSCredentials cred = new BasicAWSCredentials("アクセスキ", "シークレットキー");
AmazonSimpleDB db = new AmazonSimpleDBClient(cred);
SimpleDBMapper mapper = new SimpleDBMapper(db);
Long key = 105L;Book book = mapper.load(Book.class, key);book.setTitle("スベらないプレゼン");mapper.save(book);mapper.delete(book);
![Page 49: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/49.jpg)
![Page 50: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/50.jpg)
DynamoをSimpleに変えただけ(笑)
![Page 51: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/51.jpg)
simpledb-mapper(笑)• DynamoDBMapperライクなAPI(笑)
• でもパクっただけじゃない
• 1024byteを超えるデータ(Blob)はS3に
• @SimpleDBVersionでOptimistic
Lock(Conditional PUT/DELETE)
• 検索APIにはナウいFluent Interface採用
![Page 52: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/52.jpg)
ナウな感じに書ける
List<Book> books = mapper.from(Book.class).where("title", Like, "%スベる%").and("price", GreaterThan, 1000).orderBy("price", DESC).limit(10).fetch();
![Page 53: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/53.jpg)
ただし
![Page 54: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/54.jpg)
使用上のご注意
• 自分が現状必要としていないものは実装していません(笑)
• リレーション機能は持たせないつもりです。(Simpleじゃなくなるので)
• テストは適当です(これはひどい)
![Page 55: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/55.jpg)
というわけで
![Page 56: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/56.jpg)
![Page 57: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/57.jpg)
dateofrock/simpledb-mapper
(笑)
![Page 58: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/58.jpg)
気が向いたらForkしてね
![Page 59: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/59.jpg)
残りは後日ブログに書きますw
http://www.flickr.com/photos/mendhak/4271441910/
![Page 60: AWS小ネタ集](https://reader033.vdocuments.pub/reader033/viewer/2022042815/5565fd4bd8b42a2a4d8b4c0d/html5/thumbnails/60.jpg)
Thank you!
http://www.flickr.com/photos/mendhak/4271441910/