Nick Goloborodko
Nick Goloborodko

Reputation: 3053

Azure Storage / File System abstraction library

As part of the web application that I'd developing at the moment I have a requirement to write files out to storage. The will initially be hosted on Azure Websites, but in the future I would like to have the ability to host it on non-azure servers.

So - I'm looking for a library (and I hope that one exists) that would make it easy to switch between outputting files out to Azure Blob Storage or a local file system. Ideally something that would have a common API and would allow you to switch between the storage location by changing config files only.

I'm having some issues finding libraries that would have this sort of functionality and I hope someone can point me in the right direction.

Upvotes: 2

Views: 964

Answers (1)

pateketu
pateketu

Reputation: 451

Not sure if such library exists. If an abstraction library exists than I would have thought it would need to provide implementation of Azure, S3, local FileSytem, Rackspace etc.

Anyways, it's fairly straight forward to implement. Our project had two different version, cloud based and on-premise based with main real difference being the Blob storage. What we did was to build the core upload/download etc logic against an interface, have two difference implementation of it one for Azure Blob storage and one for Local file storage used StructureMap to get a reference to concrete implementation based on config value

we obviously did not replicate each and every BLOB Storage API in the interface but only the minimal required by our system

Some code example:

Interface: (BlobBase is our custom class holding info such as container name, file name, content type etc. and BlobStorageProviderStatus is custom enum providing some status info. But you get the idea!)

public interface IBlobStorageProvider
{
    void CreateContainer(string containerName);
    BlobStorageProviderStatus UploadFile(BlobBase file, bool uploadAsNewversion, Stream data, int chunk, int totalChunks, out string version);
    BlobStorageProviderStatus DownloadToStream(BlobBase file, Stream target, int chunkSize, IClientConnection clientConnection);
    void Delete(BlobBase blobBase);
    void DeleteDirectory(string directoryPath, string blobContainer);
    BlobStorageProviderStatus UploadFile(string container, string folder, string fileName, Stream data, string contentType);
    void DownloadToStream(string container, string filePath, Stream target);
}

Web.config has

<add key="AzureBlobStorage" value="true" />

and simplified version of StructureMap registeration:

  x.For<IBlobStorageProvider>()
                     .Use(() => bool.Parse(ConfigurationManager.AppSettings["AzureBlobStorage"])
                                    ? new AzureBlobStorageProvider()
                                    :  new FileSystemStorageProvider());

and the actual usage sample:

 IBlobStorageProvider blobStorage = ObjectFactory.GetInstance<IBlobStorageProvider>();
 blobStorage.CreateContainer("image"); 

Upvotes: 1

Related Questions