How it works
File keys are paths. Put the owner’s id first:<user id>/<file id>.pdf. Then one rule on the bucket, “the first part of the key is the caller’s id”, protects every action. A documents table records each upload, with its own owner rule, so you can list, search and title files like any other data.
1. Create the bucket
Storage → New bucket:2. Protect it with an owner-folder rule
Create a named rule in Policies → New policy,own_folder:
- All of these rules pass:
- Check a value:
$auth.user_idis set - Compare values:
$object.segments.0equals$auth.user_id
- Check a value:
own_folder for Who may read, Who may upload new objects, Who may list objects, Who may replace objects and Who may delete objects.
$object.segments.0 is the first part of the key, before the first /. A person whose id is u_123 can reach u_123/… and nothing else.
3. Create the table
Data → Tables → Create tabledocuments:
Access: every operation Record owner (owner_id), and Add records: Signed-in users. This is exactly the setup from the signed-in app guide.
4. Upload and record the file
src/documents.ts
- The key uses a random id, not the file’s name: names collide, leak information and can contain characters you don’t want in a path.
- Upload first, then create the record. If the record fails, the file is orphaned but harmless; a scheduled clean-up can remove files with no record (Scheduled jobs).
Large files: upload straight from the browser
For big files, ask for a signed upload link and send the file straight to storage, so it never passes through your own server:5. List, show and download
404.
6. Share a file with a temporary link
A signed link lets anyone who holds it read one file for a short time, with no sign-in:7. Delete
Delete the file and the record together:8. Test the boundary
test/files.test.ts
Variations
Troubleshooting
Storage
Everything buckets can do.
Rules on storage
More file rules.