Skip to content

Updated

Creating buckets

You can create a bucket using the Supabase Dashboard. Since storage is interoperable with your Postgres database, you can also use SQL or our client libraries. The following code examples create a bucket called avatars:

import { createClient } from '@supabase/supabase-js'
const supabase = createClient(process.env.SUPABASE_URL!, process.env.SUPABASE_KEY!)
// ---cut---
// Use the JS library to create a bucket.
const { data, error } = await supabase.storage.createBucket('avatars', {
public: true, // default: false
})

Reference.

  1. Go to the Storage page in the Dashboard.
  2. Click New bucket and enter a name for the bucket.
  3. Click Create.
-- Use Postgres to create a bucket.
insert into storage.buckets
(id, name, public)
values
('avatars', 'avatars', true);
void main() async {
final supabase = SupabaseClient('supabaseUrl', 'supabaseKey');
final storageResponse = await supabase
.storage
.createBucket('avatars');
}

Reference.

try await supabase.storage.createBucket(
"avatars",
options: BucketOptions(public: true)
)

Reference.

supabase.storage.create_bucket(
'avatars',
options={"public": True}
)

Reference.

var bucket = await supabase.Storage.CreateBucket("avatars", new BucketUpsertOptions { Public = true });

Reference.

Restricting uploads

When creating a bucket you can add additional configurations to restrict the type or size of files you want this bucket to contain.

For example, to allow only images of up to 1 MB in the avatars bucket, set allowedMimeTypes and fileSizeLimit:

import { createClient } from '@supabase/supabase-js'
const supabase = createClient(process.env.SUPABASE_URL!, process.env.SUPABASE_KEY!)
// ---cut---
// Use the JS library to create a bucket.
const { data, error } = await supabase.storage.createBucket('avatars', {
public: true,
allowedMimeTypes: ['image/*'],
fileSizeLimit: '1MB',
})

Storage rejects an upload that doesn't meet the bucket's restrictions. For more information, see Storage upload limits.

To change the restrictions of an existing bucket, call updateBucket. The JavaScript SDK requires public, so pass the bucket's current value to keep its visibility. getBucket returns that value.

await ..('avatars', {
: true,
: ['image/*'],
: '1MB',
})