Skip to main content

Securing Blob Storage with SAS Tokens

Generate a read-only, time-limited Azure SAS token for one blob instead of sharing an account key, then test its expiry and revoke it.

Overview and What You Will Learn

A Shared Access Signature (SAS) is a URL token that grants limited access to an Azure Storage resource: one blob or container, a fixed set of permissions, and a built-in expiry time. In this lab, you will generate a user delegation SAS for a single blob with read-only access, prove it blocks every action you did not grant, watch a short-lived token expire, and revoke tokens on demand.

Why This Matters in Production

A SAS token limits both what a holder can do and for how long, while an account key limits neither. Imagine a developer at a payments company like Razorpay handing a partner a full Storage Account Access Key to download one report. That key grants read, write, and delete on every blob in the account, and it never expires until someone rotates it. A scoped SAS would have given the partner that one file and nothing more.

Core Principles

Azure Storage offers two very different ways to grant access, and the blast radius of each is not close.

flowchart TD P([Partner needs one report]) --> K[Share Access Key] P --> S[Generate scoped SAS] K --> KR[Whole account
Read, write, delete
No expiry] S --> SR[One blob
Read-only
Expires automatically] classDef start fill:#475569,color:#FFFFFF classDef risky fill:#EF4444,color:#FFFFFF classDef safe fill:#10B981,color:#FFFFFF class P start class K,KR risky class S,SR safe

A SAS carries three things inside the token: the resource it covers, the permissions it grants, and the time it stays valid. Azure offers three kinds, and the difference is what signs them:

  1. User delegation SAS is signed with Microsoft Entra ID credentials, works only for Blob storage, and is the recommended type. It lasts at most 7 days.
  2. Service SAS is signed with the account key and covers one storage service.
  3. Account SAS is signed with the account key and can span several services, so it has the widest reach.
Aspect Access key User delegation SAS
Scope Entire account One blob or container
Expiry Until rotated Built into the token
Signed with The key itself Entra ID identity
Remember

A SAS URL is a bearer token. Anyone who holds the full URL gets exactly the access it grants, with no further login, so treat it like a password.

Detailed Step-by-Step Practical Lab

How to Create the Resource Group and Storage Account

Storage account names are globally unique, lowercase, and 3 to 24 characters, so change the suffix if yours is taken.

Bash
export RG="rg-sas-lab-mumbai"
export SA="stsaslabrahul2026"
az group create --name $RG --location centralindia
az storage account create \
--name $SA \
--resource-group $RG \
--location centralindia \
--sku Standard_LRS \
--min-tls-version TLS1_2
How to Grant Yourself Data Access Without Using Keys

Creating the account does not give you data-plane rights. Assign yourself the Storage Blob Data Contributor role, which also lets you request a user delegation key, then wait a minute or two for it to propagate.

Bash
ME=$(az ad signed-in-user show --query id --output tsv)
SA_ID=$(az storage account show --name $SA --resource-group $RG --query id --output tsv)
az role assignment create \
--assignee-object-id $ME \
--assignee-principal-type User \
--role "Storage Blob Data Contributor" \
--scope $SA_ID
How to Create a Container and Upload a Test File
Bash
az storage container create \
--name partner-reports \
--account-name $SA \
--auth-mode login
echo "monthly settlement report" > settlement.txt
az storage blob upload \
--account-name $SA \
--container-name partner-reports \
--name settlement.txt \
--file settlement.txt \
--auth-mode login
How to Generate a Read-Only SAS Token for One Blob

This command asks Entra ID for a user delegation key and signs the token with it, so the account key is never touched. --full-uri returns the blob URL with the token already attached.

Bash
URL=$(az storage blob generate-sas \
--account-name $SA \
--container-name partner-reports \
--name settlement.txt \
--permissions r \
--expiry $(date -u -d "24 hours" '+%Y-%m-%dT%H:%MZ') \
--https-only \
--auth-mode login \
--as-user \
--full-uri \
--output tsv)
echo $URL

--permissions r means read-only, so the holder cannot overwrite, delete, or list anything else in the container.

How to Test That the SAS Token Works Only as Scoped

Download the file with no Azure login at all, then try an action the token does not allow.

Bash
curl -s "$URL"
# Blocked: a write attempt, expect a 403 status line
curl -s -i -X PUT -H "x-ms-blob-type: BlockBlob" --data "tampered" "$URL" | head -n 1
How to Observe SAS Token Expiry

Generate a second token that lives for only two minutes, confirm it works, wait, and try again.

Bash
SHORT_URL=$(az storage blob generate-sas \
--account-name $SA \
--container-name partner-reports \
--name settlement.txt \
--permissions r \
--expiry $(date -u -d "2 minutes" '+%Y-%m-%dT%H:%MZ') \
--https-only \
--auth-mode login \
--as-user \
--full-uri \
--output tsv)
curl -s -i "$SHORT_URL" | head -n 1 # expect 200 OK
sleep 150
curl -s -i "$SHORT_URL" | head -n 1 # expect 403 after expiry
How to Revoke User Delegation SAS Tokens Immediately

You cannot delete a single SAS, but you can revoke every user delegation key for the account, which invalidates all tokens signed with them at once.

Bash
az storage account revoke-delegation-keys \
--name $SA \
--resource-group $RG
# The 24-hour token from earlier is now rejected, even though it has not expired
curl -s -i "$URL" | head -n 1
Tip

Revocation can take a few moments to apply. If the first retry still returns 200, wait 30 seconds and try again.

How to Clean Up
Bash
az group delete --name $RG --yes --no-wait

Production Best Practices & Common Pitfalls

Security

Never share a Storage Account Access Key with a partner, contractor, or third-party app when a SAS would do the job. A leaked key exposes the whole account indefinitely, while a leaked SAS exposes only its scope until it expires.

Common Mistake

Pasting a SAS URL into chat, tickets, or application logs. The token is the credential, so anyone who sees the URL can use it until it expires or is revoked.

Tip

Make the expiry as short as the use case allows. A one-time download needs a few hours, not 30 days, and a short window shrinks the time a leaked token can be abused.

  • Prefer user delegation SAS over key-signed SAS. It ties the token to an Entra ID identity, avoids handling the account key, and can be revoked without rotating the key.
  • Scope every token to the narrowest resource. A single blob is safer than a container, which is safer than an account, so start narrow and widen only when needed.
  • Always require HTTPS. The --https-only flag stops the token from working over plain HTTP, where it could be sniffed.
  • Use stored access policies for key-signed service SAS. Deleting the policy revokes every token that references it, which is the only per-token revocation path for that type.
  • Remember that rotating an account key breaks key-signed SAS. Any long-lived service or account SAS still in use stops working the moment the key is rotated.
  • Disable shared key access once you no longer need keys. Run az storage account update --allow-shared-key-access false so only Entra ID authorization is accepted.

Quick Reference & Troubleshooting Commands

Command Description
az storage blob generate-sas --as-user --auth-mode login Generate a user delegation SAS for one blob
az storage container generate-sas Generate a SAS scoped to a whole container
az storage account revoke-delegation-keys Invalidate every user delegation SAS at once
az storage account keys renew Rotate a key, invalidating tokens signed with it
az storage account update --allow-shared-key-access false Block key-based authorization entirely
az role assignment create --role "Storage Blob Data Contributor" Fix 403 errors when generating or using data access

Explore More in Azure Storage Solutions

All 6 Topics

Frequently Asked Questions

Is Securing Blob Storage with SAS Tokens free to learn on DevOps Network?

Yes - this topic, like everything on DevOps Network, is 100% free with no paywall or sign-up gate.

What does the Securing Blob Storage with SAS Tokens topic cover?

Generate a read-only, time-limited Azure SAS token for one blob instead of sharing an account key, then test its expiry and revoke it.

Found a mistake, an outdated step or a broken link?