Github にある johnnyhalife/waz-storage · GitHub ライブラリを使うと簡単に操作できます。Windows Azure Storage REST API のラッパーになってます。
Rubygems でインストール
$ sudo gem install waz-storage
Fetching: mime-types-1.17.2.gem (100%)
Fetching: rest-client-1.6.7.gem (100%)
Fetching: ruby-hmac-0.4.0.gem (100%)
Fetching: waz-storage-1.1.1.gem (100%)
Successfully installed mime-types-1.17.2
Successfully installed rest-client-1.6.7
Successfully installed ruby-hmac-0.4.0
Successfully installed waz-storage-1.1.1
4 gems installed
Installing ri documentation for mime-types-1.17.2...
Installing ri documentation for rest-client-1.6.7...
Installing ri documentation for ruby-hmac-0.4.0...
Installing ri documentation for waz-storage-1.1.1...
Installing RDoc documentation for mime-types-1.17.2...
Installing RDoc documentation for rest-client-1.6.7...
Installing RDoc documentation for ruby-hmac-0.4.0...
Installing RDoc documentation for waz-storage-1.1.1...
認証
waz-storage ライブラリの認証方法は Base のクラスメソッド establish_connection! にアカウント名、アクセスキー、SSLを使うかどうかをハッシュで渡します。
|
|
テーブルサービスの操作
テーブルの全てのエンティティを取得
エンティティを取り出します。サービスインスタンスにテーブル名を指定して query メソッドを呼ぶだけです。
service = WAZ::Tables::Table.service_instance entities = @service.query("<テーブル>")
これでハッシュの配列でエンティティ群が取得できます。
ただ一度に取れるのは、1000件までです。continuation_token に続きのPartitionKeyとRowKeyが入っています。これを query メソッドのオプションに渡します。
if entities.continuation_token\["NextPartitionKey"\].nil?
next_entities = service.query("<テーブル>", { :continuation_token =\> entities.continuation_token })
end
パーティション内のエンティティを取得
query オプションに expression を指定できるのでここでフィルタで定義します。
partition_key = "<PartitionKey>"
entities = service.query("<テーブル>",
{ :expression =\> "PartitionKey eq #{partition_key}" })
エンティティの取得
特定の Row だけ取得します。
entity = service.get_entity("<テーブル>", "<PartitionKey>", "<RowKey>")
エンティティの追加・更新
追加は insert_enity, 置き換え更新は update_enity, マージ更新は merge_enity になります。
entity = { :name =\> "なまえ", :age =\> 25 }
entity\[:partition_key\] = "<PartitionKey>"
entity\[:row_key\] = "<RowKey>"
entity\[:Timestamp\] = Time.now.utc
service.insert_entity("<テーブル>", entity)
upsert をやりたい場合は、REST API の 2011-08-18 バージョンから追加するもしくはマージ更新という仕様に変わっています。そこでライブラリに下記のように mix-in を使うことで upsert_entity を実装できます。
|
|
エンティティの削除
削除は delete_entity を呼び出します。
service.delete_entity("<テーブル>", “<PartitionKey>”, “<RowKey>”)