hughesdan hughesdan - 7 months ago 25
ASP.NET (C#) Question

Minimizing code impact of migrating to Azure Blob storage

I'm attempting to migrate fairly complex application to Windows Azure. In both the worker role and web role there are many instances where the application saves files to a local file system.

Here's an example:

string thumbnailFileName = System.IO.Path.GetDirectoryName(fileName) + "\\" + "bthumb_" + System.IO.Path.GetFileName(fileName);


and another example:

using (System.IO.StreamWriter file = System.IO.File.AppendText(GetCurrentLogFilePath()))
string logEntry = String.Format("\r\n{0} - {1}: {2}", DateTime.Now.ToString(""), type.ToString(), message);


In these examples we are saving images and log files to file locations specified in the app.config. Here's an example:

<add key="ImageFileDirectory" value="C:\temp\foo\root\auth\inventorypictures"/>

I'd like to make as few code changes as possible to support Azure blob storage in case we ever decide to move back to a more traditional hosting environment and more generally to reduce the potential for creating unintended problems.

Based on this post I've decided that Azure Drive is not the best way to go.

Can someone guide me in the right direction (ideally with an example)? The best solution in my mind would be one that only requires a change to my config file. But I'm guessing that is not realistic.


Indeed, you want to use Azure Blob storage to save your files.

As for your coding question, consider creating an interface, call it IFileStore:

public interface IFileStore
   void Save(string filePath, byte [] contents);
   byte [] Read(string filePath);

Then you create 2 provider classes, one for the file system, and one for Azure Blob storage.

The file system provider can implement the save function like this:

    public void Save(string filePath, byte [] content)
        File.WriteAllBytes(filePath, content);

    public byte [] Read(string filePath)
        return File.ReadAllBytes(filePath);

As for the Azure Blob provider, you will have to derive the storage path based on the filePath passed in to you.