Skip to content

Supabase Storage: Inefficient folder operations and hierarchical RLS challenges

Supabase Storage 缺乏原生的文件夹概念或批量文件夹操作的 API,这可能导致文件夹操作(移动、重命名、删除)效率低下,并且在为对象实现分层访问控制时会遇到困难。

🌐 Supabase Storage lacks native folder concepts or APIs for batch folder operations, which can lead to inefficient folder operations (move, rename, delete) and difficulties in implementing hierarchical access controls for objects.

这为什么会发生? #

🌐 Why does this happen?

存储桶把“文件夹”纯粹当作键前缀。这意味着像文件系统一样的文件夹行为和继承权限并不是 Supabase Storage 的内置功能。

🌐 Storage buckets treat "folders" purely as key prefixes. This means file system-like folder behavior and inherited permissions are not built-in features of Supabase Storage.

如何应对这些挑战 #

🌐 How to address these challenges

为了克服这些限制,并实现具有层级 RLS 的强大文件夹管理,可以考虑以下方法:

🌐 To overcome these limitations and implement robust folder management with hierarchical RLS, consider the following approach:

  • 在自定义的 Postgres 表中建模你的文件夹层级。 这个表应该管理文件夹的元数据,比如文件夹 ID、父文件夹 ID、路径和权限。
  • 在你的自定义元数据中引用 storage.objects 在你的自定义表中存储对 storage.objects.id 的引用,以将文件链接到各自的文件夹。
  • storage.objects 上实现 RLS 策略。 这些策略必须 JOIN 你的自定义元数据表,以根据你定义的文件夹结构来执行分层访问权限。
  • 通过你的元数据表处理批量文件夹操作。 对于移动或重命名文件夹等操作,请更新自定义元数据表中的相关条目。请注意,这些操作不会直接更改存储中的实际文件路径。
  • 优化 RLS 策略以提升性能。 RLS 策略中的 JOIN 可能导致性能下降,尤其是在处理大数据集时。确保对自定义元数据表进行适当索引,并考虑使用 SECURITY DEFINER 函数来优化策略执行。

另一种方法:使用 S3 协议进行批量操作 #

🌐 Alternative approach: Using the S3 protocol for bulk operations

Supabase Storage 也支持兼容 S3 的 API。这让你可以使用像 AWS CLI 这样的工具更高效地进行批量文件操作,比如下载、移动或重新整理对象。

🌐 Supabase Storage also supports an S3-compatible API. This allows you to use tools like the AWS CLI to perform bulk file operations such as downloading, moving, or reorganizing objects more efficiently.

按照 AWS CLI 安装指南 安装 AWS CLI。

🌐 Install the AWS CLI by following the AWS CLI installation guide.

使用 Supabase S3 认证指南 在 Supabase 中创建 S3 凭证。

🌐 Create S3 credentials in Supabase using the Supabase S3 authentication guide.

使用你在 Supabase 中生成的凭证配置一个 AWS CLI 配置文件。配置文件的名字可以随意,但必须与以下命令中使用的值一致。

🌐 Configure an AWS CLI profile using the credentials you generated in Supabase. The profile name can be anything, but it must match the value used in the following commands.

1
aws configure --profile supabase-s3

从存储桶或前缀下载文件:

🌐 Download files from a bucket or prefix:

1
aws s3 cp s3://bucket-name/folder-name ./download-target
2
--profile supabase-s3
3
--endpoint-url https://<project-ref>.supabase.co/storage/v1/s3
4
--recursive
5
--region <region>
  • bucket-name 替换成你的存储桶名称。
  • folder-name 换成你想下载的前缀,或者省略它来下载整个桶。
  • <project-ref> 替换成你的 Supabase 项目引用。
  • <region> 替换成你项目的区域(例如 eu-central-1)。
  • ./download-target 是保存文件的本地目录。

使用 mv 命令移动或重命名文件。因为 Supabase 存储中的文件夹是以前缀实现的,所以重命名文件夹实际上就是将对象从一个前缀移动到另一个前缀。

🌐 Move or rename files using the mv command. Because folders in Supabase Storage are implemented as prefixes, renaming a folder is effectively moving objects from one prefix to another.

1
aws s3 mv s3://bucket-name-one/folder-name-one s3://bucket-name-two/folder-name-two
2
--profile supabase-s3
3
--endpoint-url https://<project-ref>.supabase.co/storage/v1/s3
4
--recursive
5
--region <region>

这个方法对大规模下载、迁移或在存储桶内重新整理文件很有用。

🌐 This method is useful for large-scale downloads, migrations, or reorganizing files within a bucket.