Getting Url for a file in convex

When I try to send a request, it writes an error 404 Not found, I tried to find the answer in the documentation and the problem is that the file id that we have does not work to receive the link
function getFileUrl(fileId: Id<"_storage">): string{
return `${process.env.NEXT_PUBLIC_CONVEX_URL}/getImage?storageId=${fileId}`
}
function getFileUrl(fileId: Id<"_storage">): string{
return `${process.env.NEXT_PUBLIC_CONVEX_URL}/getImage?storageId=${fileId}`
}
here is the code in which I call the function:
{
file.type === "image" && <Image alt={file.name} width="200" height="100" src={getFileUrl(file.fileId)}/>
}
{
file.type === "image" && <Image alt={file.name} width="200" height="100" src={getFileUrl(file.fileId)}/>
}
id that I use and I have this fileId in the database
No description
No description
14 Replies
Convex Bot
Convex Bot11mo 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!
lee
lee11mo ago
have you defined an HTTP action /getImage? if so you want to use the HTTP action url, which ends in .convex.site, not the url that ends in .convex.cloud
[FURRY] Ебанные Блинчики・Кеняка
Is it possible to define 2 actions at once in http.ts? Now I have definitions there for authorization via clerk
lee
lee11mo ago
yes you can call http.route multiple times https://docs.convex.dev/functions/http-actions
HTTP Actions | Convex Developer Hub
HTTP actions allow you to build an HTTP API right in Convex!
[FURRY] Ебанные Блинчики・Кеняка
I tried it, still 404
function getFileUrl(fileId: Id<"_storage">): string{
const getImageUrl = new URL(`${process.env.NEXT_PUBLIC_CONVEX_URL}/getImage`);
getImageUrl.searchParams.set("storageId", fileId);
return getImageUrl.href
}
function getFileUrl(fileId: Id<"_storage">): string{
const getImageUrl = new URL(`${process.env.NEXT_PUBLIC_CONVEX_URL}/getImage`);
getImageUrl.searchParams.set("storageId", fileId);
return getImageUrl.href
}
http.route({
path: "/getImage",
method: "GET",
handler: httpAction(async (ctx, request) => {
const { searchParams } = new URL(request.url);
const storageId = searchParams.get("storageId")! as Id<"_storage">;
const blob = await ctx.storage.get(storageId);
if (blob === null) {
return new Response("Image not found", {
status: 404,
});
}
return new Response(blob);
}),
});
http.route({
path: "/getImage",
method: "GET",
handler: httpAction(async (ctx, request) => {
const { searchParams } = new URL(request.url);
const storageId = searchParams.get("storageId")! as Id<"_storage">;
const blob = await ctx.storage.get(storageId);
if (blob === null) {
return new Response("Image not found", {
status: 404,
});
}
return new Response(blob);
}),
});
No description
lee
lee11mo ago
yeah the problem is
const getImageUrl = new URL(`${process.env.NEXT_PUBLIC_CONVEX_URL}/getImage`);
const getImageUrl = new URL(`${process.env.NEXT_PUBLIC_CONVEX_URL}/getImage`);
and the urls that have .convex.cloud in them, when they should have .convex.site
lee
lee11mo ago
https://docs.convex.dev/functions/http-actions
HTTP actions are exposed at https://<your deployment name>.convex.site (e.g. https://happy-animal-123.convex.site).
HTTP Actions | Convex Developer Hub
HTTP actions allow you to build an HTTP API right in Convex!
lee
lee11mo ago
Convex Dashboard
Manage your Convex apps
[FURRY] Ебанные Блинчики・Кеняка
It worked, thank you very much!
[FURRY] Ебанные Блинчики・Кеняка
@lee But what if now when you click on a link or send a request, it says “No matching routes found”
No description
lee
lee11mo ago
hmm i'm not sure what's wrong. can you add a console.log in the http action and check convex dashbord logs to see if it's getting called? also make sure the file is convex/http.ts and it does export default http; at the bottom
sshader
sshader11mo ago
HTTP Actions | Convex Developer Hub
HTTP actions allow you to build an HTTP API right in Convex!
[FURRY] Ебанные Блинчики・Кеняка
I accidentally deleted a piece of code from http.route /getImage 😅 thanks again

Did you find this page helpful?