ROMANCE DAWN for the new world

Microsoft Azure を中心とした技術情報を書いています。

Azure Storage Client Library 1.7 を 4.3 に移行する

8月上旬に、Azure Storage の 古い REST API が削除されることが告知されました。Client Library の Version 1.x は、2015年8月1日以降に使うことができなくなります。古い REST API と Storage Client のサポート終了日は、2015年8月1日 から 2015年12月9日 に延長されました。Storage Client のサポート終了日は、Ver.1.7 ~ 1.5.1 は 2015年12月9日 から 無期限 に延期され、それ以前のバージョンは 2015年12月9日 から 2016年8月1日 に延期されました。同時に古い Azure Diagnostics もサポート終了となりますので、移行方法はこちらを参照してください。

Storage Client Library は、Version 2.0 以降で互換性が保証されない変更が行われており、移行に手間がかかります。そこで、Version 1.7 を 最新版(4.3)に移行する際のポイントをまとめます。この記事では、Storage Client 全体で共通する項目について記載します。それ以外については、記事を分けました。

名前空間

#Version 1.7
// Microsoft.WindowsAzure.StorageClient.dll
using Microsoft.WindowsAzure.StorageClient;
#Version 4.3
// Microsoft.WindowsAzure.Storage.dll
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Queue;

アセンブリ名と名前空間が変更されています。

アカウント情報の取得とリトライポリシーの設定

#Version 1.7
var account = CloudStorageAccount.FromConfigurationSetting("MyStorage");
var blobClient = account.CreateCloudBlobClient();
blobClient.RetryPolicy = RetryPolicies.Retry(3, TimeSpan.FromMilliseconds(500));
#Version 4.3
var account = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("MyStorage"));
var blobClient = account.CreateCloudBlobClient();
blobClient.DefaultRequestOptions.RetryPolicy = new LinearRetry(TimeSpan.FromMilliseconds(500), 3);

FromConfigurationSetting() メソッドが廃止されているので、Parse() メソッドでアカウント情報を取得します。LinearRetry(固定の間隔で最大回数までリトライ)、ExponentialRetry(ランダムな間隔で最大回数までリトライ)、NoRetry(リトライしない)の3種類のリトライがあり、カスタムリトライも実装可能です。

例外処理

#Version 1.7
catch (StorageException ex)
{
 var errorCode = ex.ErrorCode;
 var statusCode = ex.StatusCode;
 throw;
}
#Version 4.3
catch (StorageException ex)
{
 var statusCode = (HttpStatusCode) ex.RequestInformation.HttpStatusCode;
 var serviceRequestID = ex.RequestInformation.ServiceRequestID;
 if (ex.RequestInformation.ExtendedErrorInformation != null)
 {
  var errorCode = ex.RequestInformation.ExtendedErrorInformation.ErrorCode;
 }
 throw;
}

StorageException から直接取得できたエラーコードやステータスコードは廃止されたので、RequestInformation から取得します。取得が面倒になっただけのように思われますが、ServiceRequestID などのトラブル発生時に役立つ情報が取得可能になりました。

ストレージアカウントの構成設定

#Version 1.7
CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
{
    configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
});

WebRole の OnStart() メソッドや Global の Application_Start() メソッドで行っていた構成設定パブリッシャのセットアップは、Version 4.3 では必要ありません。

async / await の非同期対応

#Version 4.3
// Create
CloudTable table = tableClient.GetTableReference("Person");
await table.CreateIfNotExistsAsync();
// Delete
await table.DeleteIfExistsAsync();
// Exist
var isExists = await table.ExistsAsync();

呼び出し元のスレッドを待機しないで Azure Storage を使うことが可能になりました。

今回の移行作業を行うにあたり、こちらのブログが参考になりました。