mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +00:00
## Problem The public buckets section says that anyone with a file's URL can download it and that other operations still use access control. It doesn't say which policies a public bucket needs, so readers can't tell that a broad `SELECT` policy lets clients list the whole bucket, or that writes stay blocked until a policy allows them. Related to [FDBKIN-5481](https://linear.app/supabase/issue/FDBKIN-5481/document-best-practices-for-securing-public-storage-buckets) Closes [DOCS-1852](https://linear.app/supabase/issue/DOCS-1852/explain-how-to-lock-down-a-public-storage-bucket) ## Solution A new "Control access to a public bucket" subsection on the Storage buckets page covers several points. Reads by public URL need no `SELECT` policy, and a bucket-wide `SELECT` policy opens listing. Listing, overwrites, moves, copies, and deletes each need a `SELECT` policy, so the subsection says how to scope it to the user's objects and, with the operation helpers, to the operations that need it. Writes stay blocked until a scoped write policy allows them. It links the existing policy examples, upload restrictions, and helper functions instead of repeating them, and names the Security Advisor lint and the Dashboard warning that flag a listable public bucket. The Related resources line for access control now covers both bucket types. Three related fixes support the new subsection. The public buckets intro now says Storage skips access controls when it serves a file from a public bucket, and names listing among the operations that still need policies. The access control page has a sentence-case title and descriptive link text instead of "here". "Restricting uploads" on the Creating buckets page shows how to change limits on an existing bucket with `updateBucket`, notes that the JavaScript SDK requires `public`, and names the `fileSizeLimit` option correctly. Both of those pages also lose old wording. The Creating buckets title is in sentence case, and its intro and upload-rejection sentences no longer use "Here we", "above", or the future tense. Its Restricting uploads example sentence is shorter and writes "1 MB" with a space. The access control intro and Policy examples drop "perfectly", "incredibly powerful", and "An easy way", and the upsert sentence drops "you will need to additionally grant". The new title also appears in the docs nav entry for the page and in the link text on the Storage upload limits page. The Dashboard steps for creating a bucket, on the Creating buckets page and the Storage quickstart, now use Studio's current labels: **New bucket** and **Create**. ## How to verify 1. Open each changed page in the preview: - https://docs-git-tedd-docs-1852-supabase.vercel.app/docs/guides/storage/buckets/fundamentals#public-buckets - https://docs-git-tedd-docs-1852-supabase.vercel.app/docs/guides/storage/security/access-control#access-policies - https://docs-git-tedd-docs-1852-supabase.vercel.app/docs/guides/storage/buckets/creating-buckets#restricting-uploads - https://docs-git-tedd-docs-1852-supabase.vercel.app/docs/guides/storage/uploads/file-limits - https://docs-git-tedd-docs-1852-supabase.vercel.app/docs/guides/storage/quickstart 2. The "Control access to a public bucket" subsection says public URL reads need no `SELECT` policy on `storage.objects`. 3. It says a bucket-wide `SELECT` policy lets clients list every object, and that listing, overwrites, moves, copies, and deletes need a `SELECT` policy scoped to the user's objects and the operations. 4. It says uploads and other writes stay blocked until a policy allows them, and links Access policies and Policy examples. 5. It links Restricting uploads for file size and MIME type limits. 6. It says the Security Advisor flags a public bucket that allows listing. 7. The public buckets intro says downloads from a public bucket skip access controls, and that uploading, listing, deleting, moving, and copying still need policies. 8. The access control page title is "Storage access control", and its "Access policies" section has no "here" link text. The page has no "perfectly", "incredibly powerful", "An easy way", or "you will need to additionally grant". 9. The Creating buckets page title is "Creating buckets". Its intro has no "Here we", the Restricting uploads example sentence has no "You can achieve the following" and writes "1 MB", and the sentence after the example has no "above" or "will be rejected". 10. "Restricting uploads" has an `updateBucket` snippet with `public: true` and a sentence that the JavaScript SDK requires `public`. 11. The Storage nav shows "Creating buckets" for that page, and the Storage upload limits page links it as "Creating buckets". 12. The Dashboard tabs on the Creating buckets page and the Storage quickstart say **New bucket** and **Create**. 13. Each changed link opens the right page and section, including the `0025_public_bucket_allows_listing` tab on the Advisors page. ## Checks Ran `pnpm exec oxfmt --check` on the changed files, and `eslint` on `NavigationMenu.constants.ts`. The `updateBucket` snippet typechecks with its hidden setup (`tsc --ignoreConfig --moduleResolution bundler --module esnext --skipLibCheck`). MDX has no ESLint config in apps/docs. Behavior was checked against `supabase/storage` and against a local stack running storage-api v1.98.7. Public downloads skip RLS, both through the public URL and through `download()`, because `getObject.ts` reads public-bucket objects as superuser. Listing and writes go through RLS. Delete, move, copy, and upsert fail without a `SELECT` policy and work with a scoped one. `updateBucket` leaves omitted fields unchanged. The Advisor claim was checked against splinter lint 0025, and the Dashboard warning against Studio's `PublicBucketWarning`. The Dashboard labels come from Studio's `NewBucketButton` and `CreateBucketModal`. Skipped `pnpm build:guides-markdown`, which needs GitHub credentials for federated content. ## Follow-ups - The JavaScript reference for `update()` says it needs `update` and `select` on `storage.objects`. A local run on storage-api v1.98.7 showed that it also needs `insert`. That text comes from supabase-js, so this PR can't fix it. ## Checklist - [x] I have read the [Contributing Guidelines](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [x] I have checked that the change follows the docs style guide - [ ] I have checked that the change works in the preview deployment ## Feedback for Tedd To ask Tedd for a change to this PR, reply in Tedd's session on [DOCS-1852](https://linear.app/supabase/issue/DOCS-1852/explain-how-to-lock-down-a-public-storage-bucket). Tedd doesn't read comments on this PR. --------- Co-authored-by: supabase-vercel-tedd[bot] <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com> Co-authored-by: alexisintech <98043211+alexisintech@users.noreply.github.com>
318 lines
7.8 KiB
Plaintext
318 lines
7.8 KiB
Plaintext
---
|
|
id: 'storage-quickstart'
|
|
title: 'Storage Quickstart'
|
|
description: 'Learn how to use Supabase to store and serve files.'
|
|
subtitle: 'Learn how to use Supabase to store and serve files.'
|
|
sidebar_label: 'Quickstart'
|
|
tocVideo: ch2UGHMreB0
|
|
---
|
|
|
|
This guide shows the basic functionality of Supabase Storage. Find a full [example application on GitHub](https://github.com/supabase/supabase/tree/master/examples/user-management/nextjs-user-management).
|
|
|
|
## Concepts
|
|
|
|
Supabase Storage consists of Files, Folders, and Buckets.
|
|
|
|
### Files
|
|
|
|
Files can be any sort of media file. This includes images, GIFs, and videos. It is best practice to store files outside of your database because of their sizes. For security, HTML files are returned as plain text.
|
|
|
|
### Folders
|
|
|
|
Folders are a way to organize your files (like on your computer). There is no right or wrong way to organize your files. You can store them in whichever folder structure suits your project.
|
|
|
|
### Buckets
|
|
|
|
Buckets are distinct containers for files and folders. You can think of them like "super folders". Generally you would create distinct buckets for different Security and Access Rules. For example, you might keep all video files in a "video" bucket, and profile pictures in an "avatar" bucket.
|
|
|
|
<Admonition type="note">
|
|
|
|
File, Folder, and Bucket names **must follow** [AWS object key naming guidelines](https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html) and avoid use of any other characters.
|
|
|
|
</Admonition>
|
|
|
|
## Create a bucket
|
|
|
|
You can create a bucket using the Supabase Dashboard. Since the storage is interoperable with your Postgres database, you can also use SQL or our client libraries. Here we create a bucket called "avatars":
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="dashboard"
|
|
queryGroup="language"
|
|
>
|
|
<TabPanel id="dashboard" label="Dashboard">
|
|
|
|
1. Go to the [Storage](/dashboard/project/_/storage/buckets) page in the Dashboard.
|
|
2. Click **New bucket** and enter a name for the bucket.
|
|
3. Click **Create**.
|
|
|
|
</TabPanel>
|
|
<TabPanel id="sql" label="SQL">
|
|
|
|
```sql
|
|
-- Use Postgres to create a bucket.
|
|
|
|
insert into storage.buckets
|
|
(id, name)
|
|
values
|
|
('avatars', 'avatars');
|
|
```
|
|
|
|
</TabPanel>
|
|
<TabPanel id="javascript" label="JavaScript">
|
|
|
|
```js
|
|
// Use the JS library to create a bucket.
|
|
|
|
const { data, error } = await supabase.storage.createBucket('avatars')
|
|
```
|
|
|
|
[Reference.](/docs/reference/javascript/file-buckets-createbucket)
|
|
|
|
</TabPanel>
|
|
<$Show if="sdk:dart">
|
|
<TabPanel id="dart" label="Dart">
|
|
|
|
```dart
|
|
void main() async {
|
|
final supabase = SupabaseClient('supabaseUrl', 'supabaseKey');
|
|
|
|
final storageResponse = await supabase
|
|
.storage
|
|
.createBucket('avatars');
|
|
}
|
|
```
|
|
|
|
[Reference.](https://pub.dev/documentation/storage_client/latest/storage_client/SupabaseStorageClient/createBucket.html)
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:swift">
|
|
<TabPanel id="swift" label="Swift">
|
|
|
|
```swift
|
|
try await supabase.storage.createBucket("avatars")
|
|
```
|
|
|
|
[Reference.](/docs/reference/swift/storage-createbucket)
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:python">
|
|
<TabPanel id="python" label="Python">
|
|
|
|
```python
|
|
response = supabase.storage.create_bucket('avatars')
|
|
```
|
|
|
|
[Reference.](/docs/reference/python/storage-createbucket)
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:csharp">
|
|
<TabPanel id="csharp" label="C#">
|
|
|
|
```c#
|
|
var bucket = await supabase.Storage.CreateBucket("avatars");
|
|
```
|
|
|
|
[Reference.](/docs/reference/csharp/storage-createbucket)
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
</Tabs>
|
|
|
|
## Upload a file
|
|
|
|
You can upload a file from the Dashboard, or within a browser using our JS libraries.
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="dashboard"
|
|
queryGroup="language"
|
|
>
|
|
<TabPanel id="dashboard" label="Dashboard">
|
|
|
|
1. Go to the [Storage](/dashboard/project/_/storage/buckets) page in the Dashboard.
|
|
2. Select the bucket you want to upload the file to.
|
|
3. Click **Upload File**.
|
|
4. Select the file you want to upload.
|
|
|
|
</TabPanel>
|
|
<TabPanel id="javascript" label="JavaScript">
|
|
|
|
```js
|
|
const avatarFile = event.target.files[0]
|
|
const { data, error } = await supabase.storage
|
|
.from('avatars')
|
|
.upload('public/avatar1.png', avatarFile)
|
|
```
|
|
|
|
[Reference.](/docs/reference/javascript/file-buckets-upload)
|
|
|
|
</TabPanel>
|
|
<$Show if="sdk:dart">
|
|
<TabPanel id="dart" label="Dart">
|
|
|
|
```dart
|
|
void main() async {
|
|
final supabase = SupabaseClient('supabaseUrl', 'supabaseKey');
|
|
|
|
// Create file `example.txt` and upload it in `public` bucket
|
|
final file = File('example.txt');
|
|
file.writeAsStringSync('File content');
|
|
final storageResponse = await supabase
|
|
.storage
|
|
.from('public')
|
|
.upload('example.txt', file);
|
|
}
|
|
```
|
|
|
|
[Reference.](https://pub.dev/documentation/storage_client/latest/storage_client/SupabaseStorageClient/createBucket.html)
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:csharp">
|
|
<TabPanel id="csharp" label="C#">
|
|
|
|
```c#
|
|
var imagePath = Path.Combine("Assets", "avatar1.png");
|
|
|
|
await supabase.Storage
|
|
.From("avatars")
|
|
.Upload(imagePath, "public/avatar1.png");
|
|
```
|
|
|
|
[Reference.](/docs/reference/csharp/storage-from-upload)
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
</Tabs>
|
|
|
|
## Download a file
|
|
|
|
You can download a file from the Dashboard, or within a browser using our JS libraries.
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="dashboard"
|
|
queryGroup="language"
|
|
>
|
|
<TabPanel id="dashboard" label="Dashboard">
|
|
|
|
1. Go to the [Storage](/dashboard/project/_/storage/buckets) page in the Dashboard.
|
|
2. Select the bucket that contains the file.
|
|
3. Select the file that you want to download.
|
|
4. Click **Download**.
|
|
|
|
</TabPanel>
|
|
<TabPanel id="javascript" label="JavaScript">
|
|
|
|
```js
|
|
// Use the JS library to download a file.
|
|
|
|
const { data, error } = await supabase.storage.from('avatars').download('public/avatar1.png')
|
|
```
|
|
|
|
[Reference.](/docs/reference/javascript/file-buckets-download)
|
|
|
|
</TabPanel>
|
|
<$Show if="sdk:dart">
|
|
<TabPanel id="dart" label="Dart">
|
|
|
|
```dart
|
|
void main() async {
|
|
final supabase = SupabaseClient('supabaseUrl', 'supabaseKey');
|
|
|
|
final storageResponse = await supabase
|
|
.storage
|
|
.from('public')
|
|
.download('example.txt');
|
|
}
|
|
```
|
|
|
|
[Reference.](/docs/reference/dart/file-buckets-download)
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:swift">
|
|
<TabPanel id="swift" label="Swift">
|
|
|
|
```swift
|
|
let response = try await supabase.storage.from("avatars").download(path: "public/avatar1.png")
|
|
```
|
|
|
|
[Reference.](/docs/reference/swift/storage-from-download)
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:python">
|
|
<TabPanel id="python" label="Python">
|
|
|
|
```python
|
|
response = supabase.storage.from_('avatars').download('public/avatar1.png')
|
|
```
|
|
|
|
[Reference.](/docs/reference/python/storage-from-download)
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
<$Show if="sdk:csharp">
|
|
<TabPanel id="csharp" label="C#">
|
|
|
|
```c#
|
|
var bytes = await supabase.Storage.From("avatars").Download("public/avatar1.png");
|
|
```
|
|
|
|
[Reference.](/docs/reference/csharp/storage-from-download)
|
|
|
|
</TabPanel>
|
|
</$Show>
|
|
</Tabs>
|
|
|
|
## Add security rules
|
|
|
|
To restrict access to your files you can use either the Dashboard or SQL.
|
|
|
|
<Tabs
|
|
scrollable
|
|
size="small"
|
|
type="underlined"
|
|
defaultActiveId="dashboard"
|
|
queryGroup="database-method"
|
|
>
|
|
<TabPanel id="dashboard" label="Dashboard">
|
|
|
|
1. Go to the [Storage](/dashboard/project/_/storage/buckets) page in the Dashboard.
|
|
2. Click **Policies** in the sidebar.
|
|
3. Click **Add Policies** in the `OBJECTS` table to add policies for Files. You can also create policies for Buckets.
|
|
4. Choose whether you want the policy to apply to downloads (SELECT), uploads (INSERT), updates (UPDATE), or deletes (DELETE).
|
|
5. Give your policy a unique name.
|
|
6. Write the policy using SQL.
|
|
|
|
</TabPanel>
|
|
<TabPanel id="sql" label="SQL">
|
|
|
|
```sql
|
|
-- Use SQL to create a policy.
|
|
|
|
create policy "Public Access"
|
|
on storage.objects for select
|
|
using ( bucket_id = 'public' );
|
|
```
|
|
|
|
</TabPanel>
|
|
</Tabs>
|
|
|
|
---
|
|
|
|
{/* Finish with a video. This also appears in the Sidebar via the "tocVideo" metadata */}
|
|
|
|
<YouTube id="J9mTPY8rIXE" title="Adding security rules to Supabase Storage" />
|