GreenLoop IT Solutions : Articles

Automation Course Part 9 – Interacting with Azure Storage

Azure Storage provides different types of data storage options, including tables, blobs, and queues. Azure Functions, on the other hand, provide a serverless compute environment to run small pieces of code that can be triggered by various events. In this knowledge-base, we will explore how to interact with Azure Storage objects, specifically tables, blobs, and queues, using PowerShell within an Azure Function App.

First, let’s start by creating a new Azure Function App. Navigate to the Azure portal and create a new Function App resource. Choose the consumption plan and select PowerShell as the language for the function app.

Next, we need to add a connection to the Azure Storage account. Navigate to the “Platform features” section and select “Storage accounts”. From there, select the “Add” button and create a new storage account or select an existing one.

To interact with storage objects, we need to install the Azure.Storage module. We can do this by adding the following command to our function app’s requirements.psd1 file:

@{'Az'= '6.*''Azure.Storage'= '3.*'}

This command will ensure that the required modules are installed in the function app.
Now, let’s look at how to interact with the different storage objects.

Azure Storage Tables

Azure Storage Tables provide a NoSQL key-value store that can be used to store structured data. We can use the Azure.Storage.Table module to interact with tables. To get started, we need to create a reference to the storage account by adding the following code to our PowerShell script:

 $storageAccountName = "your_storage_account_name" 
$storageAccountKey = "your_storage_account_key" 
$ctx = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey

Now, let’s create a new table and add some data to it:

$tableName = "mytable" $table = Get-AzStorageTable -Name $tableName -Context $ctx -ErrorAction SilentlyContinue
if ($table -eq $null) {
$table = New-AzStorageTable -Name $tableName -Context $ctx
}
$entity = New-Object "Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity"
$entity.PartitionKey = "partition1"
$entity.RowKey = "row1"
$entity.Properties.Add("Name", "John Doe")
$entity.Properties.Add("Age", 35)
$table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Insert($entity))

In this example, we create a new table if it doesn’t exist, then create a new entity and add it to the table.

To retrieve data from the table, we can use the Get-AzTableRow cmdlet:

$results = Get-AzTableRow -Table $table -PartitionKey "partition1" -RowKey "row1"
foreach ($result in $results) {
Write-Output "Name: $($result.Properties.Name)"
Write-Output "Age: $($result.Properties.Age)"
}
This code retrieves the entity we added earlier and displays its properties.
Note: any time we have a “$results” variable in PowerShell and need to iterate over each item, it’s conventional to do $result/$results like above:
foreach ($item in $items) {
<do stuff with each $item>
}
like this.

Azure Storage Blobs

Azure Storage Blobs provide a way to store unstructured data such as text or binary data. We can use the Azure.Storage.Blob module to interact with blobs. To get started, we need to create a reference to the storage account by adding the following code to our PowerShell script:
$storageAccountName = "your_storage_account_name"
$storageAccountKey = "your_storage_account_key"
$ctx = New-AzStorageContext -Storage