Hi @M, RAKESH,
To generate a SAS token for a specific folder using PowerShell, use the New-AzStorageContainerSASToken
cmdlet. The New-AzStorageBlobSASToken
cmdlet you're using is intended for individual blobs. Ensure you specify the folder path correctly in the -Blob parameter to generate a SAS token for a folder.
Because, Azure Blob Storage operates with a virtual directory structure, where "folders" are implicitly created through the blob name (e.g., A/B/C/test.txt represents a "folder path"). These folders are not actual objects in the storage account. Operations, such as generating a SAS token, specifically target individual blobs or containers rather than these virtual folders.
Please use the below script which helps in generating a SAS token for a folder:
Connect-AzAccount
# Set the storage account name and container name
$storageAccountName = "ABC"
$containerName = "ASD"
$folderPath = "A/B/C" # The folder for which you want the SAS token
# Set expiry to one year from the current date
$expiryTime = (Get-Date).AddYears(1)
# Define permissions (Read, Write, List)
$permissions = "racwl"
# Get the storage account key
$storageAccountKey = "XYZ"
# Create storage context using the storage account and key
$context = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
# Generate the SAS token for the folder
$sasToken = New-AzStorageContainerSASToken -Container $containerName -Permission $permissions -ExpiryTime $expiryTime -Context $context
# Construct the full SAS URL for the folder
$folderUrl = "https://$storageAccountName.blob.core.windows.net/$containerName/$folderPath"
$completeSasUrl = "$folderUrl?$sasToken"
# Output the complete SAS URL
Write-Output $completeSasUrl
Please make sure to replace the $storageAccountName
, $containerName
, $folderPath
, and $storageAccountKey
with your actual values. This script will generate a SAS token that grants the specified permissions for the entire folder.
For additional reference, please follow the below links:
I hope by following the above steps, you can be able to fix the issue
Please let us know in the comments below, if the issue is resolved or still persists. We will be glad to assist you closely.
Please do consider to “up-vote” wherever the information provided helps you, this can be beneficial to other community members.