#Getting Url for a file in convex

16 messages · Page 1 of 1 (latest)

clear arrow
#

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}`
}

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)}/>
}

id that I use and I have this fileId in the database

inland talonBOT
#

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!

high glade
#

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

clear arrow
high glade
clear arrow
#

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
}
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);
  }),
});
high glade
#

yeah the problem is

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

clear arrow
#

It worked, thank you very much!

clear arrow
#

@high glade But what if now when you click on a link or send a request, it says “No matching routes found”

high glade
#

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

tame kiln
clear arrow