Hi, in my storage I have categories/sport/footba...
# help
d
Hi, in my storage I have categories/sport/football and inside football i have 2 images, and in sport folder i have a also a subfolder, golf with 1 image. But could I somehow grab that sport folder and get back all the images from each subfolder from sport folder?
Copy code
const { data, error } = await supabaseAdmin.storage
    .from("gallery")
    .list("categories/sport", {
      limit: 100,
      offset: 0,
    });
Wiht this i just get back the folder, but not the images or subfolder. Is this possible?
n
Hello @Dembe! 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
You may want to consider doing your own search function...
Copy code
create function files_in_folder(bucket text, folders text[]) returns text[]
language plpgsql
as
$$
declare filepaths text[];
begin
filepaths := ARRAY(
    SELECT name
    FROM storage.objects
    WHERE bucket_id = bucket AND path_tokens @> folders
);

return filepaths;
end;
$$;
Called like: supabase.rpc('files_in_folder',['folderroot','foldersub1']) The array is one or more folder names and it will return all files with all folder names in the path including sub folders. The @> is array contains array operation. Of course this depends on SB storage structure details which is a minor risk for changing in the future.
d
@garyaustin thanks, yes the structure is like this. So if i call for sport i want to get all the images inside their subfolders.
g
That function should work for that. I think the only issue is if you were to have two sub folders of the same name in two different parent folders. Then you have to use ['parent1','child1'].
d
alright, after i took your code into sql editor and ran that. In my next js app i called for const { data, error } = await supabaseAdmin.rpc('files_in_folder',['category','sport']) But getting null back, seems like i missing sometihing
g
Sorry, I left bucket out in my call I showed and format for parameters. so it is .rpc('files_in_folder',{bucket:'test',folders:['folder1','folder2']})
d
thanks a lot, now i get an array with all the images from the subfolders! 🙂
Do you know also if it's possible in supabase to like upload images into a table, where i have a row called category and childcategory, and store images in the folder structure i have now and like bind them to the row category and childcategory somehow?
g
I don't follow what you are asking.
d
If i upload a image to storage in a sub folder example, can I connect that to a table so the img is connected to a table to a row with more information about that row? In table I have a column called category can I connect those images I load up to storage to a folder to that column?
g
My only application for images is a note taking app and I just store the pathname in a column after storing it in storage with a separate call. You can technically have a trigger function on storage.objects and do stuff BUT upload involves an insert to objects, then storing to s3 and then either an update to objects of metadata or a delete if the store to s3 failed.
d
Alright thanks. But I think I have an idea what I could do. With .from.upload could I upload to my football folder which are in category/sport/football in that way?
g
I'm worried I'm not following what you are trying to do. One thing to remember is that there are really no folders in storage. They are all just part of the name of the file made to look like a path with folders. Anything special done with them (like in policies) is just string processing.