How to upload files to Azure Blob Storage using Powershell via the REST API

When I need to upload files on Azure Blog Storage the tools that I generally use are Storage Explorer (installed on my workstation or the web version included in the portal) or AzCopy, but within a script, I would prefer using Azure RestAPI.
Before writing this article I searched similar blogs around this topic, the most interesting one was written by Roger Zander’s blog, the only difference is that I’ve defined a function and validated the parameters, using splatting it is a bit more elegant to read in my opinion.

The main benefit of using the REST API is that the script will not require any specific module and it’s extremely simple to use, especially if you follow a DRY approach with a helper/wrapper function.

How to generate a SAS token

In this article, I assume the reader knows how to configure an Azure Storage account and know how to generate a SAS token (https://docs.microsoft.com/en-us/azure/storage/common/storage-sas-overview?toc=/azure/storage/blobs/toc.json#how-a-shared-access-signature-works)

My Powershell Script

After importing the function using dot sourcing. It is quite simple to follow the example and replacing FULL_PATH and your URI with the provided SAS Token BLOBSTORAGE_URI_WITH_SAS_TOKEN.

Input Validation

The input validation is maybe the most simple and important feature of the function.  The full file path is validated and also the connectionstring is checked against a regular expression which makes sure you are using an address similar to this “https://XXXXXX.blob.core.windows.net/YYYYYY?ZZZZZZZZZZZZZZZZZZ”.

So it checks you are also using HTTPS, the URI of the storage account is on a blob.core.windows.net domain and after the “?” you are also providing something else and it is more likely the SAS token after that address.

Retrieve the file from Azure Storage using the URI and SAS token

To get the uploaded file from the blob storage a simple retrieve the content would be a oneliner

I hope you will find it useful! As usual, you can find this script on my Github repository.

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.