Azure FAQ: Can I write to the file system on Windows Azure?

[Update 12-Oct-2012] This post only applies to Windows Azure Cloud Services (which have Web Roles and Worker Roles). This post was written a year before Windows Azure Web Sites and Windows Azure Virtual Machines (including Windows and Linux flavors) were announced and does not apply to either of them.

Q. Can I write to the file system from an application running on Windows Azure?

A. The short answer is that, yes, you can. The longer answer involves better approaches to persisting data in Windows Azure, plus a couple of caveats in writing data to (virtual) hard disks attached to the (virtual) machines on which your application is deployed.

Any of your code running in either (a) ASP.NET (e.g., default.aspx or default.aspx.cs) or (b) WebRole.cs/WorkerRole.cs (e.g., methods OnStartup, OnRun, and OnStop which are derived from RoleEntryPoint class) will not have permission to write to the file system. This. is. a. good. thing.®

To be clear, if you have code that currently writes to fixed locations on the file system, you will probably need to change it. For example, your ASP.NET or Role code cannot directly create/write the file c:\foo.txt – the permissions are against you, so Windows will not allow it. (To round out the picture though… You can write to the file system directly if you are running in an elevated Startup Task, but cannot write to it from a limited Startup Task. For more on Startup Tasks and how to configure them see How to Define Startup Tasks for a Role.)

The best option is usually to use one of the cloud-native solutions: use one of the Windows Azure Storage Services or use SQL Azure. These services are all built into Windows Azure for the purpose of supporting scalable, reliable, highly available storage. In practice, this means choosing among Windows Azure Blob storage, Windows Azure Table storage, or SQL Azure.

The second-best option is usually to use a Windows Azure Cloud Drive – which is an abstraction that sits on top of Blob storage (Page blobs, specifically) – and looks and acts a lot like an old-fashioned hard disk. You can access it with a drive letter (though you won’t know the drive letter until deployment time!), it can be mounted by and read from multiple of your role instances, but only one of these at a time will be able to mount it for updating. The Windows Azure Drive feature is really there for backward compatibility – to make it easier to migrate existing applications into the cloud without having to change them. Learn more from Neil Mackenzie’s detailed post on Azure Drives.

The third-best option is usually to use the local hard disk. (And this is what the original FAQ question specifically asked about.) Read on…

Writing Data to Local Drive from Windows Azure Role

So… Can I write to the hard disk? Yes. And you have a decent amount of disk at your disposal, depending on role size. Using Azure APIs to write to disk on your role is known as writing to Local Storage. You will need to configure some space in Local Storage from your ServiceDefinition.csdef by giving that space (a) a name, (b) a size, and (c) indicating whether the data there should survive basic role recycles (via cleanOnRoleRecycle). Note – cleanOnRoleRecycle does not guarantee your data will survive – it is just a hint to the Fabric Controller that, if it is available, should it leave it around or clean it up.  That limitation is fine for data that is easily recalculated or generated when the role starts up – so there are some good use cases for this data, even for cloud-native applications – think of it as a handy place for a local cache. (Up above I refer to this as the usually being the third-best option. But maybe it is the best option! In some use cases it might be. One good example might be if you were simply exploding a ZIP file that was pulled from blob storage, but there are others too. But let’s get back to Local Storage…)

Here is the snippet from ServiceDefinition.csdef:

...
<LocalResources>
<LocalStorage name="SomeLocationForCache"
cleanOnRoleRecycle="false"
sizeInMB="10" />
</LocalResources>
...

You can also use the Windows Azure Tools for Visual Studio user interface to edit these values; double-click on the role you wish to configure from the Roles list in your Windows Azure solution. This is the easiest approach.

Once specified, the named Local Storage area can be written to and read from using code similar to the following:

// reference Microsoft.WindowsAzure.ServiceRuntime.dll from SDK
// (probably in C:\Program Files\Windows Azure SDK\v1.4\ref)
const string azureLocalResourceNameFromServiceDefinition =
"SomeLocationForCache";
var azureLocalResource =
RoleEnvironment.GetLocalResource(
azureLocalResourceNameFromServiceDefinition);
var filepath =
azureLocalResource.RootPath +
"myCacheFile.xml"; // build full path to file
// the rest of the code is plain old reading and writing of files
// using the 'filepath' variable immediately above

Learn more from Neil Mackenzie’s blog post on Local Storage.

Writing to TEMP Folder from Windows Azure Role

How about writing temporary files? Is that supported? Yes, same as in Windows. For example, in .NET one can get a temporary scratch space and write to it using code similar to the following:

var filepath = System.IO.Path.GetTempFileName();
System.IO.File.WriteAllText(filepath, "some text");

Do Not Use Environment.SpecialFolder Locations in Azure

You may also have some existing code which writes files for the currently logged in user. Check the Environment.SpecialFolder Enumeration for the full list, but one examples is Environment.SpecialFolder.ApplicationData. You would access this location with code such as the following:

string filepath = Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData,
Environment.SpecialFolderOption.DoNotVerify);

You will find that your ASP.NET code will be able to write to this location, but that is almost certainly not what you want! By default, the user account under which you will be saving this data is one that is generated when your role is deployed – something like RD00155D328831$ – not some IPrincipal from your Windows domain.

Further, for data you care about, you don’t want to store data it in the local file system in Windows Azure. Better options should be apparent from earlier points made in this article.

And, finally, you may prefer the elegance of claims-based federated authentication using the Access Control Service.

Writing to File System from Windows Service in Windows Azure Role

If you want to do something unusual, like write to the file system from outside of Role’s code, there are ways to write to the file system from a Windows Service or a Startup Task (though be sure to run your Startup Task with elevated permissions).

Is this useful? Did I leave out something interesting or get something wrong? Please let me know in the comments! Think other people might be interested? Spread the word!

39 thoughts on “Azure FAQ: Can I write to the file system on Windows Azure?

  1. Balaji

    Thanks for the blog post. I was trying to do this without using the Azure storage for so long until I found the answere here :)

    Reply
  2. Pingback: The Low Down Dirty Azure Blues - Steve Wellens

    1. Bill Wilder Post author

      @Josey – that’s a great question. Short answer: any needed cleanup would fall to you (there is nothing automatic that will happen otherwise), but such cleanup is usually not needed. But the full answer is that “it depends” – do you expect to make use of the TEMP folder and expect that files will be abandoned in there (rather than having the programs that created them also delete them when done)?

      Reply
  3. windows

    Hello, i think that i saw you visited my weblog so i came to “return the
    favor”.I’m attempting to find things to enhance my website!I suppose its ok to use some of your ideas!!

    Reply
  4. Ernest

    “Azure FAQ: Can I write to the file system on Windows Azure?
    | Coding Out Loud” deadseaskincarecosmetics was indeed a
    wonderful article. If merely there was significantly more weblogs
    such as this one on the actual web. Anyway, thank you for your time, Carrol

    Reply
  5. Chemietoilette

    My partner and I absolutely love your blog and find most of your post’s to be just what I’m looking for.
    Does one offer guest writers to write content for yourself?

    I wouldn’t mind writing a post or elaborating on some of the subjects you write regarding here. Again, awesome web site!

    Reply
  6. augenlasern

    We are a bunch of volunteers and starting a brand new scheme in our community.

    Your web site offered us with useful info to work on.
    You have performed a formidable process and our whole neighborhood will probably be thankful to you.

    Reply
  7. Deloris

    Good day! Do you know if they make any plugins to protect against hackers?
    I’m kinda paranoid about losing everything I’ve worked hard on.
    Any suggestions?

    Reply
  8. Pingback: From my reading list #5 – December 17, 2013 | Pascal Laurin

  9. Pingback: Stupid Azure Trick #7 – Use Windows Azure’s Local Storage Emulator with Web Sites | Coding Out Loud

  10. Pingback: 2015 100% Microsoft Official Guaranteed Pass for Braindump2go Latest 70-534 Dumps (11-20) | Braindump2go Updated Real Microsoft MCITP Exam Questions

  11. Pingback: 2015 New Released Microsoft 70-534 Dumps Verison Free Use in Braindump2go (11-20) | Braindump2go Free MCPD Exam Dumps, Exam Questions

  12. Pingback: 2015 Microsoft Official Latest Updated 70-534 Exam Dumps Questions Free Download Provided By Braindump2go (11-20) | Updated Real Microsoft MCPD Exam Questions & MCPD Dumps

  13. Pingback: Braindump2go 100% Pass Guaranteed: Microsoft 70-534 Exam Questions Dumps Full Version Download (11-20) | Braindump2go Free MCITP Exam Questions Collection

  14. Pingback: 2015 Latest Microsoft 70-534 Exam Questions Provided By Braindump2go For Free Download (11-20) | Free Download Braindump2go MCSA Exam Questions & Dumps with PDF&VCE

  15. Pingback: New Updated Version: Microsoft 70-534 Dumps VCE Free Download (11-20) | Braindump2go Updated Real Microsoft MCTS Exam Questions & MCTS Dumps

  16. Pingback: New Released Version: Microsoft 70-534 Exam Dumps PDF Free Try (11-20)Free Download Latest Braindump2go Microsoft Exam Dumps | Free Download Latest Braindump2go Microsoft Exam Dumps

  17. Pingback: Braindump2go 2015 New Released 70-534 Practice Exams Dumps Free Download (11-20) | Free Download Braindump2go Microsoft Exam Questions and Dumps PDF & VCE

  18. Pingback: Braindump2go 100% Pass Promise: 70-534 Exam Dumps Questions New Updated By Microsoft Official Exan Center (11-20)Braindump2go Free Latest MCITP Exam Dumps | Braindump2go Free Latest MCITP Exam Dumps

  19. Pingback: 2015 Braindump2go 100% Real Microsoft 70-534 Exam Questions From Microsoft Official Exam Center (11-20) | Free Braindump2go Latest Microsoft Exam Dumps

  20. Pingback: 2015 100% Pass Guarantee: Braindump2go 70-534 Exam Questions and Answers from Real 70-534 Exam Dumps! (11-20) | Braindump2go Hot IT Certification Exam Questions

  21. Pingback: New Released Brain Dumps:2015 Braindump2go Microsoft 70-534 Dumps Instant Free Download (11-20) | Free Download Braindump2go Oracle OCA, OCP, OCM,Microsoft Exam Questions & Dumps with PDF&VCE

  22. Pingback: Braindump2go 70-534 Exam Dumps New Version Provided For Free Download Today! (11-20) | Braindump2go Updated Real Microsoft Exam Dumps PDF&VCE

  23. Pingback: High Pass Rate: 2015 Braindump2go New Released Microsoft 70-534 Dumps Guarantee 100% Pass (11-20)Free Braindump2go Microsoft Exam Dumps PDF&VCE | Free Braindump2go Microsoft Exam Dumps PDF&VCE

  24. Pingback: 2015 Microsoft 70-534 Practice Tests Free Download 100% Pass Promised By Braindump2go (11-20) | Updated Real Microsoft MCSA Exam Questions & MCSA Dumps

  25. Pingback: 100% Pass Microsoft 70-534 Exam Questions and Answers Instant Download in Braindump2go (11-20) | Braindump2go IT Exam Questions Free Download

  26. Pingback: 100% Pass Microsoft 70-534 Exam Questions Free Braindump2go Instant Download (11-20) | Free IT Exam VCE Download - ExamCollection

  27. Pingback: 2015 70-534 Dumps VCE Latest Updated By Braindump2go Today (11-20) | Braindump2go Free Latest Microsoft Hot Exam Dumps In PDF & VCE Free Download

  28. Pingback: Braindump2go Provides Instant Free Download for Microsoft 70-534 Exam Dumps PDF! (11-20) | Free Braindump2go Latest Microsoft Exam Dumps

  29. Pingback: 2015 Braindump2go Microsoft 70-534 Practice Exam Dumps New Version From Microsoft Official Exam Center (11-20) | Free Latest Braindump2go Exam Dumps

  30. Pingback: Braindump2go 2015 100% Real 70-534 Exam Questions Guaranteed from Microsoft Official (11-20)Free Download Braindump2go VMware VCP510 & VCP550 Exam Dumps | Free Download Braindump2go VMware VCP510 & VCP550 Exam Dumps

  31. Pingback: 2015 Latest 70-534 Practice Tests Updated By Braindump2go in Accordance with Microsoft Official Exam Center (11-20) | Braindump2go Shares Latest IT Exam Dumps PDF&VCE

  32. Pingback: 2015 Latest 70-534 Dumps PDF Free Download Today In Braindump2go! (11-20) | Free Exam Dumps PDF&VCE Examcollections

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s