Why Supabase Storage handles file uploads - Performance Analysis
When uploading files to Supabase Storage, it is important to understand how the time taken grows as the number or size of files increases.
We want to know how the number of upload operations changes when we add more files.
Analyze the time complexity of the following operation sequence.
const uploadFiles = async (files) => {
for (const file of files) {
const { data, error } = await supabase.storage
.from('my-bucket')
.upload(file.name, file);
if (error) {
console.error('Upload failed:', error);
}
}
};
This code uploads each file one by one to a Supabase Storage bucket.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: The
uploadAPI call to send each file to storage. - How many times: Once for each file in the input list.
As the number of files increases, the number of upload calls increases at the same rate.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 upload calls |
| 100 | 100 upload calls |
| 1000 | 1000 upload calls |
Pattern observation: The number of upload operations grows directly in proportion to the number of files.
Time Complexity: O(n)
This means the time to upload grows linearly with the number of files.
[X] Wrong: "Uploading multiple files happens all at once, so time stays the same no matter how many files."
[OK] Correct: Each file upload is a separate operation that takes time, so more files mean more upload calls and longer total time.
Understanding how file uploads scale helps you design efficient cloud storage solutions and explain your reasoning clearly in interviews.
"What if we uploaded all files in parallel instead of one by one? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of buckets in Supabase Storage
Buckets are containers that help organize files logically and manage permissions.Step 2: Identify why organization and access control matter
Organizing files helps keep data tidy and access control ensures security for uploads.Final Answer:
To organize files logically and control access -> Option AQuick Check:
Buckets = Organize + Access Control [OK]
- Thinking buckets speed up uploads
- Believing buckets convert file types
- Assuming buckets delete files automatically
Solution
Step 1: Recall the Supabase Storage upload method syntax
The correct method is callingsupabase.storage.from(bucketName).upload(path, file).Step 2: Match the syntax with the options
Only supabase.storage.from('bucket').upload('path/file.txt', file) matches the correct method and parameter order.Final Answer:
supabase.storage.from('bucket').upload('path/file.txt', file) -> Option CQuick Check:
Correct upload syntax = supabase.storage.from('bucket').upload('path/file.txt', file) [OK]
- Using incorrect method names like uploadFile
- Passing parameters in wrong order
- Skipping the .from('bucket') call
const { data, error } = await supabase.storage.from('photos').upload('image.png', file);
console.log(error ? error.message : 'Upload successful');Solution
Step 1: Understand the upload result structure
The upload returns an object withdataanderror. If upload fails,errorcontains details.Step 2: Analyze the console.log statement
Iferrorexists, it printserror.message, which explains the permission issue.Final Answer:
An error message explaining permission denied -> Option BQuick Check:
Error present = print error message [OK]
- Assuming success message always prints
- Expecting undefined when error occurs
- Confusing error object with data
const { data, error } = await supabase.storage.upload('docs', 'file.pdf', file);Solution
Step 1: Check the correct method chaining for upload
The upload method must be called onsupabase.storage.from('bucket'), not directly onsupabase.storage.Step 2: Identify the missing part in the code
The code misses the.from('docs')call before.upload.Final Answer:
Missing .from('docs') before .upload -> Option DQuick Check:
Use .from('bucket') before upload [OK]
- Calling upload directly on storage
- Assuming upload returns no data
- Ignoring file variable initialization
Solution
Step 1: Consider bucket management best practices
Creating a bucket per user is inefficient and can complicate access control.Step 2: Use folder paths inside a single bucket
Uploading files to paths like 'userID/filename' inside one bucket organizes files logically and keeps access control manageable.Step 3: Avoid root uploads or relying only on metadata
Uploading all files to root or relying on metadata makes file management and security harder.Final Answer:
Use one bucket and upload files to paths like 'userID/filename' -> Option AQuick Check:
Organize by folder path inside bucket [OK]
- Creating too many buckets
- Uploading all files to root folder
- Ignoring folder structure for security
Ready to prove it?
Pass the test and this pattern moves unknown
