Generating public URLs in Supabase - Time & Space Complexity
When we generate public URLs for files stored in Supabase, we want to know how the time it takes changes as we ask for more URLs.
We ask: How does the number of URL requests affect the total work done?
Analyze the time complexity of the following operation sequence.
const { data, error } = await supabase
.storage
.from('avatars')
.list('public', { limit: 100 })
const urls = data.map(file =>
supabase.storage.from('avatars').getPublicUrl(file.name).publicUrl
)
This code lists up to 100 files in the 'public' folder and then generates a public URL for each file.
Identify the repeating operations.
- Primary operation: Calling
getPublicUrlfor each file to get its URL. - How many times: Once per file in the list (up to 100 times in this example).
Each file requires one call to get its public URL, so the total work grows as we have more files.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to getPublicUrl |
| 100 | 100 calls to getPublicUrl |
| 1000 | 1000 calls to getPublicUrl |
Pattern observation: The number of calls grows directly with the number of files requested.
Time Complexity: O(n)
This means the time to generate all public URLs grows in a straight line as the number of files increases.
[X] Wrong: "Generating all public URLs happens in constant time regardless of how many files there are."
[OK] Correct: Each file needs its own URL generated, so more files mean more work and more calls.
Understanding how work grows with input size helps you explain and design efficient cloud operations, a key skill in real projects.
"What if we cached the URLs after the first call? How would that change the time complexity when requesting URLs again?"
Practice
getPublicUrl function in Supabase do?Solution
Step 1: Understand the purpose of
This function is designed to generate a URL that can be shared publicly to access a file stored in Supabase storage.getPublicUrlStep 2: Compare options with function behavior
Only It creates a link that anyone can use to access a file in a bucket. describes creating a public link. Other options describe unrelated actions like deleting, uploading, or encrypting files.Final Answer:
It creates a link that anyone can use to access a file in a bucket. -> Option AQuick Check:
Public URL = Link to file [OK]
- Confusing URL generation with file upload
- Thinking it deletes files
- Assuming it encrypts files automatically
getPublicUrl in Supabase to get a public URL for a file named photo.jpg in bucket images?Solution
Step 1: Recall the correct method chaining
The correct syntax is to select the bucket first withfrom('bucket_name'), then callgetPublicUrl('file_path').Step 2: Match options with correct syntax
supabase.storage.from('images').getPublicUrl('photo.jpg') matches this pattern exactly. Options A, B, and D misuse method calls or argument order.Final Answer:
supabase.storage.from('images').getPublicUrl('photo.jpg') -> Option CQuick Check:
Bucket then file path = Correct call [OK]
- Calling getPublicUrl directly on storage
- Swapping bucket and file arguments
- Using wrong method names
const { data } = supabase.storage.from('docs').getPublicUrl('file.txt');
console.log(data.publicUrl);Solution
Step 1: Understand getPublicUrl return value
ThegetPublicUrlmethod returns an object with apublicUrlstring property immediately, not a promise.Step 2: Analyze the console.log output
Loggingdata.publicUrlwill print the URL string to access the file publicly.Final Answer:
A string URL to access 'file.txt' publicly in 'docs' bucket. -> Option BQuick Check:
getPublicUrl returns URL string [OK]
- Assuming getPublicUrl is async
- Expecting undefined data
- Confusing output with file existence check
supabase.storage.from('media').getPublicUrl('video.mp4') but got an empty string. What is the most likely cause?Solution
Step 1: Check file existence in bucket
If the file does not exist, the public URL generated will be empty or invalid.Step 2: Evaluate other options
Importing client or bucket name mismatch would cause errors, not empty string. getPublicUrl is synchronous, so async is not needed.Final Answer:
The file 'video.mp4' does not exist in the 'media' bucket. -> Option DQuick Check:
Missing file = empty URL [OK]
- Assuming getPublicUrl is async
- Ignoring file existence
- Mixing up bucket names
getPublicUrl is best?Solution
Step 1: Understand bucket privacy settings
Supabase storage buckets are either public or private. Public buckets allow all files to be accessed via public URLs.Step 2: Evaluate file-level public access
Supabase does not support per-file public access in private buckets. To share files publicly, the bucket must be public.Step 3: Choose best practice
UsegetPublicUrlonly on files in a public bucket. correctly states to usegetPublicUrlon files in a public bucket to share them. Options A and D are incorrect because file-level public control is not supported. Make the entire bucket public so all files are accessible. makes all files public, which is not desired.Final Answer:
Use getPublicUrl only on files in a public bucket. -> Option AQuick Check:
Public URLs need public bucket [OK]
- Thinking private buckets allow public URLs per file
- Making whole bucket public without control
- Assuming tags control public access
Ready to prove it?
Pass the test and this pattern moves unknown
