Monday, February 16, 2015

Upload all files in a folder to a Container in Azure Blob Storage using PowerShell

#TODO: Handle Errors
clear screen
 
$bgcolor="Gray"
$fgcolor="Cyan"
#storage name
$storageAccountName = "sawestusdp"
#container name
$containerName="script"
#script name
$localScriptFolder="c:\dev\script"
 
#add your account
Write-Host "Going to add account..." -BackgroundColor $bgcolor -ForegroundColor $fgcolor
Add-AzureAccount
Write-Host "Added account." -BackgroundColor $bgcolor -ForegroundColor $fgcolor
 
#check your subscription
Write-Host "Going to print azure subscription." -BackgroundColor $bgcolor -ForegroundColor $fgcolor
Get-AzureSubscription
 
#ensure that you are in service management mode
Write-Host "Going to switch to service management mode..." -BackgroundColor $bgcolor -ForegroundColor $fgcolor
Switch-AzureMode -Name AzureServiceManagement
Write-Host "Switched to service management mode." -BackgroundColor $bgcolor -ForegroundColor $fgcolor
 
 
# Get the storage account key and context
Write-Host "Going to collect Storage context..." -BackgroundColor $bgcolor -ForegroundColor $fgcolor
$storageAccountKey = (Get-AzureStorageKey -StorageAccountName $storageAccountName).Primary
Write-Host "Storage Key $storageAccountKey" -BackgroundColor $bgcolor -ForegroundColor $fgcolor
 
#get storage contxt
$storageCtx = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
Write-Host "Storage context collected: $storageCtx" -BackgroundColor $bgcolor -ForegroundColor $fgcolor
 
#get container
Write-Host "Going to collect container..." -BackgroundColor $bgcolor -ForegroundColor $fgcolor
$scriptContainer=Get-AzureStorageContainer -Context $storageCtx -Name $containerName
Write-Host "$containerName collected: $scriptContainer" -BackgroundColor $bgcolor -ForegroundColor $fgcolor
 
#Upload each file in the local subfolder to the folder in the share
Write-Host "Going to upload script to container..." -BackgroundColor $bgcolor -ForegroundColor $fgcolor
$files = Get-ChildItem $localScriptFolder
foreach($file in $files){
  $totalFilePath = "$localScriptFolder\$file"
  $uploadedFile=Set-AzureStorageBlobContent -File "$totalFilePath"  -Container $containerName -Blob "$file" -Context $storageCtx -Force
  Write-Host "$totalFilePath uploaded to $containerName" -BackgroundColor $bgcolor -ForegroundColor $fgcolor
}
 

No comments:

Post a Comment