Last active
August 29, 2015 14:01
-
-
Save Salvodif/2a6eb1764fb08fd61c07 to your computer and use it in GitHub Desktop.
Upload Assets to Azure's Blob Storage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<AzureConf> | |
<Blob> | |
<CssContainer Name="styles"> | |
<FileFormat Ext="css" ContentType="text/css" /> | |
</CssContainer> | |
<JsContainer Name="scripts"> | |
<FileFormat Ext="js" ContentType="application/javascript" /> | |
</JsContainer> | |
<ImgContainer Name="images"> | |
<FileFormat Ext="png" ContentType="image/png" /> | |
<FileFormat Ext="jpg" ContentType="image/jpeg" /> | |
<FileFormat Ext="gif" ContentType="image/gif" /> | |
</ImgContainer> | |
</Blob> | |
<AzureAccountInfo> | |
<Name>NameOfYourBlobStorage</Name> | |
<Key>t4M9LTrKhWi5[.....]c9n1A==</Key> | |
</AzureAccountInfo> | |
</AzureConf> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param([string]$PathToAssetsDir, [string]$PathToConfigXml) | |
## Azure Assets Deployment Script | |
## | |
## This script is used to upload asset files to an Azure BLOB storage container. | |
if ($PathToAssetsDir -eq "") { | |
Write-Host "PathToAssetsDir parameter not provided, exiting" -ForegroundColor Red | |
exit 1; | |
} | |
if ($PathToConfigXml -eq "") { | |
Write-Host "PathToConfigXml parameter not provided, exiting" -ForegroundColor Red | |
exit 1; | |
} | |
Write-Host "Uploading assets to Azure." | |
# Get Azure config xml | |
[xml]$conf = Get-Content $PathToConfigXml | |
$name = $conf.AzureConf.AzureAccountInfo.Name | |
$key = $conf.AzureConf.AzureAccountInfo.Key | |
$containers = $conf.AzureConf.Blob.ChildNodes | |
$fileFormats = $conf.SelectNodes("//FileFormat") | |
$files = Get-ChildItem $PathToAssetsDir -Recurse -File | % -process { $_.FullName } | |
# Connect to Azure blob storage | |
$ctx = New-AzureStorageContext -StorageAccountName $name -StorageAccountKey $key | |
Write-Host "Connecting to Azure host..." -NoNewline | |
try | |
{ | |
$currContainers = Get-AzureStorageContainer -Context $ctx -ErrorAction Stop | % {$_.Name} | |
Write-Host " done." | |
Write-Host "Connected to Azure host $($ctx.BlobEndPoint)." | |
} | |
catch | |
{ | |
Write-Host "" | |
Write-Host "Unable to connect to Azure host: $_" -ForegroundColor Red | |
return | |
} | |
# Create containers in Azure if not present | |
foreach ($item in $containers) | |
{ | |
if ($currContainers -notcontains $item.Name) | |
{ | |
try | |
{ | |
New-AzureStorageContainer -Name $item.Name -Permission "Container" -Context $ctx -ErrorAction Stop | Out-Null | |
Write-Host "Created container $($item.Name)." | |
} | |
catch | |
{ | |
Write-Host "Unable to create container $($item.Name): $_" -ForegroundColor Red | |
} | |
} | |
} | |
# Deploy files to Azure | |
foreach ($file in $files) | |
{ | |
$file = Get-Item $file | |
$format = $fileFormats | ? {$_.Ext -like $file.Extension.TrimStart(".")} | |
$container = $format.ParentNode.Name | |
$contentType = $format.ContentType.ToString() | |
try | |
{ | |
Set-AzureStorageBlobContent -File "$file" -Container "$container" -Properties @{"ContentType"="$contentType"} -Context $ctx -Force -ErrorAction Stop | Out-Null | |
Write-Host "Uploaded $($file.Name) to $container container." | |
} | |
catch | |
{ | |
Write-Host "Unable to upload $($file.Name) to $container container: $_" -ForegroundColor Red | |
} | |
} | |
Write-Host "Finished upload." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment