When I run my script below it keeps failing with "The ImportExport operation failed because of invalid storage credentials."
The access key and SAS all seem to be correct when I check in the portal. Can someone please point me in the right direction
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: $_"
}
}
2
Answers
I have reproduced in my environment and got expected results as below and I followed Microsoft-Document and Micrsoft-Document2:
Firstly, I have got Access key as below:
Then have used below command:
Output in Portal:
Here you don’t need SAS token and try to follow above process.
For anyone else (like me) that finds this page via Google. The "invalid storage credentials" error also appears when you supply an invalid URL for the blob that holds the export.
To test this, try and download the blob with your account key through the CLI. This is an example command to test the URI: