Hi Everyone,
This blog is an addendum to the Working with Azure Storage Tables from PowerShell post, to talk about the new capability of the Azure Rm Storage Tables PowerShell module that now supports Azure Cosmos DB Tables, for more information on Azure Cosmos DB, please refer to Azure Cosmos DB Documentation.
This post will show how to add this support, notice that Cosmos DB Table API is in preview therefore this module is not well suited at this moment for production workloads and is very prone to frequently changes.
In order to use it against Azure Cosmos DB, the first requirement is that your Azure Cosmos DB is created with support for Table API as shown below:
Although I added support to the module, it does not come with necessary required DLLs to access Azure Cosmos DB tables, in order to add that support to your local copy of the module, please follow these steps:
- Make sure you followed all the steps to install the module outlined here (same link mentioned in the first paragraph of this post).
- Open an elevated PowerShell command prompt
- Change folder to the root folder of the module
- Download the file https://raw.githubusercontent.com/paulomarquesc/AzureRmStorageTable/master/Install-CosmosDbInstallPreReqs.ps1 from GitHub and save it with the same name to that folder. You can use the following code snippet to download the file from PowerShell:
$filename = "$pwdInstall-CosmosDbInstallPreReqs.ps1"
$webclient = New-Object System.Net.WebClient
$url = "https://raw.githubusercontent.com/paulomarquesc/AzureRmStorageTable/master/Install-CosmosDbInstallPreReqs.ps1"
$webclient.DownloadFile($url,$filename)
- Execute the script:
.Install-CosmosDbInstallPreReqs.ps1
- This will add the following DLLs to your module folder, without these DLLs the module won’t work with Azure Cosmos DB
Notice that since this is preview API, these files are prone to change more frequently than the normal and changes to the script may need to be done in order to add them, specially the versions of the nuget packages.
After you add the necessary assemblies the operation of the cmdlets are exactly the same, with a small difference that the Azure Cosmos DB table API at this moment has a bug related to empty strings for partition and row keys at this moment so there is a test that is performed when trying to use empty strings with it and the following error will show up:
Cosmos DB table API does not accept empty partition or row keys when using CloudTable.Execute operation, because of this we are disabling this capability in this module and it will not proceed.
At C:Program FilesWindowsPowerShellModulesAzureRmStorageTableAzureRmStorageTableCoreHelper.psm1:32 char:9
+ Throw $cosmosDBEmptyKeysErrorMessage
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (Cosmos DB table…ll not proceed.:String) [], RuntimeException
+ FullyQualifiedErrorId : Cosmos DB table API does not accept empty partition or row keys when using CloudTable.Execute operation, because of this we are disabling this capability in this module and it will not proceed.
Another change performed in the module is that now there is a cmdlet called Get-AzureStorageTableTable and it gets the reference for the table you want to work with, independently if it is Azure Storage Table or Azure Cosmos DB table. The examples below shows how to get each table:
Azure Storage Table
$resourceGroup = "myResourceGroup"
$storageAccount = "mystorageaccount"
$tableName = "table01"
$table = Get-AzureStorageTabletable -resourceGroup $resourceGroup -tableName $tableName -storageAccountName $storageAccount
Azure Cosmos DB Table
$resourceGroup = "myResourceGroup"
$databaseName = "myCosmosDb"
$tableName = "table01"
$table01 = Get-AzureStorageTabletable -resourceGroup $resourceGroup -tableName $tableName -databaseName $databaseName
That’s it for this post, stay tuned for updates.
For more information please refer to:
Azure Rm Storage Table PowerShell module source
https://github.com/paulomarquesc/AzureRmStorageTable
Module at PowerShell Gallery
https://www.powershellgallery.com/packages/AzureRmStorageTable
Frequently asked questions about Azure Cosmos DB
https://docs.microsoft.com/en-us/azure/documentdb/documentdb-faq
Azure Cosmos DB: Build a .NET application using the Table API
https://docs.microsoft.com/en-us/azure/cosmos-db/create-table-dotnet
Regards
Paulo