Hi there, I need help on the following issue: I'm ...
# help
f
Hi there, I need help on the following issue: I'm using Clerk with Supabase and I'd like my users to upload images. I want to create a bucket for each user and I'm trying to achieve the following thing:
Copy code
js
const { data, error } = await supabase.storage.createBucket(`${userId}-bucket`)
Where
userId
comes from
useAuth()
hook from Clerk. Although I'm getting the following error on POST:
Copy code
json
{
error: ""
message: "invalid input syntax for type uuid: \"user_28YHvhguBwY5edeBlB6WrD0pKNE\""
statusCode: "22P02"
}
The payload looks like this:
Copy code
json
id: "user_28YHvhguBwY5edeBlB6WrD0pKNE-bucket"
name: "user_28YHvhguBwY5edeBlB6WrD0pKNE-bucket"
public: false
Is it a RLS error?
n
Hello @Franck! This thread has been automatically created from your message in #843999948717555735 a few seconds ago. We have already mentioned the @User so that they can see your message and help you as soon as possible! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ``...`` menu) and select "Leave Thread" to unsubscribe from future updates. Want to change the title? Use the ``/title`` command! We have solved your problem? Click the button below to archive it.
g
Without digging thru your specific stuff, it would be much easier to create a "directory" per user all in the same bucket. Then everything is dealt with at the object table level.
n
Franck (2022-05-07)
f
That's want I wanted to do in the first place but I couldn't find out how to create directories from the api 😓
g
directories are folders but everything under the bucket is just a path and you can control easily the first level of the path. Just add uuid of user as the first part of your path when you upload/download a file. So you write your url with something like: const fileurl = userid + '/' + filenameWithExtension
f
I did this:
Copy code
js
const { data, error } = await supabase
      .storage
      .from('users')      .upload(`${userId}/${file.path}`, file, {
        cacheControl: '3600',
        upsert: false
      })
But still getting the same error... I think it's because of this https://github.com/supabase/supabase/discussions/4954 I'm not super confident writing RLS Policies, how could I turn the template provided above to something similar to this example?
Copy code
sql
CREATE POLICY "Authenticated users can update their own todos" 
    ON public.todos FOR UPDATE USING (
        auth.role() = 'authenticated'::text
    ) WITH CHECK (
        requesting_user_id() = user_id
    );
g
I'm out for the night, but see my image above for storage policy (in the templates in the UI) for a uuid folder in the path.
f
I'll try to figure it out, if I fail I'll ask for help later, thanks for your help and enjoy your night 🍻
I tried something like this:
Copy code
sql
bucket_id = 'users' AND requesting_user_id() = (storage.foldername(name))[1]
where requesting_user_id() does:
Copy code
sql
create or replace function requesting_user_id()
returns text 
language sql stable
as $$
  select nullif(current_setting('request.jwt.claims', true)::json->>'sub', '')::text;
$$;
But still the same error...
What does
storage.foldername(name)[1]
is supposed to return?
g
https://supabase.com/docs/guides/storage#helpers that should return the first folder name in your files path. Postgres arrays start at 1.
f
Is there a way to know at what level is the error
Copy code
error: ""
message: "invalid input syntax for type uuid: \"user_28oxcIJAoJ8A07LMAaXIoZmdHO2\""
statusCode: "22P02"
triggered?
@garyaustin would it be possible to have some help regarding this issue please? For storage or db even if I follow the Clerk+Supabase integration tutorial I keep getting the same error regarding the uuid type, is there a workaround like changing uuid types to simple text?
g
Looks like you are setting a variable that is a uuid with a string starting with user_ and possibly then having the uuid concatenated to it.
f
Sorry I made an error in my previous comment, it's working fine with the db, only storage fails. In the Clerk+Supabase integration tutorial, it says that the ID is not of type uuid, hence why they provide this requesting_user_id() for RLS, it's working fine with the db but on storage there must be somewhere where an uuid is requested, (S3 bucket level maybe?) In the fail post request I can see this search param containing the owner value. On schema.storage, the owner value is of type uuid, could that be the source of error? I'm not trying to concatenate anything, this is the user ID provided by Clerk
"search": "?columns=%22name%22%2C%22owner%22%2C%22bucket_id%22",