当我运行下面的脚本时,它不断失败,并显示“由于存储凭据无效,导入导出操作失败。” 当我检查门户时,访问 key 和 SAS 似乎都是正确的。有人可以指出我正确的方向
param(
[string]$SubscriptionId,
[string]$ResourceGroupName,
[string]$storageRG,
[string]$ServerName,
[string]$DatabaseName,
[string]$StorageAccountName,
[string]$StorageContainerName,
[string]$StorageAccountKey,
[string]$ImportBacpacFilename,
[string]$AdministratorLogin,
[string]$AdministratorLoginPassword
)
Add-Type -AssemblyName System.Web
try {
# Set the subscription context
Write-Verbose "Setting subscription context..."
Select-AzSubscription -SubscriptionId $SubscriptionId
# Get the storage account
Write-Verbose "Getting storage account..."
$StorageAccount = Get-AzStorageAccount -ResourceGroupName $storageRG -Name $StorageAccountName
# Get the storage account context
Write-Verbose "Getting storage account context..."
$StorageAccountContext = $StorageAccount.Context
# Generate the SAS token for the storage container
$StartTime = Get-Date
$ExpiryTime = $StartTime.AddHours(6)
$SasTokenRaw = New-AzStorageContainerSASToken -Name $StorageContainerName -Permission rw -StartTime $StartTime -ExpiryTime $ExpiryTime -Context $StorageAccountContext
$SasToken = [System.Web.HttpUtility]::UrlEncode($SasTokenRaw)
$StorageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $storageRG -Name $StorageAccountName)[0].Value
# Build the destination URL
$Sourceurl = "https://$StorageAccountName.blob.core.windows.net/$StorageContainerName/$ImportBacpacFilename$SasToken"
# Import the Azure SQL Database to the Blob Storage container
$ImportRequest = New-AzSqlDatabaseImport -DatabaseName $DatabaseName -ResourceGroupName $ResourceGroupName -StorageKeyType StorageAccessKey -StorageKey $StorageAccountKey -StorageUri $Sourceurl -Edition Standard -ServiceObjectiveName S0 -DatabaseMaxSizeBytes 5000000 -AdministratorLogin $AdministratorLogin -AdministratorLoginPassword (ConvertTo-SecureString $AdministratorLoginPassword -AsPlainText -Force)-ServerName $ServerName
# Check the status of the import operation
$ImportStatus = Get-AzSqlDatabaseImportExportStatus -OperationStatusLink $ImportRequest.OperationStatusLink
[System.Console]::Write("Importing")
while ($ImportStatus.Status -eq "InProgress") {
Start-Sleep -s 30
$ImportStatus = Get-AzSqlDatabaseImportExportStatus -OperationStatusLink $ImportStatus.OperationStatusLink
[System.Console]::Write(".")
}
[System.Console]::Write("")
$ImportStatus
if ($ImportStatus.Status -eq "Succeeded") {
Write-Host "Import completed successfully!"
}
else {
Write-Host "Import failed. Status: $($ImportStatus.Status)"
}
}
catch {
Write-Error "An error occurred: $_"
}
}
最佳答案
我已经在我的环境中重现并得到了如下预期结果,我按照 Microsoft-Document 进行操作和 Micrsoft-Document2 :
首先,我有如下访问 key :
(Get-AzStorageAccountKey -ResourceGroupName "resource group name" -Name "name of storage account")[0].Value
然后使用以下命令:
New-AzSqlDatabaseExport -ResourceGroupName "resource group name" -DatabaseName "databasename" -StorageKeytype StorageAccessKey -StorageKey "PVggweeFl6SqaVjzHX3eKD7Qs7BWFqRpAG2AW/CvyLBYB/QezI+ASt9Ms02w==" -StorageUri "https://storageaccountname.blob.core.windows.net/rconatinername/blobaname" -AdministratorLogin "emo" -AdministratorLoginPassword $(ConvertTo-SecureString "Password" -AsPlainText -Force) -ServerName "servername"
门户中的输出:
这里您不需要 SAS token 并尝试遵循上述流程。
关于azure - SQL Azure - 由于存储凭据无效,导入导出操作失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76043575/