supabase/storage-js

Improve `getPublicUrl` DX

thorwebdev opened this issue · 3 comments

Feature request

Is your feature request related to a problem? Please describe.

  • We should return the public URL when uploading a file
  • We should make getPublicUrl return publicUrl instead of publicURL although for backwards compatibility we might need to return both.

cc @dijonmusters fyi

This is not quite the same issue as I reported in #40, as this is noting the difference in the API prop names. My request has to do with the implementation of the function directly and its behavior.

Specifically, it's confusing that the output of upload to a public bucket cannot be used as input for the getPublicUrl function, and also generally confusing what the output of upload is supposed to be at all and what the input of getPublicUrl should be.

--

My report:

I have code like this:

const { data, error } = await supabase.storage
  .from('public-bucket')
  .upload(uuid(), file)
if (error) {
  onError(error)
  return
}
const res = supabase.storage.from('public-bucket').getPublicUrl(data?.Key)
if (res.error) {
  onError(res.error)
  return
}
onSuccess(res.publicURL)

The value of data is an object that looks like this:

{ Key: 'public-bucket/dv_iPhZFUFIeGk0a' }

Then, when I call getPublicUrl with the value of data.Key, I get a URL that looks like this: [REDACTED_SUPABASE_URL]/storage/v1/object/public/public-bucket/public-bucket/dv_iPhZFUFIeGk0a

Uploading works fine, but the getPublicUrl implementation seems to be doubly prefixing the bucket name.

Currently working around it by just creating the public URL myself via string concatenation.

const SUPABASE_STORAGE_BUCKET_URL = 
`${process.env.NEXT_PUBLIC_SUPABASE_URL}/storage/v1/object/public/\`
...
onSuccess(`${SUPABASE_STORAGE_BUCKET_URL}${data.Key}`)
inian commented

@thorwebdev

We should return the public URL when uploading a file

We use the same upload method for both private and public buckets. It is probably not a good idea to return a signed url when uploading to a private bucket since the client might not want that. We could only return the url only for public buckets but that makes the types messy since the url is only returned in some cases.

We should make getPublicUrl return publicUrl

In the next version, we are return the url directly without putting it in an object (see linked PR).

@kaspnilsson upload returns only the path in next version of supabase-js

fenos commented

This should now be fixed with V2