You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Supabase存储桶创建时的行级安全(Row-level security)问题求助

Fixing "new row violates row-level security policy for table 'objects'" in Supabase Storage

Hey there! I’ve run into this exact error plenty of times when working with Supabase Storage—usually it boils down to missing or misconfigured row-level security (RLS) policies that block your user from interacting with storage buckets and objects. Let’s fix this step by step:

1. Confirm RLS is enabled (and enable it if not)

First, make sure RLS is turned on for both storage tables. Run these commands in your Supabase SQL Editor:

ALTER TABLE storage.buckets ENABLE ROW LEVEL SECURITY;
ALTER TABLE storage.objects ENABLE ROW LEVEL SECURITY;

2. Create the required RLS policies

You need two core policies to let users manage their own buckets and the objects inside them:

Policy for storage.buckets (let users create/access their own buckets)

-- Allow users to create buckets they own
CREATE POLICY "Users can create their own buckets" ON storage.buckets
  FOR INSERT WITH CHECK (auth.uid() = owner);

-- Allow users to view buckets they own
CREATE POLICY "Users can view their own buckets" ON storage.buckets
  FOR SELECT USING (auth.uid() = owner);

Policy for storage.objects (let users manage objects in their buckets)

-- Allow users to upload, view, and delete objects in buckets they own
CREATE POLICY "Users can manage objects in their own buckets" ON storage.objects
  FOR ALL USING (
    auth.uid() = (SELECT owner FROM storage.buckets WHERE id = bucket_id)
  );

3. Tweak your bucket creation code (for better alignment)

When creating the bucket, explicitly set the owner to the current user’s ID. This ensures the bucket is properly linked to the user, which our policies rely on:

const { data, error } = await supabase
  .storage
  .createBucket(`bucket-${user.id}`, { 
    public: false,
    owner: user.id // Add this line to associate the bucket with the user
  });
console.log(data, error);

Why this works

  • The bucket policies ensure only the user who owns a bucket can create or view it.
  • The objects policy checks that the current user owns the bucket containing the object, granting them full access to manage those files.

After setting these up, try creating your bucket and uploading files again—this should resolve the RLS violation error.

内容的提问来源于stack exchange,提问作者Gustavo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 14:44:07