alexk64
alexk64•12mo ago

getting images by url

Hi please , am I the only who can't see images stored in the bucket with their id ?
26 Replies
lee
lee•12mo ago
how are you constructing the URL? storage URLs cannot be constructed directly from an ID. this used to be possible, but it was never intended to work and if there's any documentation describing it, please let us know so we can fix it. the supported way to get a url is ctx.storage.getUrl(id)
alexk64
alexk64OP•12mo ago
but the id is : kg29qaz64q9dyzcythm72w8t7s6nvqdf
lee
lee•12mo ago
correct
alexk64
alexk64OP•12mo ago
when I remove the last part of the url and put the id in the link I got an error.
lee
lee•12mo ago
yes that is not supported. is there any documentation that indicates it's supported? if that worked, it would potentially be a security hole, because it would make it impossible to add access rules to stored files
alexk64
alexk64OP•12mo ago
2 weeks ago it worked.
lee
lee•12mo ago
yes we patched the security hole 🙂
alexk64
alexk64OP•12mo ago
ok thank you I will use the supported way you showed to me.
lee
lee•12mo ago
btw here's the prior conversation. sorry about the unexpected change. https://discord.com/channels/1019350475847499849/1019350478817079338/1213139043227934810
alexk64
alexk64OP•12mo ago
exactly , he made a tuto , in which everything used to work. ctx.storage.getUrl(id), I'm supposed to pass the id of the image ?
lee
lee•12mo ago
ah that makes sense. Thanks for letting us know! Since there's a tutorial maybe we should announce this change somewhere
alexk64
alexk64OP•12mo ago
ctx.storage.getUrl(id) what is id ?
lee
lee•12mo ago
the storage id of the image. In your example, it's "kg29qaz64q9dyzcythm72w8t7s6nvqdf"
alexk64
alexk64OP•12mo ago
ok thank you. @Lee it's me again , please do you have a sample , an example because I'm a bit lost . I don't know how to get the id in the convex file and then passing it through the query and displaying it in my App.tsx file.
lee
lee•12mo ago
there's sample code at https://docs.convex.dev/file-storage/serve-files . If you have code that doesn't work, you could also post it with the error and see what's wrong
Serving Files | Convex Developer Hub
Files stored in Convex can be served to your users by generating a URL pointing
alexk64
alexk64OP•12mo ago
1h on that page but still nothing
lee
lee•12mo ago
how are you uploading the file? That should give you a storage id
alexk64
alexk64OP•12mo ago
it's over for me.🥲 uploading is fine , the doc is good . The problem come when I run ctx.db.query("DataFile").collect(); I got the data in an array how can I pull out the id and then pass it to ctx.storage.getUrl(id)
lee
lee•12mo ago
guessing at your schema, maybe this will help
const dataFiles = await ctx.db.query("DataFile").collect();
const dataFilesWithStorageUrls = await Promise.all(dataFiles.map(async (file) => {
const storageUrl = await ctx.storage.getUrl(file.storageId);
return {...file, storageUrl};
}));
const dataFiles = await ctx.db.query("DataFile").collect();
const dataFilesWithStorageUrls = await Promise.all(dataFiles.map(async (file) => {
const storageUrl = await ctx.storage.getUrl(file.storageId);
return {...file, storageUrl};
}));
alexk64
alexk64OP•12mo ago
I'm going to test and also thanks for the time you are spending to solve my problem.
presley
presley•12mo ago
In general, you should use the same storageId you used to manually generate the URL, to storage.getUrl(:storage_id). This will give you the proper URL to use. You should never manually construct the URL.
alexk64
alexk64OP•12mo ago
@Lee thank you so much I solved my issue.
evanletz
evanletz•12mo ago
Hey @Lee , I'm making the same change to my code right now. How can I tell TS that the ID is of type storageId? I keep running into the fact that passing a string is deprecated. Thanks in advance
No description
lee
lee•12mo ago
can you share your schema? i am imagining you have
trades: defineTable({imageId: v.id("_storage")})
trades: defineTable({imageId: v.id("_storage")})
which should work
evanletz
evanletz•12mo ago
Ah that's my bad. I had it defined as a string in the schema! Moving too quick - thanks a ton

Did you find this page helpful?