Temporary File Storage for Azure Web Role

First, make sure that you design for cloud operating system, this way your application will not require to write to some specific file path or directory etc. But there will be instances where you will need to write or save a file in a local path somewhere. Azure helps us to do this by means of “LocalResource”

To set this up, its simple, open your ServiceDefinition.csdef file,
Add under section,

<WebRole name="MyWebRole" vmsize="Small">
       <LocalStorage name="MyLocalStorage" sizeInMB="10" />

One you have this, your role environment will get you a local resource when its running, (even when using an emulator on local)

Now you are ready to use local file system on Azure, here is how you access:

if (RoleEnvironment.IsAvailable) // this guard helps not to throw error when running outside emulator in local
LocalResource localResource = RoleEnvironment.GetLocalResource("MyLocalStorage");
Debug.WriteLine (localResource.RootPath);


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s