Upload files to Blob Storage using Azure Power Shell
Hi all,
Often when working with azure you would need to upload content to your storage.
The below script uploads data to your storage container. the comments inline describe the every step
# The storage account to which you want to upload data to
$StorageAccountName = Read-Host 'Enter the Storage Account Name you created'
Write-Host "You have to download the publish settings file inorder to continue with the deployment"
Write-Host "You will be redirected to the azure site to download the file. the download will start automatically"
$name = Read-Host 'Press Enter to Continue'
# Downloads the publish settings file from your azure subscription
Get-AzurePublishSettingsFile
$name = Read-Host 'Pres Enter to Continue'
# Usee file dialog to browse the publish settings file. I am pointing it to Downloads folder of user
Write-Host "Browse the .publishsetttings file you have just downloaded"
$name = Read-Host 'Pres Enter to Continue'
$dialog = New-Object system.windows.forms.openfiledialog
$dialog.InitialDirectory = $home + "\Downloads"
$dialog.DefaultExt = '.publishsettings'
$dialog.Filter = 'Publish Settings|*.publishsettings|All Files|*.*'
$dialog.Multiselect = $false
$dialog.Title = "Select a Azure Subscriptions Publishsettings file"
$rc = $dialog.ShowDialog()
if ($rc -eq [System.Windows.Forms.DialogResult]::OK)
{
$dialog = $dialog.FileName
}
#note I am overriding the $dialog with file name
#imports the publish settings file
# the publish settings file has the certificate which lets you access the azure subscription .
Import-AzurePublishSettingsFile $dialog
Write-Host "Uploading to Storage Account"
$name = Read-Host 'Pres Enter to Continue'
# Setting the container name. Creates a container in which the files will be uploaded
$ContainerName = "AzureFiles"
# fetching the Key from the storage account. this is needed to set the context.
$storageAccountKey = Get-AzureStorageKey $storageAccountName | %{ $_.Primary }
Write-Host "Files will be uploaded to the storage account/Container"
#create a storage context
$context = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $storageAccountKey
#creates the container in your storage account. I am not if container already exists.
# you can check by get-storagecontainer and check for errors.
New-AzureStorageContainer $ContainerName -Permission Container -Context $context
# Getting the current folder from script execution path
$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath
#Appending the subfolder name data to yhe current script path. All data is should be in data folder of current script
$dir = $dir + "\Data"
# $_.mode -match "-a---" scans the data directory and ony fetches the files. It filters out all directories
$files = Get-ChildItem $dir -force| Where-Object {$_.mode -match "-a---"}
# iterate through all the files and start uploading data
foreach ($file in $files)
{
#fq name represents fully qualified name
$fqName = $dir + "\" + $file.Name
#upload the current file to the blob
Set-AzureStorageBlobContent -Blob $file.Name -Container $ContainerName -File $fqName -Context $context -Force
}
Comments
Anonymous
April 22, 2014
I thought, this would be useful. I added an ANT task to download/upload blobs from/to Azure: widgeterian.com/.../apache-ant-task-to-upload-and-download-filesblobs-tofrom-windows-azureAnonymous
October 08, 2014
Do you have one to download multiple files? Get-AzureStorageBlobCotent doesn't except wildcardsAnonymous
October 08, 2014
I figured it out using a pipeline! #download all files in Blob Get-AzureStorageBlob -Container $ContainerName -Context $context | Get-AzureStorageBlobContent -Destination 'C:tempDownload' -Force