Chubbyman
Chubbyman

Reputation: 103

Supabase not allowing upload of files into storage bucket

I have a simple script for uploading a .txt file into a Supabase bucket named 'training-data'. The bucket was made public, and I made sure to include the necessary storage/object policies in order to allow access. I know others have encountered the following error, as have I, but I managed to fix it using the policies:

'new row violates row-level security policy for table "objects"'

My problem is now, it's giving me a different error that, as far as I could see, no one else encountered before:

storage3.utils.StorageException: {'statusCode': 400, 'error': 'Key is not present in table "buckets".', 'message': 'insert or update on table "objects" violates foreign key constraint "objects_bucketId_fkey"'}

I'm not sure why it's telling me that my bucket is a table and that it needs a unique identifier key. How do I fix this? My code is below:

from supabase import create_client
from dotenv import load_dotenv
import os

load_dotenv()
url = os.environ.get("DEPLOY_URL")
key = os.environ.get("DEPLOY_KEY")


supabase = create_client(url, key)
data = supabase.table("Blog Data").select("*").execute()

file = "dzonescrape\\dzonescrape\\spiders\\test.txt"
# data = supabase.storage().from_("public/training-data").download("test.txt")
# print(data)

supabase.storage().from_("public/training-data").upload("test.txt", file)

FYI, I tested downloading files that were manually uploaded, and it works, so it's just the upload that's giving me issues. Here is my insert object policy:

CREATE POLICY "Make inserting training data publicly available (INSERT)" ON "storage"."objects"
AS PERMISSIVE FOR INSERT
TO public

WITH CHECK (bucket_id = 'training-data')

Upvotes: 4

Views: 6081

Answers (2)

ASA Serge
ASA Serge

Reputation: 29

I came across this problem and solved it in this way.

Go to Storage > Policies > Other policies under storage.objects > New Policy

Then upload the policy from the screenshot, you will be able to upload.enter image description here

Upvotes: 0

igdmitrov
igdmitrov

Reputation: 546

Try to change your bucket name:

supabase.storage().from_("training-data").upload("test.txt", file)

I checked my code on Dart:

  Future<StorageResponse> _uploadFile() async {
    final supabase = Supabase.instance.client;

    const file = '/Users/igdmitrov/DEV/Test.txt';
    final response = await supabase.storage
        .from('training-data')
        .upload('text.txt', File(file));

    print(response.error);

    return response;
  }

It works with policy:

CREATE POLICY "Give users authenticated access to folder 1ov7tml_0" ON storage.objects FOR INSERT TO public WITH CHECK (bucket_id = 'training-data');

CREATE POLICY "Give users authenticated access to folder 1ov7tml_1" ON storage.objects FOR SELECT TO public USING (bucket_id = 'training-data');

Upvotes: 1

Related Questions