add-cache-headers.sh
Adds cache control headers to Azure Blob Storage files that don’t have them set.Description
This script scans all blobs in the specified container and adds the cache control headerCache-Control: public, max-age=3600, must-revalidate to files that don’t have it or have a different value. It skips files that already have the correct header set.
Prerequisites
- Azure CLI installed and configured
jqfor JSON processing- Authenticated Azure session (
az login) - Write access to Azure Storage Account
Configuration
Edit these variables in the script:Usage
Basic Syntax
Parameters
PARALLEL (optional, default:10)
- Number of parallel operations to run simultaneously
Examples
Default Usage (10 Parallel Operations)
High Parallelism
Conservative Approach
Output
Features
- Smart Detection: Only updates blobs that need it
- Parallel Processing: Updates multiple blobs simultaneously
- Progress Tracking: Real-time status with symbols:
- ✓ Updated successfully
- ⊘ Skipped (already has correct header)
- ✗ Failed to update
- Summary Report: Shows counts at the end
Cache Control Header
The script sets:public- Can be cached by browsers and CDNsmax-age=3600- Cache for 1 hour (3600 seconds)must-revalidate- Must check with server after expiration
Customizing Cache Duration
Edit the script to change cache duration:Performance
Recommended Parallelism
| Blob Count | Recommended Parallel |
|---|---|
| < 100 | 5-10 |
| 100-500 | 10-15 |
| 500-1000 | 15-20 |
| > 1000 | 20-30 |
Execution Time
Approximate times (depends on network):- 100 blobs: ~30 seconds (10 parallel)
- 500 blobs: ~2 minutes (15 parallel)
- 1000 blobs: ~3-4 minutes (20 parallel)
Error Handling
Failed to Fetch Storage Key
az login and verify access
Failed Updates
Individual blob update failures are tracked and reported in the summary. Common causes:- Blob is locked
- Insufficient permissions
- Blob doesn’t exist (deleted during operation)
Missing jq
Verification
Check a Specific Blob
List All Blobs Without Cache Headers
Use Cases
Initial Setup
Run once to add cache headers to all existing blobs:After Bulk Upload
After uploading new files without cache headers:Periodic Maintenance
Run periodically to ensure all blobs have proper caching:Best Practices
- Test First: Run on a test container before production
- Monitor Progress: Watch for failed updates
- Verify Results: Check a few blobs manually after completion
- Schedule Wisely: Run during low-traffic periods
- Backup First: Consider backing up blob metadata before bulk updates
Troubleshooting
All Updates Failing
- Verify write permissions on storage account
- Check if container has immutability policy
- Ensure blobs aren’t locked
Slow Performance
- Reduce parallel count
- Check network latency to Azure region
- Run from Azure VM in same region for best speed
Inconsistent Results
- Some blobs may be updated by other processes
- Re-run the script to catch any missed blobs
Related Scripts
- list-media-files.sh - View all blobs and their properties