【问题标题】:Azure New-AzureStorageBlobSasToken incorrect fulluri string being returnedAzure New-AzureStorageBlobSasToken 返回不正确的 fulluri 字符串
【发布时间】:2026-01-16 06:55:01
【问题描述】:

我正在尝试使用 AzureRM PowerShell 模块在命令行中为 Blob 容器(和子文件夹数据)生成新的 SAS 令牌。 此过程在门户中导航并为指定文件手动创建 SAS 令牌时有效,但在使用 PS 时失败

$SAResourceGroupName="someresourcegroupname"
$StorageAccountName="randomstorageaccountnamehere"

$StorageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $SAResourceGroupName -AccountName $StorageAccountName).Value[1]
$Context=New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey

$tmpStart = Get-Date
$tmpEnd = $tmpStart.AddHours(0.5)

$Starttime = ($tmpStart).ToString("yyyy-MM-ddTHH:mm:ssZ")
$EndTime = ($tmpEnd).ToString("yyyy-MM-ddTHH:mm:ssZ")


$SASToken = New-AzureStorageBlobSASToken -Blob $StorageAccountName -Container "ContainerNameHere/ToolsSubFolder/randomfile.ZIP" -Context $Context -Permission r -StartTime $StartTime -ExpiryTime $EndTime -FullURI

生成的结果 SAS 令牌有两次 $StorageAccountName,并且格式化是在 HTML 中完成的,因此令牌本身没有正确的字符。

(数据已清理)

PS C:\Users\lsierra> New-AzureStorageBlobSASToken -Container “ContainerNameHere/ToolsSubFolder/randomfile.ZIP”-Blob $StorageAccountName -Permission r -Context $Context -FullUri https://randomstorageaccountnamehere.blob.core.windows.net/ContainerNameHere/ToolsSubFolder/randomfile.ZIP/randomstorageaccountnamehere?sv=2017-07-29&sr=b&sig=kXzYwqW%2BjKH1BAXwsBovVzCbGY2XzLxUY BxKQNkeqns%3​​D&se=2018-11-02T18%3A02%3A02Z&sp=r

如果我导航到门户并手动生成新的 SAS 令牌,则 FullURI 在内容和格式上都是正确的。

PowerShell v5.1 视窗 10

【问题讨论】:

    标签: azure powershell azure-storage azure-powershell


    【解决方案1】:

    问题是由您的最后一条命令引起的:

    $SASToken = New-AzureStorageBlobSASToken -Blob $StorageAccountName -Container "ContainerNameHere/ToolsSubFolder/randomfile.ZIP" -Context $Context -Permission r -StartTime $StartTime -ExpiryTime $EndTime -FullURI
    

    在你的情况下,应该是:

    $SASToken = New-AzureStorageBlobSASToken -Blob "ToolsSubFolder/randomfile.ZIP" -Container "ContainerNameHere" -Context $Context -Permission r -StartTime $StartTime -ExpiryTime $EndTime -FullURI
    

    所以你的完整的 powershell 脚本如下所示,试试吧,它在我这边运行良好。

    $SAResourceGroupName="someresourcegroupname"
    $StorageAccountName="randomstorageaccountnamehere"
    
    $StorageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $SAResourceGroupName -AccountName $StorageAccountName).Value[1]
    $Context=New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
    
    $tmpStart = Get-Date
    $tmpEnd = $tmpStart.AddHours(0.5)
    
    $Starttime = ($tmpStart).ToString("yyyy-MM-ddTHH:mm:ssZ")
    $EndTime = ($tmpEnd).ToString("yyyy-MM-ddTHH:mm:ssZ")
    
    $SASToken = New-AzureStorageBlobSASToken -Blob "ToolsSubFolder/randomfile.ZIP" -Container "ContainerNameHere" -Context $Context -Permission r -StartTime $StartTime -ExpiryTime $EndTime -FullURI
    

    我的测试样本:

    有关New-AzureStorageBlobSASToken的使用详情,请参阅此link

    【讨论】: