Frank
Frank2mo ago

[storage] Is it possible to reference file url instead of storage id in a table?

Hi, I've just started trying out Convex file storage. The documentation uses a chat app example (https://docs.convex.dev/file-storage/serve-files#generating-file-url-in-queries): 1. If the message is an image, it's message.body is a storage id 2. when retrieving that message, the convex function uses await ctx.storage.getUrl(message.body) to convert storage id to file url I observed that 1. the storage id is something like kg25kw... and the file url is something like https://<project-id>.convex.cloud/api/storage/ce47fad6-xxxx 2. storage id to file url seems to be a one-to-one mapping The might be a dumb question but: - I wonder when creating the message, is it possible to directly store the file url, instead of the storage id? - In that case I assume when retrieving that message, we can save a network call ctx.storage.getUrl() Appreciate any thoughts on this 🙏
Serving Files | Convex Developer Hub
Files stored in Convex can be served to your users by generating a URL pointing
3 Replies
Convex Bot
Convex Bot2mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
ballingt
ballingt2mo ago
Don't assume that the storage id always maps like that to the url, but sure, once you get a url from ctx.storage.getUrl() you can store that returned URL instead of storing the storage id. These URLs don't expire today, in the future there might be options for URLs that do expire after a bit. re
save a network call ctx.storage.getUrl()
you don't need to save network calls like this, this code runs in the Convex backend an API call like getUrl is very fast. They're barely "network" calls since the run in the backend. If this was running on your own server and this was a network call to Convex or some other database, that would make more sense — you could save as much as 100ms — but this runs in Convex, this call is going to take <1ms.
Frank
FrankOP2mo ago
Ohh I see: - the "url with expiration" use case make sense - I didn't measure and thought ctx.storage.getUrl() was like a normal network call (~100ms), but if it's actually super fast within Convex backend like (~1ms), then there's no need to save that call at all. I'll keep it then. Thanks for the explanation!

Did you find this page helpful?