nikhildhoka
nikhildhoka3w ago

How to retrieve files from the Storage in Convex

code:
export const getPackageById = query({
args: { packageId: v.id("packageTable") }, // Validate that packageId is an ID from "packageTable"
handler: async (ctx: any, args: any) => {
const identity = await ctx.auth.getUserIdentity();
if (!identity) {
throw new Error("Unauthorized");
}
const pkg = await ctx.db.get(args.packageId); // Fetch the package by ID
pkg.metadata.ID = pkg._id;
if (!pkg) {
throw new Error(`Package with ID ${args.packageId} not found.`);
}
const fileStorageId = (pkg.data.Content) ! as Id<"_storage">;
console.log('File Storage ID:', fileStorageId);
const blob = await ctx.storage.get(fileStorageId);
console.log("blob: ", blob);
if (!blob) {
return new Response("Blob not found", { status: 404 });
}
pkg.data.Content = await blobToBase64(blob);
return pkg;
},
});
export const getPackageById = query({
args: { packageId: v.id("packageTable") }, // Validate that packageId is an ID from "packageTable"
handler: async (ctx: any, args: any) => {
const identity = await ctx.auth.getUserIdentity();
if (!identity) {
throw new Error("Unauthorized");
}
const pkg = await ctx.db.get(args.packageId); // Fetch the package by ID
pkg.metadata.ID = pkg._id;
if (!pkg) {
throw new Error(`Package with ID ${args.packageId} not found.`);
}
const fileStorageId = (pkg.data.Content) ! as Id<"_storage">;
console.log('File Storage ID:', fileStorageId);
const blob = await ctx.storage.get(fileStorageId);
console.log("blob: ", blob);
if (!blob) {
return new Response("Blob not found", { status: 404 });
}
pkg.data.Content = await blobToBase64(blob);
return pkg;
},
});
error: Dec 07, 19:53:04 Q queries/packageTable:getPackageById failure 33ms Uncaught TypeError: ctx.storage.get is not a function at handler (../../convex/queries/packageTable.ts:37:31)
4 Replies
Convex Bot
Convex Bot3w 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
ballingt2w ago
See the docs, ctx.storage.get doesn't exist (EDIT: in queries and actions, thanks Lee), there's .getUrl() and .getMetadata() https://docs.convex.dev/api/interfaces/server.StorageWriter#methods
nikhildhoka
nikhildhokaOP2w ago
got it, thanks!
lee
lee2w ago
ctx.storage.get only exists in actions, not queries or mutations. The philosophical reason it doesn't exist in queries is that queries are for small, reactive data processing (they have strict limits on runtime and how much they can read), while stored files may be large. But if you want to write an action that does ctx.storage.get, that should work But as Tom suggested, the typical flow is to do ctx.storage.getUrl in a query and then use the url from the client

Did you find this page helpful?