it's me again is it possible to rename a file in a...
# sql
n
it's me again is it possible to rename a file in a storage bucket through sql? I upload a file through the JS API to "{user_id}/upload.png", I can't assign it a name yet because the ID gets generated when i insert a new row in the database, which happens in the function executed by the trigger. I found out that the path is saved in
path_tokens
, so my approach was to modify it in the function.
Copy code
sql
create or replace function
  public.create_post_on_bucket_upload()
  returns trigger as
  $$
  declare
    post_id text;
  begin
    insert into public.posts (user_id)
    values (
      auth.uid()
    )
    returning id into post_id;
    new.path_tokens[2] = post_id;
    return new;
  end;
  $$ language plpgsql security definer;
it creates the function and I can run the upload process on my website, but the file name/location doesn't change. What did I do wrong?
s
No you can't rename a file in a storage bucket with SQL. The data that is inserted into the database table happens after the file have been saved to Amazon s3.
n
alright thanks, do you know any other way to accomplish this? I just want to dynamically rename the file based on the id in the "posts" database row
i could try to hit the move endpoint with an http request somehow, i'll try that
s
Yeah you can run http requests from inside of the trigger function