Signed URLs for temporary access in GCP - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to create signed URLs changes as we create more of them.
Specifically, how does the work grow when we generate many temporary access links?
Analyze the time complexity of the following operation sequence.
from google.cloud import storage
client = storage.Client()
bucket = client.bucket('my-bucket')
for i in range(n):
blob = bucket.blob(f'file_{i}.txt')
url = blob.generate_signed_url(expiration=3600)
print(url)
This code generates a signed URL for each file in a bucket to allow temporary access.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Generating a signed URL for each blob (file).
- How many times: Once per file, so
ntimes.
Each signed URL requires a separate operation, so the total work grows directly with the number of files.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 signed URL generations |
| 100 | 100 signed URL generations |
| 1000 | 1000 signed URL generations |
Pattern observation: The work increases evenly as you add more files.
Time Complexity: O(n)
This means the time to generate signed URLs grows in direct proportion to the number of files.
[X] Wrong: "Generating one signed URL automatically creates URLs for all files at once."
[OK] Correct: Each signed URL is created individually, so you must do the work separately for each file.
Understanding how operations scale helps you design systems that handle many users or files efficiently.
"What if we cached signed URLs instead of generating them every time? How would the time complexity change?"
Practice
Solution
Step 1: Understand what signed URLs do
Signed URLs allow access to a file for a limited time without changing its public status.Step 2: Compare options
Only To provide temporary, secure access to a file without making it public describes temporary, secure access without making the file public.Final Answer:
To provide temporary, secure access to a file without making it public -> Option CQuick Check:
Signed URL = Temporary secure access [OK]
- Thinking signed URLs make files permanently public
- Confusing signed URLs with encryption
- Assuming signed URLs delete files
gcloud command correctly creates a signed URL valid for 1 hour to download a file named photo.jpg from bucket my-bucket?Solution
Step 1: Identify correct command syntax
The correct command uses 'gcloud storage signed-urls create' with '--duration' to specify time.Step 2: Check options for correct flags and command
gcloud storage signed-urls create gs://my-bucket/photo.jpg --duration=1h matches the correct syntax and flag '--duration=1h'. Others use wrong flags or commands.Final Answer:
gcloud storage signed-urls create gs://my-bucket/photo.jpg --duration=1h -> Option AQuick Check:
Correct command + --duration flag = gcloud storage signed-urls create gs://my-bucket/photo.jpg --duration=1h [OK]
- Using incorrect flags like --valid-for or --time
- Using 'generate' instead of 'create'
- Mixing command order or bucket syntax
Solution
Step 1: Understand signed URL expiration
Signed URLs only allow access during their valid time window.Step 2: Identify behavior after expiration
After expiration, access is denied and an error is returned.Final Answer:
The signed URL will return an error indicating access denied -> Option AQuick Check:
Expired signed URL = Access denied error [OK]
- Assuming URLs auto-renew after expiration
- Thinking files become public after expiration
- Believing files get deleted automatically
gcloud storage signed-urls create gs://my-bucket/file.txt --duration=30m, but users report they cannot access the file. What is the most likely cause?Solution
Step 1: Check bucket and file existence
If the file name is wrong or missing, access via signed URL fails.Step 2: Evaluate other options
Bucket name error would cause different error; duration too long is allowed; file type does not restrict signed URLs.Final Answer:
The filefile.txtis missing or misspelled in the bucket -> Option BQuick Check:
Missing file = Access failure [OK]
- Assuming duration too long blocks access
- Believing signed URLs depend on file type
- Ignoring typos in file or bucket names
Solution
Step 1: Understand HTTP methods for signed URLs
PUT allows uploading or replacing a file; GET allows downloading; DELETE removes the file.Step 2: Match requirement to method
To allow upload but not download, use a signed URL with PUT method and set duration to 2 hours.Final Answer:
Create a signed URL with PUT method and 2-hour duration for uploading -> Option DQuick Check:
Upload access = PUT method signed URL [OK]
- Using GET method which allows download
- Making file public exposes it permanently
- Using DELETE method deletes the file
