且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何通过PowerShell使用Rest API创建Azure存储表

更新时间:2022-05-22 00:58:48

通过简单的方法,您可以利用

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

有关更多详细信息,您可以关注此官方关于如何在Azure存储中创建表的教程.

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

此外,您可以利用Invoke-restmethod调用创建表 REST API即可在您的存储帐户中创建表.您可以遵循以下命令:

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

结果: