Storage buckets help you save and organize files like photos or documents in the cloud. They keep your data safe and easy to find.
Creating storage buckets in Supabase
Introduction
Syntax
Supabase
const { data, error } = await supabase.storage.createBucket('bucket-name', { public: false })Use a unique bucket name with no spaces.
The public option controls if files are open to everyone or private.
Examples
Supabase
const { data, error } = await supabase.storage.createBucket('photos', { public: true })Supabase
const { data, error } = await supabase.storage.createBucket('backups', { public: false })Sample Program
This code connects to Supabase and creates a private storage bucket called 'user-files'. It prints success or error messages.
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 createBucket() { const { data, error } = await supabase.storage.createBucket('user-files', { public: false }) if (error) { console.log('Error creating bucket:', error.message) } else { console.log('Bucket created:', data) } } createBucket()
Important Notes
Bucket names must be lowercase and unique within your project.
Private buckets keep files secure but require authentication to access.
Public buckets allow easy sharing but anyone can view the files.
Summary
Storage buckets organize and protect your files in the cloud.
Use createBucket with a name and privacy setting to make a bucket.
Choose public or private based on who should access your files.
Practice
1. What is the main purpose of creating a storage bucket in Supabase?
easy
Solution
Step 1: Understand storage bucket purpose
A storage bucket is used to hold and organize files securely in the cloud.Step 2: Compare options
Options B, C, and D describe other services not related to file storage.Final Answer:
To organize and protect files in the cloud -> Option DQuick Check:
Storage bucket = organize and protect files [OK]
Hint: Buckets store files safely in the cloud [OK]
Common Mistakes:
- Confusing buckets with databases
- Thinking buckets run code
- Assuming buckets send emails
2. Which is the correct way to create a private storage bucket named
photos in Supabase?easy
Solution
Step 1: Recall correct method signature
The methodcreateBuckettakes the bucket name and an options object with apublicboolean.Step 2: Identify correct privacy option
Settingpublic: falsemakes the bucket private. Other options use incorrect keys.Final Answer:
supabase.storage.createBucket('photos', { public: false }) -> Option BQuick Check:
Use public: false for private bucket [OK]
Hint: Use public: false to make bucket private [OK]
Common Mistakes:
- Using wrong option keys like isPublic or private
- Setting public: true for private buckets
- Confusing access property with public
3. What will be the result of this code snippet?
const { data, error } = await supabase.storage.createBucket('docs', { public: true });
console.log(data);medium
Solution
Step 1: Understand createBucket return value
The method returns an object with bucket info if successful, including name and public status.Step 2: Analyze the code output
Sincepublic: trueis valid,datawill contain bucket details, not an error or boolean.Final Answer:
An object with bucket details including name 'docs' and public true -> Option CQuick Check:
createBucket returns bucket info object [OK]
Hint: createBucket returns bucket info object on success [OK]
Common Mistakes:
- Thinking it returns only true/false
- Assuming it throws error for public option
- Expecting no return value
4. You wrote this code but get an error:
What is the likely cause?
await supabase.storage.createBucket('files', { public: 'no' });What is the likely cause?
medium
Solution
Step 1: Check option types
Thepublicoption expects a boolean true or false, not a string.Step 2: Identify error cause
Passing 'no' as a string causes a type error. Bucket name and await usage are correct.Final Answer:
The 'public' option must be a boolean, not a string -> Option AQuick Check:
public option = boolean, not string [OK]
Hint: Use true/false for public, not strings [OK]
Common Mistakes:
- Passing 'yes' or 'no' as strings
- Ignoring await keyword
- Using invalid bucket names
5. You want to create two buckets: one public named
images and one private named backups. Which code correctly does this?hard
Solution
Step 1: Set correct privacy flags
Public bucket needspublic: true, private bucket needspublic: false.Step 2: Verify method calls
await supabase.storage.createBucket('images', { public: true }); await supabase.storage.createBucket('backups', { public: false }); correctly sets these flags. await supabase.storage.createBucket('images', { public: false }); await supabase.storage.createBucket('backups', { public: true }); reverses them. await supabase.storage.createBucket('images'); await supabase.storage.createBucket('backups'); omits privacy, defaulting to private. await supabase.storage.createBucket('images', { access: 'public' }); await supabase.storage.createBucket('backups', { access: 'private' }); uses invalid keys.Final Answer:
await supabase.storage.createBucket('images', { public: true }); await supabase.storage.createBucket('backups', { public: false }); -> Option AQuick Check:
Public = true, Private = false [OK]
Hint: Public bucket: public true; Private bucket: public false [OK]
Common Mistakes:
- Mixing up public true/false
- Omitting privacy option
- Using wrong option keys like access
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
