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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#Paolo Frigo, https://scriptinglibrary.com <# .SYNOPSIS Add a file to a blob storage .DESCRIPTION This function leverages the Azure Rest API to upload a file into a blob storage using a SAS token. .PARAMETER file Absolute path of the file to upload .PARAMETER connectionstring Uri and SAS token .EXAMPLE Add-FileToBlogStorage -file "FULL_PATH" -connectionstring "BLOBSTORAGE_URI_WITH_SAS_TOKEN" #> function Add-FileToBlobStorage{ Param( [Parameter(Mandatory=$true)] [ValidateScript({Test-Path $_ })] [string] $file, [Parameter(Mandatory=$true)] [ValidateScript({$_ -match "https\:\/\/(.)*\.blob.core.windows.net\/(.)*\?(.)*"})] [string] $connectionstring ) $HashArguments = @{ uri = $connectionstring.replace("?","/$($(get-item $file).name)?") method = "Put" InFile = $file headers = @{"x-ms-blob-type" = "BlockBlob"} } Invoke-RestMethod @HashArguments } |
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
1 |
invoke-restmethod -uri "your_uri_with_sas_token" |
I hope you will find it useful! As usual, you can find this script on my Github repository.
using Gs Richcopy 360 and Carbonite to upload to Azure Blob is more easy