Supabase Storage manages file uploads so you can easily save and access files like images or documents in your app without building your own storage system.
You want to let users upload profile pictures to your app.
You need to store documents or reports that users can download later.
You want to save images for a blog or gallery in your website.
You need a simple way to handle file uploads without managing servers.
You want secure and scalable storage integrated with your database.
This code connects to Supabase, then uploads a file to the 'avatars' bucket under 'user123/profile.png'. It logs the file path on success or an error message on failure.
Supabase
import { createClient } from'@supabase/supabase-js';
const supabaseUrl = 'https://xyzcompany.supabase.co';
const supabaseKey = 'public-anonymous-key';
const supabase = createClient(supabaseUrl, supabaseKey);
async function uploadFile(file) {
const { data, error } = await supabase.storage.from('avatars').upload('user123/profile.png', file);
if (error) {
console.error('Upload failed:', error.message);
return;
}
console.log('File uploaded at:', data.path);
}
// Assume 'file'is a File object from an input element
// uploadFile(file);
OutputSuccess
Important Notes
Supabase Storage automatically handles file security and scaling.
Always check for errors after uploading to handle issues gracefully.
Files are stored in buckets, which are like folders to organize your files.
Summary
Supabase Storage makes file uploads easy and secure.
Use buckets to organize your files logically.
Check upload results to handle errors properly.
Practice
(1/5)
1. Why does Supabase Storage use buckets when handling file uploads?
easy
A. To organize files logically and control access
B. To speed up file uploads by splitting files
C. To automatically convert files to images
D. To delete files after upload automatically
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 A
Quick Check:
Buckets = Organize + Access Control [OK]
Hint: Buckets group files and manage who can see them [OK]
Common Mistakes:
Thinking buckets speed up uploads
Believing buckets convert file types
Assuming buckets delete files automatically
2. Which of the following is the correct way to upload a file to Supabase Storage using JavaScript?
easy
A. supabase.upload('bucket', 'file.txt', file)
B. storage.uploadFile('bucket', file)
C. supabase.storage.from('bucket').upload('path/file.txt', file)
D. supabase.storage.upload('bucket', 'file.txt', file)
Solution
Step 1: Recall the Supabase Storage upload method syntax
The correct method is calling supabase.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 C
Step 1: Check the correct method chaining for upload
The upload method must be called on supabase.storage.from('bucket'), not directly on supabase.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 D
Quick Check:
Use .from('bucket') before upload [OK]
Hint: Always call .from('bucket') before .upload [OK]
Common Mistakes:
Calling upload directly on storage
Assuming upload returns no data
Ignoring file variable initialization
5. You want to upload multiple files securely to Supabase Storage and ensure each file is stored in a separate folder by user ID. Which approach best achieves this?
hard
A. Use one bucket and upload files to paths like 'userID/filename'
B. Create a bucket per user and upload files directly
C. Upload all files to root bucket folder and rename later
D. Upload files without folders and rely on metadata
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 A
Quick Check:
Organize by folder path inside bucket [OK]
Hint: Use folder paths with userID inside one bucket [OK]
Common Mistakes:
Creating too many buckets
Uploading all files to root folder
Ignoring folder structure for security
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.