Can I getPublicUrl from sql? `select publicURL fro...
# help
j
Can I getPublicUrl from sql?
select publicURL from storage.objects where id = '...'
something like this maybe. But i dont see a url in that so maybe something else
n
Hello @jar! 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.
j
Because was returning json from rpc func and figured including public url in that was better than additional call on front end but lmk if reason not to do this
g
GetPublicURL does not do an API call. It just build up the URL from your instance URL and some constants and the the pathname.
j
Ok I still may move the file to diff folders or may have external urls so a url seams most universal. Should i just assemble the string in sql? How would i get my instance url in sql or should i hardcode the string from what i see
So i have storage object id that i think can get all neccisarry parts from except instance id
@garyaustin like this perhaps is how I should do it?
Copy code
if not myitem.image_storage_object_id is null then
      select * from storage.objects where id = myitem.image_storage_object_id into storage_object;
      image_src := 'https://myrandomprojstring.supabase.co/storage/v1/object/public/' || storage_object.bucket_id || '/' || storage_object.name;
    end if;
g
In general that is the right idea, but I'm not actually checking your syntax. I would seriously consider NOT doing this as now you have to maintain the URL when you run it in different instances, like dev versus production or on an upgrade to a newer instance. It is a low overhead function on the client as it does not make a database call.
Although.... you might be able to use: return current_setting('request.headers', true)::json; And use the http header data to get it....
j
@garyaustin ok yeah idk all the works with the "instance" but ya I may in fact follow that and just use getPublicUrl. Idk what this current_setting is but looks intriging may. mess around with it
j
@garyaustin well this does work it seems of course sending bucket and name and doing on client may be the slightest bit more to docs but do you think this is good? Also I'm trying to have as ready to go as possible for ssr and all that for best seo so figured the image src already ready may be best but im not expert on seo
Copy code
create or replace function get_public_url_from_storage_object_id(storage_object_id uuid) returns text as $$
  declare
    storage_object storage.objects%rowtype;
    image_src text;
    cur_setting jsonb;
    instance_id text;
  begin
    if not storage_object_id is null then
      select * from storage.objects where id = storage_object_id into storage_object;
      cur_setting := current_setting('request.headers', true)::jsonb;
      instance_id := cur_setting->>'x-forwarded-host'::text;
      image_src :=  'https://' || instance_id || '/storage/v1/object/public/' || storage_object.bucket_id || '/' || storage_object.name;
    end if;
    return image_src;
  end;
$$ language plpgsql