Azure: MVC application to refer js from blob
When deploying your MVC application package to Microsoft Azure, all the script files are deployed to Azure within the package itself. You can give direct reference to js files within your views or you can bundle those files and then give the reference to the bundle(s). More information about bundling is available here.
Once you have such an application deployed live into Azure then users will expect you to quickly fix any error they see.
If a fix involves code change (that means C# change) then you need to redeploy the package after your fix. This will reset your application and can mean downtime.
If a fix involves only js code change then we can avoid Azure deployment and just fix the bug in the js file and upload to the Azure blob. will solve your problem with zero downtime. More information about Azure storage, you can be found here.
So, here I am pointing to the following architectural implementations:
- You need to deploy your application to Microsoft Azure having your web and worker roles
- Keep all the js files into the Azure blob
- In your view files give the reference of js files in the following manner:
If(application runs in debug mode)
Refer local js files
Else
Refer js from the blob
After reading this much information about this post you might be thinking about CDN. But the extra feature/thing you can achieve with the given approach is, you can keep your blob as a private one. While CDN is a public one which is accessible to all.
Now, to automate the process of uploading the js files from your local machine to blob I have a simple solution for you. Here is the PowerShell that will help you to upload the js files from your local hard drive to Azure blob container.
NOTE: Here I am assuming that you have blob container having the name as "scripts".
Param(
$StorageContainer = "scripts"
)
# The script has been tested on PowerShell 3.0
Set-StrictMode -Version 3
# Following modifies the Write-Verbose behavior to turn the messages on globally for this session
$VerbosePreference = "Continue"
Get-AzurePublishSettingsFile
$AzurePublishSettingFilePath = Read-Host 'Please enter the local path for the downloaded publish setting file: '
Import-AzurePublishSettingsFile $AzurePublishSettingFilePath
$EnvironmentName = Read-Host 'Please enter the storage account name: '
Set-AzureSubscription -SubscriptionName "<provide your subscription name>" -CurrentStorageAccount $EnvironmentName
# Delete the existing files if any
$deleteBlobs = "Delete the blob content for the container " + $StorageContainer
Write-Warning -Message $deleteBlobs
$allBlobs = Get-AzureStorageBlob -Container $StorageContainer
foreach($blob in $allBlobs)
{
Remove-AzureStorageBlob -Container $StorageContainer -Blob $blob.Name
}
$deleteBlobs = "Deletion completed."
Write-Warning -Message $deleteBlobs
# Check if Windows Azure PowerShell is available
if ((Get-Module -ListAvailable Azure) -eq $null)
{
throw "Windows Azure PowerShell not found! Please install from http://www.windowsazure.com/en-us/downloads/#cmd-line-tools"
}
workflow UploadFilesInParallel
{
param(
# The name of the storage container to copy files to.
[Parameter(Mandatory = $true)]
[string]$StorageContainer,
# An array of files to copy to the storage container.
[Parameter(Mandatory = $true)]
[System.Object[]]$Files
)
if ($Files.Count -gt 0)
{
foreach -parallel ($file in $Files)
{
$textMsg = "File FullName " + $file.FullName
$textMsg1 = $textMsg -split "Scripts"
#Write-Warning -Message $textMsg1[1]
$stringaftersplit1 = $textMsg1[1] -split "\\"
if($stringaftersplit1.Count -eq 2)
{
$stringaftersplit = $textMsg1[1] -split "\\"
$blobFileName = $stringaftersplit[1] -replace '\\',''
Write-Warning -Message $blobFileName
}
else
{
$blobFileName = $textMsg1[1].Substring(1)
#Write-Warning -Message $blobFileName
}
#$blobFileName = Split-Path -Path $file.Name -NoQualifier
try
{
Set-AzureStorageBlobContent -Container $StorageContainer `
-File $file.FullName -Blob $blobFileName `
-ConcurrentTaskCount 0 -Force
}
catch
{
$warningMessage = "Unable to upload file " + $file.FullName
Write-Warning -Message $warningMessage
}
}
}
}
$LocalPath = Read-Host 'Please enter the local file path for the script folder: '
# Ensure the local path given exists. Create it if switch specified to do so.
if (-not (Test-Path $LocalPath -IsValid))
{
throw "Source path '$LocalPath' does not exist. Specify an existing path."
}
$files = ls -File $LocalPath -Recurse
if ($files -ne $null)
{
# Upload the files to storage container.
$fileCount = $files.Count
$time = [DateTime]::UtcNow
UploadFilesInParallel -StorageContainer $StorageContainer -Files $files
$duration = [DateTime]::UtcNow - $time
}
else
{
Write-Warning "No files found."
}