Skip to content
Storage

存储快速入门

Learn how to use Supabase to store and serve files.

本指南展示了 Supabase Storage 的基本功能。在 GitHub 上可以找到完整的 示例应用

🌐 This guide shows the basic functionality of Supabase Storage. Find a full example application on GitHub.

概念 #

🌐 Concepts

Supabase 存储由文件、文件夹和存储桶组成。

🌐 Supabase Storage consists of Files, Folders, and Buckets.

文件 #

🌐 Files

文件可以是任何类型的媒体文件。这包括图片、GIF 和视频。由于文件体积较大,最好将它们存储在数据库之外。出于安全原因,HTML 文件会以纯文本形式返回。

🌐 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

桶是用于存放文件和文件夹的独立容器。你可以把它们想象成“超级文件夹”。通常,你会为不同的安全和访问规则创建不同的桶。例如,你可能会把所有视频文件放在一个“video”桶里,而把头像图片放在一个“avatar”桶里。

🌐 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.

创建一个桶 #

🌐 Create a bucket

你可以使用 Supabase 控制面板来创建一个存储桶。由于存储与你的 Postgres 数据库互通,你也可以使用 SQL 或我们的客户端库。这里我们创建一个名为“avatars”的存储桶:

🌐 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":

  1. 去仪表板的存储页面。
  2. 点击 新建桶 并输入桶的名称。
  3. 点击 创建存储桶

上传文件 #

🌐 Upload a file

你可以从仪表板上传文件,或者在浏览器中使用我们的 JS 库上传。

🌐 You can upload a file from the Dashboard, or within a browser using our JS libraries.

  1. 去仪表板的存储页面。
  2. 选择你想上传文件的桶。
  3. 点击 上传文件
  4. 选择你想上传的文件。

下载文件 #

🌐 Download a file

你可以从仪表板下载文件,或者在浏览器中使用我们的 JS 库下载。

🌐 You can download a file from the Dashboard, or within a browser using our JS libraries.

  1. 去仪表板的存储页面。
  2. 选择包含该文件的桶。
  3. 选择你想下载的文件。
  4. 点击下载

添加安全规则 #

🌐 Add security rules

要限制对你文件的访问,你可以使用仪表板或 SQL。

🌐 To restrict access to your files you can use either the Dashboard or SQL.

  1. 去仪表板的存储页面。
  2. 在侧边栏中点击 政策
  3. OBJECTS 表中点击 添加策略 来为文件添加策略。你也可以为存储桶创建策略。
  4. 选择你想让策略应用于下载(SELECT)、上传(INSERT)、更新(UPDATE)还是删除(DELETE)。
  5. 给你的策略取个独特的名字。
  6. 用 SQL 写这个策略。