SQL Server 从 Azure 存储 Blob 容器上的备份文件还原(使用 SAS 令牌)

pyg*_*gri 1 restore azure-blob-storage sql-server-2019

我已在 azure blob 存储容器中设置了 SAS 令牌。目前它是有效的,正如 Windows cmd.exe 中的此命令有效的事实所证明的那样:

azcopy list "https://<mystorageaccountname>.blob.core.windows.net/<mystorageaccountcontainername>/path/to/file.BAK?<SAS token>"
Run Code Online (Sandbox Code Playgroud)

并作为输出返回:

INFO: ;  Content Length: 7.58 GiB
Run Code Online (Sandbox Code Playgroud)

现在,如果我通过 SSMS 在 SQL Server 中发出以下命令(服务器在发出 azcopy 命令的同一台计算机上本地运行,并且 <> 括号中的值与上面相同。请注意,我末尾没有“/”容器名称,并且令牌中没有“?”,遵循 Microsoft 文档的建议):

CREATE CREDENTIAL [https://<mystorageaccountname>.blob.core.windows.net/<mystorageaccountcontainername>] 
  -- this name must match the container path, start with https and must not contain a forward slash at the end
WITH IDENTITY='SHARED ACCESS SIGNATURE' 
  -- this is a mandatory string and should not be changed   
 , SECRET = '<SAS token>';
GO

RESTORE HEADERONLY FROM
URL = N'https://<mystorageaccountname>.blob.core.windows.net/<mystorageaccountcontainername>/path/to/file.BAK';
Run Code Online (Sandbox Code Playgroud)

RESTORE 命令后出现错误:

Msg 3201, Level 16, State 2, Line 28
Cannot open backup device 'https://<mystorageaccountname>.blob.core.windows.net/<mystorageaccountcontainername>/path/to/file.BAK'. Operating system error 3(The system cannot find the path specified.).
Msg 3013, Level 16, State 1, Line 28
RESTORE HEADERONLY is terminating abnormally.
Run Code Online (Sandbox Code Playgroud)

我检查并仔细检查路径中没有拼写错误,并且标记是相同的。<SAS token>已格式化

sv=<date>&st=<datetime>&se=<datetime>&sr=c&sp=rl&sig=<sig_token>
Run Code Online (Sandbox Code Playgroud)

最后一点,如果我在恢复之前删除凭据,我会收到不同的错误消息

Operating system error 86(The specified network password is not correct.).
Run Code Online (Sandbox Code Playgroud)

所以看起来令牌有效......

我希望能就这里可能出现的问题提供一些建议,或者如何解决它的行为与 azcopy 实用程序不同的原因。

编辑 1:已对存储文件路径的区分大小写进行了评论。

<mystorageaccountname><mystorageaccountcontainername>在 Microsoft Azure 存储资源管理器以及 azcopy 和 SQL 命令中均为小写。

path/to/file.BAK大小写混合,并且在 Microsoft Azure 存储资源管理器以及 azcopy 和 SQL 命令中也相同。

pyg*_*gri 5

我找到了问题的原因。

要使用 azcopy 列出文件,只需在 SAS 设置中分配读取权限即可。

但是,为了执行诸如 之类的命令RESTORE HEADERONLY,您需要在 SAS 设置中同时拥有读取写入权限。

创建具有读写属性的新 SAS 令牌后,它起作用了。