Storage access keys and SAS tokens in Azure - Time & Space Complexity
We want to understand how the time to access Azure Storage changes when using access keys versus SAS tokens.
Specifically, how the number of operations grows as we perform more storage requests.
Analyze the time complexity of generating and using SAS tokens versus using storage access keys.
// Using storage access key directly
var client = new BlobServiceClient(storageAccountUrl, new StorageSharedKeyCredential(accountName, accountKey));
await client.GetBlobContainerClient(containerName).GetBlobClient(blobName).DownloadAsync();
// Using SAS token
var sasToken = GenerateSasToken(containerName, permissions, expiry);
var sasClient = new BlobServiceClient(storageAccountUrl + "?" + sasToken);
await sasClient.GetBlobContainerClient(containerName).GetBlobClient(blobName).DownloadAsync();
This code shows two ways to access blobs: one with a storage key, one with a SAS token.
Look at what repeats when accessing storage multiple times.
- Primary operation: Blob download requests to Azure Storage service.
- How many times: Once per blob access, regardless of key or SAS token.
- Additional operation for SAS: SAS token generation, which happens once per token creation.
Each blob download requires one request. SAS token generation is a one-time cost.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 blob downloads + 1 SAS token generation (if using SAS) |
| 100 | 100 blob downloads + 1 SAS token generation |
| 1000 | 1000 blob downloads + 1 SAS token generation |
Pattern observation: The number of blob download calls grows linearly with n; SAS token generation stays constant.
Time Complexity: O(n)
This means the time grows directly in proportion to the number of storage accesses.
[X] Wrong: "Generating SAS tokens for each request is necessary and scales well."
[OK] Correct: Generating a SAS token each time adds extra overhead and is inefficient; usually one token is generated and reused for many requests.
Understanding how repeated storage access scales helps you design efficient cloud apps and shows you grasp practical cloud performance.
What if we generated a new SAS token for every blob access? How would the time complexity change?
Practice
Solution
Step 1: Understand storage access keys
Storage access keys give full control over the entire storage account without restrictions.Step 2: Understand SAS tokens
SAS tokens grant limited, time-bound access to specific resources or actions within the storage account.Final Answer:
Storage access keys provide full access; SAS tokens provide limited, temporary access. -> Option CQuick Check:
Access scope = Full vs Limited [OK]
- Confusing SAS tokens as full access keys
- Thinking SAS tokens never expire
- Assuming access keys are resource-specific
Solution
Step 1: Identify the correct Azure CLI command for container SAS
The command to generate a SAS token for a container isaz storage container generate-sas.Step 2: Check parameters
az storage container generate-sas --name mycontainer --permissions rwl --expiry 2024-12-31T23:59:00Z --account-name mystorage correctly uses container name, permissions, expiry, and account name parameters.Final Answer:
az storage container generate-sas --name mycontainer --permissions rwl --expiry 2024-12-31T23:59:00Z --account-name mystorage -> Option BQuick Check:
Correct CLI command and parameters [OK]
- Using 'az storage blob generate-sas' for containers
- Omitting required parameters like --account-name
- Using incorrect command syntax
?sv=2023-01-01&ss=b&srt=sco&sp=rl&se=2024-07-01T00:00:00Z&st=2024-06-01T00:00:00Z&spr=https&sig=abc123, what permissions does it grant?Solution
Step 1: Decode the SAS token parameters
sp=rlmeans permissions are Read (r) and List (l).Step 2: Understand service and resource types
ss=bmeans the service is Blob storage.Final Answer:
Read and List permissions on blob service -> Option AQuick Check:
sp=rl means Read+List [OK]
- Confusing 'rl' with read-write
- Assuming full control from SAS token
- Ignoring service type 'ss' parameter
--permissions rwd but users report they cannot delete blobs. What is the likely cause?Solution
Step 1: Understand permissions for delete
Delete permission is represented by 'd', so 'rwd' includes delete.Step 2: Check resource type relevance
If the SAS token is generated for a container but used on blobs, or vice versa, permissions may not apply correctly.Final Answer:
The SAS token was generated for the wrong resource type. -> Option DQuick Check:
Permissions + resource type must match [OK]
- Assuming 'x' is delete permission
- Ignoring resource type mismatch
- Not checking token expiry
Solution
Step 1: Avoid sharing storage account keys
Sharing keys gives full control and is insecure for third parties.Step 2: Use SAS token with limited scope and expiry
Generating a SAS token scoped to the container with write permission and a 2-hour expiry limits access and duration.Step 3: Evaluate other options
Creating a new storage account is unnecessary overhead; SAS token with full permissions and no expiry is insecure.Final Answer:
Generate a SAS token scoped to the container with write permission and 2-hour expiry. -> Option AQuick Check:
Limited scope + expiry = SAS token best practice [OK]
- Sharing full access keys with third parties
- Using SAS tokens without expiry
- Creating unnecessary storage accounts
Ready to prove it?
Pass the test and this pattern moves unknown
