26 Nov 2023
Cannot open backup device 'https://blobstorage.blob.core.windows.

Msg 3201, Level 16, State 2, Line 9
Cannot open backup device 'https://blobstorage.blob.core.windows.net/container/AdventureWorks2016.bak'. Operating system error 5(Access is denied.).
RESTORE DATABASE is terminating abnormally.
The error occurs when trying to restore a SQL database from a backup stored in a Azure storage blob container.
The message suggests SQL instance is unable to access the backup file located at Azure blob storage container.
Fix:
- Ensure all the required permissions are selected while creating the SAS token for the container.
- Ensure your public IP address is added in "Allowed IP address" value while creating the SAS token for the container.
- Ensure storage account's networking setting Allows public connectivity.
The below snip shows how to create a SAS token for Azure blob storage container:
Drop any older credentials that might be present at your instance for the same Azure blob storage container, and recreate new one:
-- Drop Existing Credential
IF EXISTS
(SELECT * FROM sys.credentials WHERE name = 'https://blobstorage.blob.core.windows.net/dbcontainer')
DROP CREDENTIAL 'https://blobstorage.blob.core.windows.net/dbcontainer'
-- Create New Credential
CREATE CREDENTIAL [https://blobstorage.blob.core.windows.net/dbcontainer]
WITH IDENTITY = 'SHARED ACCESS SIGNATURE',
SECRET = 'sp=racwdl&st=2021-02-20T16:59:39Z&se=2021-02-23T00:59:39Z&sip=45.250.227.132&spr=https&sv=2020-02-10&sr=c&sig=cXNdGD9lMh2Mj%2FnkjpKV5W53IC8yX%2BVivFJBy6BGH7Q%3D';
-- for SECRET parameter, use the SAS token generated earlier for Azure blob storage container.
Once the credential is created, try restoring or backing up the database using URL parameter:
BACKUP DATABASEAdventureWorks2016
TO URL = 'https://blobstorage.blob.core.windows.net/dbcontainer/AdventureWorks2016.bak'
WITH COMPRESSION
,STATS = 5;
GO
--OR
RESTORE DATABASEAdventureWorks2016
FROM URL = 'https://blobstorage.blob.core.windows.net/dbcontainer/AdventureWorks2016.bak'