Forum Discussion

TomWechsler's avatar
Nov 08, 2020

Create and use a SAS (Shared Access Signature) with the PowerShell in Azure!

 

Hi Azure friends,

 

I used the PowerShell ISE for this configuration. But you are also very welcome to use Visual Studio Code, just as you wish. Please start with the following steps to begin the deployment (the Hashtags are comments):

 

#The first two lines have nothing to do with the configuration, but make some space below in the blue part of the ISE

Set-Location C:\Temp
Clear-Host

 

#So that you can carry out the configuration, you need the necessary cmdlets, these are contained in the module Az (is the higher-level module from a number of submodules)

Install-Module -Name Az -Force -AllowClobber -Verbose

 

#Log into Azure
Connect-AzAccount

 

#Select the correct subscription

Get-AzContext

Get-AzSubscription

Get-AzSubscription -SubscriptionName "your subscription name" | Select-AzSubscription

 

#Variables
$location = "westeurope"
$rgname = "twstoragedemo"

#A file we use later
$today = Get-Date
New-Item -ItemType file -Path C:\Temp\test.txt -Force -value $today

#Create a Resource Group
New-AzResourceGroup -Name $rgname -Location $location

#Create a Storage Account
New-AzStorageAccount -Location $location -ResourceGroupName $rgname -Name twstorage75 -SkuName Standard_LRS

#We need at least one Storage Account Key
$keys = Get-AzStorageAccountKey -Name twstorage75 -ResourceGroupName $rgname

#Now we need to create Storage context
$context = New-AzStorageContext -StorageAccountName twstorage75 -StorageAccountKey $keys[0].Value

#Once we have it, let’s create a storage container
New-AzStorageContainer -Context $context -Name bilder

#Now we have required pre-requisites to create an SAS
$token = New-AzStorageContainerSASToken -Context $context -Name bilder -Permission rwd

#Now we need to create Storage Container context
$containercontext = New-AzStorageContext -SasToken $token -StorageAccountName twstorage75

#Let's upload a file to the Storage Container
Set-AzStorageBlobContent -Context $containercontext -Container bilder -File C:\Temp\test.txt

#List the blobs in the container
Get-AzStorageBlob -Container bilder -Context $context | select Name, Blobtype, LastModified
 
Now you have used the PowerShell to create an Azure Storage Account and an Shared Access Signature! Congratulations!
 

#Delete all resources (when you no longer need it)

Remove-AzResourceGroup -Name $rgname -Force

 

I hope this article was useful. Best regards, Tom Wechsler

 

P.S. All scripts (#PowerShell, Azure CLI, #Terraform, #ARM) that I use can be found on github! https://github.com/tomwechsler

Resources