I add the R2 component to my Convex and it creates a table in a different schema as I think called R2 that contains file metadata that are uploaded. How can I get the file ID or the record ID from this table and reference it in my schema table? Keep in mind that I don't have a reference in the schema table that I have in my code.
#How to Query data from a component table R2
6 messages · Page 1 of 1 (latest)
Hi @neon forum. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
You’re right that the R2 component creates its own tables (for metadata) that live outside your app’s DataModel. You can still reference those records from your own tables; you just don’t declare them as v.id("...") fields in your schema.
From the docs, the R2 component:
- Stores metadata “in your Convex database” when you upload. [Uploading files]
- Lets you access that metadata via helpers like
r2.getMetadata,r2.listMetadata, andr2.pageMetadata. [Accessing metadata]
However, the knowledge sources don’t show the exact shape of the R2 metadata table or how to query it directly with ctx.db. They only show accessing metadata through the R2 helper methods, which return objects like:
{
"ContentType": "image/jpeg",
"ContentLength": 125338,
"LastModified": "2024-03-20T12:34:56Z"
}
``` [[Accessing metadata](<https://www.convex.dev/components/cloudflare-r2#accessing-file-metadata>)]
Because of that, I can’t reliably tell you:
- The internal table name the component uses
- Whether those rows have Convex `_id` values you can query with `ctx.db`
- How to type those IDs as `Id<"...">`
What you *can* safely do, based on the docs:
1. **Use the R2 object key as your “ID” in your own tables.**
When you upload via `useUploadFile`, it returns the object key:
```ts
const key = await uploadFile(file);
You can store that key in your own table as a plain string field (e.g. imageKey: v.string()), and later:
- Generate URLs with
r2.getUrl(key)[Serving files]- Fetch metadata with
r2.getMetadata(ctx, key)[Accessing metadata]
- Fetch metadata with
-
React to uploads using callbacks instead of direct table queries.
The component exposes callbacks like
onUploadandonSyncMetadatawhere you get thekeyand can write to your own tables:export const { generateUploadUrl, syncMetadata } = r2.clientApi<DataModel>({ checkUpload: async (ctx, bucket) => { // auth / validation }, onUpload: async (ctx, bucket, key) => { // Here you can insert or update your own document // e.g. await ctx.db.insert("files", { r2Key: key, ... }); }, }); ``` [[Uploading files](<https://www.convex.dev/components/cloudflare-r2#uploading-files>)] Or with `onSyncMetadata`: ```ts export const { generateUploadUrl, syncMetadata, onSyncMetadata } = r2.clientApi<DataModel>({ callbacks, onSyncMetadata: async (ctx, args) => { // args: { bucket: string; key: string; isNew: boolean } const metadata = await r2.getMetadata(ctx, args.key); // use args.key to link into your own tables }, }); ``` [[Accessing metadata](<https://www.convex.dev/components/cloudflare-r2#accessing-file-metadata>)]
In other words:
- Treat the R2 object key as the stable identifier you store and reference in your own schema.
- Use the R2 helpers (
getUrl,getMetadata,listMetadata,pageMetadata) to work with the component’s metadata instead of querying its internal table directly.
The knowledge sources don’t document a supported way to query the R2 component’s internal table via ctx.db or to use its internal record IDs as Id<"..."> in your own schema, so I can’t recommend that approach based on the docs.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@neon forum Kapa.ai is still learning and improving, please let me know how I did by reacting below.