前回、Worker Role で Web Server を試したので、今回は、FTP Server を試してみます。
FTP Server は「C# FTP Server」を使い、ファイルの保存先は Windows Azure Drive を使います。
C# FTP Server
http://www.codeguru.com/csharp/csharp/cs_network/sockets/article.php/c7409
1.「ASP.NET Web Role」プロジェクトを作成します。
2.Worker ロールのプロパティページを開き、「Settings」「Endpoints」「Local Srorage」に設定を追加します。
[Settings]
今回は、StorageConnectionString という名前で追加しています。
[Endpoints]
エンドポイント情報を追加し、公開するポート番号などを設定します。
設定例)
| Name | : | Endpoint1 |
| Type | : | Input |
| Protocol | : | TCP |
| Port | : | 21 |
| SSL Certificate Name | : | (not applicable) |
[Local Srorage]
設定例)
| Name | : | LocalStorage1 |
| Size | : | 1000 |
3.ダウンロードした C# FTP Server と Microsoft.WindowsAzure.CloudDrive.dll を参照設定に追加します。
4.OnStart に CloudStorageAccount の設定を追加します。
public override bool OnStart()
{
CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
{
configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
RoleEnvironment.Changed += (sender, arg) =>
{
if (arg.Changes.OfType<roleenvironmentconfigurationsettingchange>()
.Any((change) => (change.ConfigurationSettingName == configName)))
{
if (!configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)))
{
RoleEnvironment.RequestRecycle();
}
}
};
});
RoleEnvironment.Changing += RoleEnvironmentChanging;
return base.OnStart();
}
5.後は実行時に Windows Azure Drive 作り、FTPの設定を行って待つだけです。
private Assemblies.Ftp.FtpServer ftpServer = null;
public override void Run()
{
// Windows Azure Drive 設定値
const int DRIVE_SIZE = 1000;
const int CACHE_SIZE = 500;
string driveName = "drive2.vhd";
CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting("StorageConnectionString");
// コンテナがなかったら作成
string containerName = "vhd";
CloudBlobClient client = account.CreateCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference(containerName);
container.CreateIfNotExist();
// 仮想ディスク作成
LocalResource localCache = RoleEnvironment.GetLocalResource("LocalStorage1");
CloudDrive.InitializeCache(localCache.RootPath + "cache", localCache.MaximumSizeInMegabytes);
CloudDrive drive = account.CreateCloudDrive(container.GetPageBlobReference(driveName).Uri.ToString());
try
{
drive.Create(DRIVE_SIZE);
}
catch (CloudDriveException)
{
}
string driveLetter = drive.Mount(CACHE_SIZE, DriveMountOptions.None);
// FTP設定
string user = "user1";
string passwd = "passwd";
string dir = driveLetter + @"\" + user;
System.IO.Directory.CreateDirectory(dir);
IPEndPoint endpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint;
Assemblies.Ftp.UserData.Get().AddUser(user);
Assemblies.Ftp.UserData.Get().SetUserPassword(user, passwd);
Assemblies.Ftp.UserData.Get().SetUserStartingDirectory(user, dir);
Assemblies.Ftp.FtpServerMessageHandler.Message += new Assemblies.Ftp.FtpServerMessageHandler.MessageEventHandler(MessageHandler_Message);
ftpServer = new Assemblies.Ftp.FtpServer(new Assemblies.Ftp.FileSystem.StandardFileSystemClassFactory());
ftpServer.Start(endpoint.Port);
ftpServer.ConnectionClosed += new Assemblies.Ftp.FtpServer.ConnectionHandler(ftpServer_ConnectionClosed);
ftpServer.NewConnection += new Assemblies.Ftp.FtpServer.ConnectionHandler(ftpServer_NewConnection);
while (true)
{
Thread.Sleep(10000);
}
}
private void MessageHandler_Message(int nId, string sMessage)
{
}
private void ftpServer_ConnectionClosed(int nId)
{
}
private void ftpServer_NewConnection(int nId)
{
}
※アクティブモードでのみ接続可能です。