Azure Table Storageとは
Azure Table Storageは、AWSでいうDynamoDBの位置づけのようです。料金的にも容量とアクセス数のみらしいので(誤解しているかもしれないが)、いろいろ考えた末、Azure Table StorageをラボのメインDBに変えていこうか、、という結論に至りました。
Azure Table Storageのセッティング
Azure Table Storageは、Azure Storageの機能の一つとなっています。セッティングは簡単です。ポイントだけ書いていきます。
- AzurePortalサイトからStorageアカウントは作成しておく
- テーブルは事前に作成しなくても良い
- Storageアカウントのアクセスキーより、接続文字列を入手しておく
Visual Studio の設定
下記の2つのライブラリをNugetで明示的に取得しておく
- WindowsAzure.Storage
- Microsoft.WindowsAzure.ConfigurationManager
Nugetマネージャーを見てみると他のライブラリも登録されていましたが、これらに付随してインストールされた可能性あり。
サンプルコード
アカウント情報(アクセスキーなど)はコードに直接記述せず、App.configファイルに記述してプログラムにアクセスするのが基本です。下記のようにApp.configファイルに記述します。
<appSettings> <add key="StorageConnectionString" value="XXXXXXX" /> </appSettings>
下記のコードは、"SampleTable"というテーブルに接続している例です。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.WindowsAzure.Storage; | |
using Microsoft.WindowsAzure.Storage.Table; | |
using Microsoft.Azure; | |
static void Main(string[] args){ | |
CloudTable table; | |
// Web.configに指定した接続情報を取得 | |
CloudStorageAccount storageAccount = CloudStorageAccount.Parse( | |
CloudConfigurationManager.GetSetting("StorageConnectionString")); | |
// テーブルクライアントの作成 | |
CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); | |
// テーブルへの参照を取得し、存在しない場合には作成する | |
table = tableClient.GetTableReference("SampleTable"); | |
table.CreateIfNotExists(); | |
} |
0 件のコメント:
コメントを投稿