Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/lib/StorageFileApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,29 @@ export class StorageFileApi {
}
}

/**
* Retrieve URLs for assets in public buckets
*
* @param path The file path to be downloaded, including the path and file name. For example `folder/image.png`.
*/
getPublicUrl(
path: string
): {
data: { publicURL: string } | null
error: Error | null
publicURL: string | null
} {
try {
const _path = this._getFinalPath(path)
let publicURL = `/object/public/${_path}`
const data = { publicURL }
publicURL = `${this.url}${publicURL}`
return { data, error: null, publicURL }
} catch (error) {
return { data: null, error, publicURL: null }
}
}

/**
* Deletes files within the same bucket
*
Expand Down
9 changes: 9 additions & 0 deletions test/__snapshots__/storageFileApi.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`get public URL 1`] = `
Object {
"publicURL": "/object/public/my-new-public-bucket/profiles/myUniqueUserId/profile.png",
}
`;

exports[`get public URL 2`] = `"http://localhost:8000/storage/v1/object/public/my-new-public-bucket/profiles/myUniqueUserId/profile.png"`;
16 changes: 16 additions & 0 deletions test/storageFileApi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { SupabaseStorageClient } from '../src/index'

// TODO: need to setup storage-api server for this test
const URL = 'http://localhost:8000/storage/v1'
const KEY =
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzdXBhYmFzZSIsImlhdCI6MTYwMzk2ODgzNCwiZXhwIjoyNTUwNjUzNjM0LCJhdWQiOiIiLCJzdWIiOiIzMTdlYWRjZS02MzFhLTQ0MjktYTBiYi1mMTlhN2E1MTdiNGEiLCJSb2xlIjoicG9zdGdyZXMifQ.pZobPtp6gDcX0UbzMmG3FHSlg4m4Q-22tKtGWalOrNo'

const storage = new SupabaseStorageClient(URL, { Authorization: `Bearer ${KEY}` })
const newBucketName = 'my-new-public-bucket'

test('get public URL', async () => {
const res = storage.from(newBucketName).getPublicUrl('profiles/myUniqueUserId/profile.png')
expect(res.error).toBeNull()
expect(res.data).toMatchSnapshot()
expect(res.publicURL).toMatchSnapshot()
})