Temporary file download links in Azure Storage

Lets say that you have a project in which you want to make files available in Azure Storage for a limited amount of time. This is not only possible, it’s super simple! Here’s how you do it:

First, make something that will connect you to your Azure Storage Account:

image

The image above shows my class AttachedFileRepository and it’s constructor, where we do the regular stuff of connecting to a CloudBlobClient, which is the object that represent that class’s state.

 

Next, provide a time-limited download URL in the method:

image

The magic, of course, happens in the method GetContainerSharedAccessUri:

image

Basically, what I’m doing there, is to create a SharedAccessBlobPolicy object where I set the expiration time of the policy and limit the access to the blob to read only.

Once the policy is created, I can create a “Shared Access Signature” using that policy that will provide access to the blob even though it is in a private container, and the blob itself is marked as private.

Appending the sharedAccessSignature to the blob URI is all it takes to open up that blob for download for the set time. Ingenious!

Uses

If you need to protect your files, and grant access only to paying customers, then this will provide the customer with a time-bombed download URL. You could, for example, allow 2 minutes download link.

Another scenario is provding download links to only authenticated users, for example a class that wants access to some class-related files, must be logged on to get the list of files. If they attempt to publish the download link to someone else, that link will only be valid for the time that you’ve set.

About digitaldias

Software Engineer during the day, photographer, videographer and gamer in the evening. Also a father of 3. Pedro has a strong passion for technology, and gladly shares his findings with enthusiasm.

View all posts by digitaldias →

One Comment on “Temporary file download links in Azure Storage”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.