问题
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