azure - SQL Azure - 由于存储凭据无效,导入导出操作失败

标签 azure powershell azure-storage-account bacpac

当我运行下面的脚本时,它不断失败,并显示“由于存储凭据无效,导入导出操作失败。” 当我检查门户时,访问 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

enter image description here

然后使用以下命令:

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"

enter image description here

门户中的输出:

enter image description here

这里您不需要 SAS token 并尝试遵循上述流程。

关于azure - SQL Azure - 由于存储凭据无效,导入导出操作失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76043575/

相关文章:

mysql - 无法将 Azure 应用服务中的 NodeJS 应用连接到 Azure MySQL 数据库

azure - 直接在 Azure Batch 中使用 Windows 容器

c# - AppFabric 缓存突然过期

azure - 如何使用 Powershell 将所有 secret 从一个 Azure Keyvault 复制到另一个 Azure Keyvault

regex - 在指定字符串之前获取数字

azure - 无法从门户创建存储帐户 : name already taken

python - 定义 Azure IoT 中心客户端 (Python) 使用的代理

azure - Azure 机器人服务的 Directline 没有响应并被 CORS 策略阻止

r - dbutils.fs.mount() 适用于 Python,但不适用于 R?

Azure 存储帐户 - 克隆文件共享?