Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A PowerShell script to upload a folder to a blob container in an Azure Storage account.
Param(
[string]$storageAccountName = "the_default_storage_account_name",
[string]$containerName = "the_default_container_name",
[string]$resourceGroupName = "your_default_resource_group_name",
[int] $maxAge = 86400,
[int] $indexMaxAge = 3600,
[boolean]$isProductionBuild = $false,
[boolean]$skipBuild = $false,
[boolean]$isAzurePipelineBuild = $false,
[string]$localFolder = ".\dist"
)
function Get-MimeType() {
param([parameter(Mandatory=$true, ValueFromPipeline=$true)][ValidateNotNullorEmpty()][System.IO.FileInfo]$CheckFile)
begin {
Add-Type -AssemblyName "System.Web"
[System.IO.FileInfo]$check_file = $CheckFile
[string]$mime_type = $null
}
process {
if ($check_file.Exists) {
$mime_type = [System.Web.MimeMapping]::GetMimeMapping($check_file.FullName)
}
else {
$mime_type = "false"
}
}
end {
return $mime_type
}
}
if ($false -eq $skipBuild) {
# Build the UI first
if ($false -eq $isProductionBuild) {
write-host "Building the UI for non-prod"
npm run build
} else {
write-host "Building the UI for PROD"
npm run build:prod
}
write-host "UI build completed"
write-host "Launching Azure login pop-up.."
}
if ($false -eq $isAzurePipelineBuild) {
# Login into Azure
Login-AzureRmAccount
}
# The destination folder. BE SURE TO INCLUDE THE TRAILING SLASH IF YOU ENTER A NON-EMPTY VALUE HERE.
$destfolder = ""
$storageAccount = Get-AzureRmStorageAccount -ErrorAction Stop | where-object {$_.StorageAccountName -eq $storageAccountName}
If($storageAccount)
{
$storageAccountKey = Get-AzureRmStorageAccountKey -ResourceGroupName $storageAccount.ResourceGroupName -StorageAccountName $storageAccountName
$storageContext = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey[0].Value
$storageContainer = Get-AzureStorageContainer -Context $storageContext -ErrorAction Stop | where-object {$_.Name -eq $ContainerName}
If($storageContainer)
{
Write-Host "'$ContainerName' blob container exists."
}
Else
{
Write-Host "'$ContainerName' blob container not found. Will create it now.."
New-AzureStorageContainer -name $ContainerName -Context $storageContext -Permission Blob
Write-Host "Created blob container."
}
Invoke-Expression -Command "ls $localFolder"
$files = Get-ChildItem $localFolder -File -Recurse
foreach($file in $files)
{
Write-Host "Processing " + $file.Name
$directoryName = $file.Directory.Name
$filename = $file.Name
if ($directoryName -ne "dist") {
$blobName = $destfolder + $file.FullName.Substring($file.FullName.IndexOf("dist\") + 5)
} else {
$blobName = $destfolder + $filename
}
write-host "copying $directoryName\$filename to $blobName"
if ($file.Name -like "*mp4*") {
$mimeType = "video/mp4"
} elseif ($file.Name -like "*webm*") {
$mimeType = "video/webm"
} else {
$mimeType = $(Get-MimeType -CheckFile $file.FullName)
}
# For index.html, use the $indexMaxAge
if ($file.Name -eq "index.html") {
$maxAge = $indexMaxAge
}
$Properties = @{"CacheControl" = "max-age=$maxAge"; "ContentType" = $mimeType}
Set-AzureStorageBlobContent -File $file.FullName -Container $containerName -Blob $blobName -Context $storageContext -Force -Properties $Properties
}
write-host "All files in $localFolder uploaded to $containerName!"
} else {
Write-Warning "'$storageAccountName' storage account not found."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.