1
votes

I am attempting to use the AzureRM PowerShell Module to generate a new SAS Token for a Blob Container (and subfolder data) at the command line. This process works when navigating in the Portal and manually creating the SAS Token for the specified file, but is failing when using 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

The resulting SAS Token that is being generated has the $StorageAccountName twice, and the formatting is being done in HTML, so the token itself doesnt have the correct characters.

(data scrubbed)

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%3D&se=2018-11-02T18%3A02%3A02Z&sp=r

If I navigate to the Portal and manually generate a new SAS Token, the FullURI is correct, both in content and formatting.

PowerShell v5.1 Windows 10

1

1 Answers

-1
votes

The issue was caused by your last command:

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

In your case, it should be:

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

So your complete powershell script will like below, just try it, it works fine on my side.

$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

My test sample:

enter image description here

For more details about the usage of the New-AzureStorageBlobSASToken, refer to this link.