Star-forming_region_S106_(captured_by_the_Hubble_Space_Telescope)

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 `
}

2 responses to Using #PowerShell to Upload Content to #Azure Storage

  1. 

    Nice post. I can see this really coming in handy. I love that Azure and PowerShell work so well together!

    Like

  2. 

    Nice post Buddy !
    Working with blob is so simple and cheap :)

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s