Uploading Content to Azure Blobs
I use Azure Blobs on a regular basis. They’re generally really useful and help me through some tough situations. Working with Blobs is simple. You can interact with them using Visual Studio, third-party tools, REST and even through PowerShell.
The following PowerShell command demonstrates how I upload content to Azure Storage.
Set-BlobContent -StorageAccountName 'scaleupdowndemopkgs' ` -StorageContainer 'packages' ` -FilePath 'C:\Service\cloud_package.cspkg' ` -BlobName 'extra_small_vm_cloud_package.cspkg'
The following is the the PowerShell function invoked in the previous example. It finds your Azure Storage keys and creates a Blob Storage Container is it does not already exist. Then it uploads the contents of the provided file path to an Azure Blob.
function Set-BlobContent { Param ( [Parameter(Mandatory=$true)] [string] $StorageAccountName, [Parameter(Mandatory=$true)] [string] $StorageContainer, [Parameter(Mandatory=$true)] [string] $FilePath, [Parameter(Mandatory=$true)] [string] $BlobName ) # Setup the Storage Context used for Storage Operations $AccountKeys = Get-AzureStorageKey -StorageAccountName $StorageAccountName ` -Verbose ` -WarningAction Stop ` -ErrorVariable $e ` $StorageContext = New-AzureStorageContext -StorageAccountName $StorageAccountName ` -StorageAccountKey $AccountKeys.Primary ` -Verbose ` -WarningAction Stop ` -ErrorVariable $e ` try { Write-Verbose('Try create Container') $newcontainer = New-AzureStorageContainer -Context $StorageContext ` -Container $StorageContainer ` -Verbose ` -WarningAction Ignore ` -ErrorVariable $e ` } catch { Write-Verbose('Container already exists') } # Upload and rename the Cloud Service package to Blob Storage Set-AzureStorageBlobContent -File $FilePath ` -Container $StorageContainer ` -Context $StorageContext ` -Blob $BlobName ` -Force ` -Verbose ` -WarningAction Stop ` -ErrorVariable $e ` }
Nice post. I can see this really coming in handy. I love that Azure and PowerShell work so well together!
LikeLike
Nice post Buddy !
Working with blob is so simple and cheap :)
LikeLike