Generating SAS Tokens in Azure Blob Storage

What Are SAS Tokens?

SAS (Shared Access Signature) tokens provide time-limited, scoped access to your storage resources without exposing your account keys.


Types of SAS Tokens

TypeScopeUse Case
Account SASEntire storage accountCross-service access
Service SASSpecific service (blob, queue)Single service access
User Delegation SASSpecific container/blobDelegated access with Azure AD

Generate SAS Token via Portal

  1. Go to Storage Account → Containers
  2. Select a container
  3. Click Generate SAS token and URL
  4. Configure:
    • Permissions (Read, Write, Delete, List)
    • Start and expiry time
    • Allowed IP addresses (optional)
  5. Click Generate
  6. Copy the Blob SAS token

Generate SAS Token via CLI

# Generate container SAS token (valid for 1 hour)
az storage container generate-sas \
  --name mycontainer \
  --account-name mystorage001 \
  --permissions rwl \
  --expiry 2024-12-31T23:59Z

# Output example:
# ?sv=2023-08-03&ss=b&srt=sco&sp=rwl&se=2024-12-31T23:59:00Z&st=2024-01-01T00:00:00Z&spr=https&sig=...

Use SAS Token in Code

Construct Full URL

string sasToken = "sv=2023-08-03&ss=b&srt=sco&sp=rwl&se=2024-12-31T23:59:00Z&sig=...";
string containerUri = "https://mystorage001.blob.core.windows.net/mycontainer";

var blobContainerClient = new BlobContainerClient(
    new Uri($"{containerUri}?{sasToken}")
);

Upload with SAS

var blobClient = new BlobClient(
    new Uri("https://mystorage001.blob.core.windows.net/mycontainer/myfile.txt?sas-token"),
    new AzureSasCredential(sasToken)
);

await blobClient.UploadAsync("localfile.txt");

SAS Token Parameters

ParameterDescription
svStorage version
ssSigned service (b=blob, q=queue, f=file)
srtSigned resource type (s=service, c=container, o=object)
spSigned permissions (r read, w write, d delete, l list)
seExpiry time
stStart time
sipAllowed IP addresses
sprSigned protocol (https, http)
sigSignature

Security Best Practices

  1. Set appropriate expiry - Don't make it too long (1 hour to 1 day is typical)
  2. Use HTTPS - Always use https://
  3. Store securely - Don't hardcode in source control
  4. Prefer User Delegation SAS - Uses Azure AD instead of account key

When to Use SAS Tokens

  • Share specific files with external users
  • Time-limited access for mobile apps
  • Temporary access for partners
  • Load content from CDN

Azure Integration Hub - Beginner Level