How to create azure storage table using rest api via powershell

风格不统一 提交于 2019-12-25 08:03:09

问题


I have an idea how to create azure storage tables using PowerShell and portal, can some one guide how to create it using rest api like

$jsonbody = @{}
$authorization = ""
Invoke-restmethod -uri "" 

回答1:


For a simple way, you could leverage New-AzureStorageTable cmdlet to create your storage table as follows:

#Define the storage account and context.
$StorageAccountName = "yourstorageaccountname"
$StorageAccountKey = "yourstorageaccountkey"
$Ctx = New-AzureStorageContext $StorageAccountName -StorageAccountKey $StorageAccountKey

#Create a new table.
$tabName = "yourtablename"
New-AzureStorageTable –Name $tabName –Context $Ctx

For more details, you could follow this official tutorial about how to create a table in Azure Storage.

Also, you could leverage Invoke-restmethod to invoke Create Table REST API to create a table in your storage account. You could follow the following command:

#Define the storage account.
$StorageAccount = "yourstorageaccountname"
$Key = "yourstorageaccountkey"

$sharedKey = [System.Convert]::FromBase64String($Key)
$date = [System.DateTime]::UtcNow.ToString("R")

$tabName= "yourtablename"
$contentType="application/json"
$accept="application/json;odata=minimalmetadata"
$canonicalizedResource = "/Tables"
$x_ms_version="2015-04-05"
$stringToSign = "POST`n`n$contentType`n$date`n/$StorageAccount$canonicalizedResource"
$hasher = New-Object System.Security.Cryptography.HMACSHA256
$hasher.Key = $sharedKey

$signedSignature = [System.Convert]::ToBase64String($hasher.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($stringToSign)))

$authHeader = "SharedKey ${StorageAccount}:$signedSignature"

$headers = @{"x-ms-date"=$date
            "Authorization"=$authHeader
            "Accept"=$accept
            "Content-Type"=$contentType
            "x-ms-version"=$x_ms_version}

$hash=@{"TableName"=$tabName}
$json=$hash | convertto-json

Invoke-RestMethod -Method "POST" -Uri "https://$StorageAccount.table.core.windows.net/Tables" -Headers $headers -Body $json

Result:



来源:https://stackoverflow.com/questions/41715260/how-to-create-azure-storage-table-using-rest-api-via-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!