public/Copy-ToStorageContainer.ps1
<#
.Synopsis Syncs a local folder to an Azure storage account recursively. .Description The app is registered in the given tenant including reply URL and unique ID and gets access service principal. Optionally, permissions to external APIs can be added and granted. .Parameter TenantId The GUID of the AAD where you want to create the app registration in. .Parameter SubscriptionId The GUID of the Azure Subscription the target storage account is located at. .Parameter AccountName The unique name of the storage account. .Parameter ContainerName The name of the storage container where the files should be synced to. .Parameter SourceDir The path to the local directory or an UNC path where the files are located at. .Parameter ExpiryHours The expiration time for the SAS token in hours (defaults to 1). .Example Sync-AzdStorageContainer -TenantId 00000-00000000-000000-000 -SubscriptionId 00000-00000000-000000-000 -AccountName stoddyourname -AccountKey SECRETKEY== -ContainerName uploads -SourceDir C:\Temp\ Sync local folder C:\temp recursively to storage container #> Function Copy-ToStorageContainer { [CmdLetBinding()] param ( [Parameter(Mandatory = $true)] [string] $TenantId, [Parameter(Mandatory = $true)] [string] $SubscriptionId, [Parameter(Mandatory = $true)] [string] $AccountName, [Parameter(Mandatory = $true)] [string] $ContainerName, [Parameter(Mandatory = $true)] [string] $SourceDir, [int] $ExpiryHours = 1, [switch] $NoLogo ) begin { if (!$NoLogo.IsPresent) { Write-Logo $MyInvocation.MyCommand } New-FunctionStartup # ensure that we are at the correct subscription Set-SubscriptionContext -TenantId $TenantId -SubscriptionId $SubscriptionId -NoLogo if (!$?) { Write-HostError "Could not set context." return } } process { # get storage context $ctx = New-AzStorageContext -StorageAccountName $AccountName -UseConnectedAccount # calculate time stamps $startTime = Get-Date $expiryTime = $startTime.AddHours($ExpiryHours) # use context to retrieve SAS token $sas = Get-AzStorageContainer -Container $ContainerName -Context $ctx | New-AzStorageContainerSASToken -Permission rwdl -StartTime $startTime -ExpiryTime $expiryTime # perform the sync azcopy sync $SourceDir "https://$AccountName.blob.core.windows.net/$ContainerName$sas" --recursive=true } } |