How To Upload files to Azure Blob Storage using PowerShell and AzCopy
Published May 30 2019 04:16 AM 27.6K Views
Microsoft

Azure Blob Storage is a great place to store files. In this post, I quickly wanted to show you how you can create a simple script to upload files to Azure blob storage using PowerShell and AzCopy. AzCopy is a command-line utility that you can use to copy blobs or files to or from a storage account. It is the recommended option for faster copy operations.
 

First, make sure you have AzCopy installed. If you didn’t configure the path variable, make sure you run this script from the path where AzCopy is stored. This example uses the SAS token to authenticate against the Azure Storage.

 

AzCopy Syntax:

 

azcopy copy SourcePath DestinationURI

 

AzCopy Example uploading a local image folder an Azure Blob Storage Container using a SAS token.

 

azcopy copy “C:\temp\images” “https://account.blob.core.windows.net/images/?sv=2018-03-28&ss=bjqt&srt=sco&sp=rwddgcup&se=2019-05-01T05:01:17Z&st=2019-04-30T21:01:17Z&spr=https&sig=MGCXiyEzbtttkr3ewJIh2AR8KrghSy1DGM9ovN734bQF4%3D" --recursive=true”

With this quick Azure PowerShell script, you can automatically generate the SAS token. If you are running on Linux or macOS, you can simply install the cross-platform PowerShell Core 6 version and the Azure Az PowerShell module.

 

# Connect to Azure
Connect-AzAccount
# List Azure Subscriptions
Get-AzSubscription

# Define Variables
$subscriptionId = "yourSubscriptionId"
$storageAccountRG = "demo-azcopy-rg"
$storageAccountName = "tomsaccount"
$storageContainerName = "images"
$localPath = "C:\temp\images"

# Select right Azure Subscription
Select-AzSubscription -SubscriptionId $SubscriptionId

# Get Storage Account Key
$storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $storageAccountRG -AccountName $storageAccountName).Value[0]

# Set AzStorageContext
$destinationContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey

# Generate SAS URI
$containerSASURI = New-AzStorageContainerSASToken -Context $destinationContext -ExpiryTime(get-date).AddSeconds(3600) -FullUri -Name $storageContainerName -Permission rw

# Upload File using AzCopy
azcopy copy $localPath $containerSASURI –-recursive

I hope this helps you to find a quick way to easily upload files to Azure Blob Storage containers. To learn more about how to interact with Azure PowerShell and Azure Blob Storage, check out the Microsoft Docs: Quickstart: Upload, download, and list blobs by using Azure PowerShell

If you have questions, let me know in the comments.

4 Comments
Version history
Last update:
‎May 06 2020 07:40 AM
Updated by: