Subhankar Sarkar

Information Technology Consultant, Solution architect, Software Engineer.

Document Versioning with Azure Blob Storage

  • Home
  • Blogs
  • Document Versioning with Azure Blob Storage

Document Versioning With Azure Blob Storage

Document Versioning With Azure Blob Storage

Some Background First

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.

Enable Blob Versioning While Creation

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 have to 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.

Search

Recent Blog Posts

  • The Plain and Simple Way to Create SPN and Service Connection for Azure DevOps Pipelines July 18, 2021
  • .NET 6 Azure Web App Deployment using Azure DevOps Pipelines July 3, 2021
  • Hosting Free Website with Serverless backend on Azure Static Web App May 16, 2021
  • Inner Source –  the key to improving software asset reusability April 15, 2021
  • Visual Studio Codespaces – Bringing the Power of Cloud to Your Development Environment October 24, 2020
  • Document Versioning with Azure Blob Storage September 26, 2020
  • One Dollar Deployment – the inexpensive way of deploying Single Page App in Azure December 1, 2019
  • .NET Core WebJob Console app CI/CD using Azure DevOps Pipelines July 20, 2019
  • WSL2 for Dockerized .NET Core application June 17, 2019
  • Azure DevOps – manage your application lifecycle in cloud September 30, 2018
  • Make your .NET Core HTTP endpoint to react to Custom Azure Event Grid Events July 21, 2018
  • SQL Information Protection – why should you care and what you can do about it? March 25, 2018
  • SQL Server on Linux – Install and use it with SQL Operations Studio December 24, 2017
  • Windows Subsystem for Linux – build ASP.NET Core MVC web app in Linux December 12, 2017
  • Run native Linux bash on Windows 10 December 10, 2017
  • Microsoft Loves Linux – what it means to you and your clients? December 4, 2017

My twitter feed

Subhankar SarkarFollow

Information Technology Consultant, Solution Architect, a Coffee person with a lot of enthusiasm for Tech. RT/Like/Shares are not endorsement. Views are my own.

Subhankar Sarkar
Retweet on TwitterSubhankar Sarkar Retweeted
Gartner_incGartner@Gartner_inc·
31 Dec 2021

We’re reinventing the future of work every day and it’s time to be more intentional about it — whether employees are working at home, remotely, in reimagined on-site locations or some combination of the 3. Visit our #FutureOfWork resource center here: https://gtnr.it/3JzwDiU

Reply on Twitter 1476957099343200265Retweet on Twitter 14769570993432002653Like on Twitter 14769570993432002656Twitter 1476957099343200265
Retweet on TwitterSubhankar Sarkar Retweeted
ddskierDeeDee Walsh 💻⛷🍩@ddskier·
22 Dec 2021

Convert #ASPdotNET Web Forms to Native Web App https://hubs.li/Q010BGZ10 cc: @csharpfritz #dotnetconf #dotnet

Reply on Twitter 1473665379478212609Retweet on Twitter 14736653794782126094Like on Twitter 14736653794782126093Twitter 1473665379478212609
Retweet on TwitterSubhankar Sarkar Retweeted
SoniaCuffSonia Cuff@SoniaCuff·
19 Dec 2021

OMG automated key rotation (preview) in #Azure Key Vault!

https://docs.microsoft.com/azure/key-vault/keys/how-to-configure-key-rotation?WT.mc_id=modinfra-0000-socuff

#security #secrets #KeyManagement #AzOps #Microsoft #secOps

Reply on Twitter 1472705493088944130Retweet on Twitter 147270549308894413029Like on Twitter 147270549308894413094Twitter 1472705493088944130
Retweet on TwitterSubhankar Sarkar Retweeted
MLRepositoriesAwesome Machine Learning Repositories@MLRepositories·
14 Dec 2021

spark: .NET for Apache® Spark™ makes Apache Spark™ easily accessible to .NET developers.
Lang: C#
⭐️ 1719
#MachineLearning
Author: @dotnetfdn
https://github.com/dotnet/spark

Reply on Twitter 1470825261277003790Retweet on Twitter 14708252612770037907Like on Twitter 147082526127700379020Twitter 1470825261277003790
Retweet on TwitterSubhankar Sarkar Retweeted
azure_pirateAzure Pirate 🏴‍☠️@azure_pirate·
10 Dec 2021

Ahoy Thar Swashbuckler!

New YouTube post from @NTFAQGuy called: Native SFTP in Microsoft Azure with Azure Storage

Check it out it here: https://youtube.com/watch?v=-0PPA0tJLKA

#Azure #AzureFamily #CloudFamily #AzurePirate

Reply on Twitter 1469327866169020416Retweet on Twitter 14693278661690204163Like on Twitter 14693278661690204161Twitter 1469327866169020416
Load More...

Follow Me

  • LinkedIn
  • Twitter
  • GitHub
  • reddit
Designed and developed by Subhankar Sarkar