如何使用resthell通过powershell创建azure存储表

时间:2017-01-18 09:10:48

标签: powershell azure-table-storage

我知道如何使用PowerShell和portal创建azure存储表,有人可以指导如何使用rest api创建它

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

1 个答案:

答案 0 :(得分:1)

以简单的方式,您可以使用New-AzureStorageTable cmdlet创建存储表,如下所示:

#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

有关详细信息,您可以按照此官方tutorial了解如何在Azure存储中创建表。

此外,您可以利用Invoke-restmethod调用Create Table REST API在您的存储帐户中创建一个表。您可以按照以下命令执行:

#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

结果:

enter image description here