Document Versioning with Azure Blob Storage

Subhankar Sarkar
5 min readSep 26, 2020

Almost all organizations have a need for document storage for their applications. If the organization is a Microsoft shop then, one of the most popular choices for them is SharePoint Document Library. SharePoint Document library allows you to store documents, retain their versions, provides the ability to make any old version to the current version, last but not the least deleting a specific version of a document.

With all the goodness of SharePoint, it comes with its own limitations. For many custom cloud applications build, SharePoint may not always be the right choice because of various reasons. It needs a dedicated setup, licensing etc. And just to use the document library feature, the investment may not always be justifiable.

So, what option do you have? can you achieve all the above SharePoint Document Library features otherwise?

If you are building your application in Azure, then it is highly likely that you are taking the benefit of Azure Storage Account Blob Container for any storage needs, including but not limited to document/ file storage.

The good news is you can now achieve all of these using Azure Storage Account’s blob versioning feature.

In this article, I’m going to talk about how you can leverage your existing Azure Storage Account Infrastructure and use it effectively for your document and file storage needs.

I’m going to cover the following with a few C# code samples.

  • Document Versioning
  • List all document with all their versions
  • Download a specific version of the document
  • Make a specific version of the document to the current version
  • Delete a specific version of the document

You can find the entire project in GitHub.

How can you achieve versioning in blob storage?

Until this announcement of Blob Versioning for Azure Storage Account, it was tedious to maintain the versions of any document stored as a blob in a container. One of such manual approach was to take a snapshot of the document while uploading. Maintain that snapshot information in some sort of table (e.g.: database tables). And to restore the file, use the table stored information to restore from the snapshot.

You can read more about Snapshot and Blob versioning here.

Okay, so those days are gone now. With some simple steps, you can enable versioning to your blob and achieve document library like features.

You can enable Versioning while creating the storage account. Under Data protection tab check Tracking >> Turn on versioning option.

For your existing Storage Account, to enable versioning go to the left side blade >> look for Data Protection under Blob service. On your left pane under Tracking select Turn on versioning

Now that the blob versioning is enabled, let's see it in action.

Now, upload a file and see blob versioning in action

To test it out, upload a file with the same name with varying content to the container multiple times. Now find the file in the container which you have just uploaded. You can use the Azure portal to upload the file or use the following code

Click on the View previous versions option. You will see all the versions of the file listed with timestamp.

Now let’s write some code to list all the versions of the uploaded files in the container.

Listing all the versions of an uploaded file

Blob versioning features can be programmatically accessed using the Azure Storage client library for .NET, version 12.5.1, or later. However, I couldn’t find a way to list down the versions of all the files in the container using this nuget.

So, I did a little hack. From the Azure Portal — I clicked on the View Previous Versions and I intercepted the REST API url from the Network tab of developer tools.

Version 2019–10–10 and higher of the Azure Storage REST API supports blob versioning. Using the below code, I was able to call REST API url https://{accountname}.blob.core.windows.net/{containername}?restype=container&comp=list&include=versionsand retrieve all the versions of all the files in the container.

Downloading a specific version

To download a specific version of a file use WithVersion. This line var blob = blobClient.WithVersion(fileVersion); in the code below is doing all the magic.

Restoring a file to a specific version

One of the most important functionalities if any document library is to restore any file version and make it as the current version. To restore to a specific version, you must use the copy blob function. I found StartCopyFromUri very helpful since I could pass the VersionId of the file with the blob url query parameter as https://{0}.blob.core.windows.net/{1}/{2}?versionid={3}

Deleting a specific version

Last but not the least, you can delete a specific version of a file from the storage container. Again .WithVersion will help you here to delete a specific version as below

Wrap up

In this post, I have covered a few key features of a Document library which you can achieve with Azure Blob Versioning and a few lines of code. This solution is not meant to replace SharePoint or any other document management solutions. I felt this would become very handy to retain document versions, restore a document, delete a specific version of a custom build application that uses Azure Storage Account as storage infrastructure.

Originally published at https://subhankarsarkar.com on September 26, 2020.

--

--