Storage
Store files in a Fabric app with Rayfin Storage — declare @blob folders, register storageService on the client, and upload, download, list, and delete objects by path.
Use Rayfin Storage when a Fabric app needs to upload, download, list, and delete files through a typed client, with access controlled by the same @role() policies as your data. Storage is generally available from Rayfin 2.0 and needs no feature flag. Changed in 2.0
Storage is remote-only. Folders and their files live in the deployed Fabric backend; there is no local Storage service or emulator. With the default Fabric provider, rayfin dev applies your folder declarations to the Fabric backend it uses, so a frontend running on localhost reads and writes the same remote folders.
If your code was written for the experimental Storage in 1.36, see Migrate from the 1.36 experimental Storage.
Add Storage to a project
Run Storage initialization from the root of an npm project: New in 2.0
npx rayfin storage initThe command changes local files only — it does not sign in, create a backend, or deploy folders. It:
- Enables
services.storageand the Data service that Storage requires. An existing Data dialect is kept; a missing one is set tomssql. - Adds
@microsoft/rayfin-storageto the package that declares@microsoft/rayfin-client, and@microsoft/rayfin-coreto the package that holds the declarations, at the version your other Rayfin dependencies use. - Creates an owner-scoped starter folder,
Attachment, registers it in the runtime schema, and writes a browser-safe type map for the client. - Adds a
build:storagescript and runs it beforebuild,build:fabric, andtypecheck. - Runs
npm installand compiles the declarations to check them.
In a Universal App project, it also registers Storage on the typed client in packages/frontend/src/lib/rayfin-client.ts. In other layouts it ends with Manual integration required: lines; make that change yourself as shown in Add Storage to the client.
Preview the plan without writing anything:
npx rayfin storage init --dry-runrayfin storage init lists every flag, explains how the command picks packages in npm workspaces, and describes how it rolls back on failure. For a new project, npx rayfin init --services auth,data,storage enables Storage when the project is created. New in 2.0
Set up Storage manually
Use manual setup when rayfin storage init cannot run — for example, in a project managed by another package manager. Install the client package at the same exact version as your other Rayfin packages, in the package that creates your Rayfin client:
npm install @microsoft/rayfin-storage@2.0.0Enable Data and Storage:
services:
data:
enabled: true
dialect: mssql
storage:
enabled: trueStorage requires Data because folder configuration and object metadata live in the deployed database. If Storage is enabled without Data, Storage apply fails with Storage requires the Data service. Storage can stay enabled before you declare a folder; until one is registered, apply is skipped with a warning.
Declare a folder
Declare each folder as a class under rayfin/storage/ that extends StorageObject, with @blob() and at least one authenticated @role() rule. Import all of them from @microsoft/rayfin-core. Changed in 2.0
import {
blob,
ContentTypes,
mb,
role,
StorageObject,
} from '@microsoft/rayfin-core';
@blob({
name: 'profile-images',
maxSize: mb(5),
allowedContentTypes: [ContentTypes.AnyImage],
})
@role('authenticated', '*', {
policy: (claims, item) => claims.sub.eq(item.owner_id),
})
export class ProfileImage extends StorageObject {}StorageObject is declaration-only. It exposes server-managed fields such as owner_id, path, size, and etag to policies without adding metadata fields. owner_id is the uploader's sub claim, so this policy limits each signed-in user to their own files.
Every folder needs at least one authenticated permission rule; Storage apply rejects a folder without one. Anonymous Storage permissions are not supported. See Permissions for the policy DSL.
rayfin storage init scaffolds this starter folder:
import { blob, mb, role, StorageObject } from '@microsoft/rayfin-core';
// Each signed-in user can reach only their own files. Uploads keep the default
// onConflict: 'error', so reusing an object path is reported, not overwritten.
@blob({ maxSize: mb(10) })
@role('authenticated', '*', {
policy: (claims, item) => claims.sub.eq(item.owner_id),
})
export class Attachment extends StorageObject {}Folder options
| Option | Type | Default | Description |
|---|---|---|---|
name | string | normalized class name | Folder name: 3–63 lowercase letters, digits, or hyphens after normalization. @blob('profile-images') is shorthand for @blob({ name: 'profile-images' }). |
onConflict | 'error' | 'overwrite' | 'error' | error rejects an upload to an existing path with Conflict; overwrite replaces the object and requires an authenticated role with update. |
maxSize | number | ByteSize | service limit | Maximum object size. Use the bytes(), kb(), mb(), or gb() helpers for typed byte values. |
allowedContentTypes | MimeGlob[] | any type | MIME globs such as 'image/*', or constants from ContentTypes. Other types fail with UnsupportedContentType. |
Keep a folder's name stable. Without an explicit name, renaming the class renames the folder, and the next apply treats the old folder as removed — see Remove a folder.
Register folders
List every folder you deploy in the runtime schema array. The array is authoritative: Rayfin does not apply a decorated @blob class that is not registered.
import { Attachment } from './Attachment.js';
import { ProfileImage } from './ProfileImage.js';
export const schema = [Attachment, ProfileImage];Give the client a matching type map in a separate file, so browser code never imports the decorated classes. Each key is the folder name — the class name, or the @blob({ name }) value when one is set. Each value lists the folder's custom metadata fields, or object when it has none; the client adds the stored-object envelope (path, size, contentType, and so on) itself.
export type AppStorageSchema = {
Attachment: object;
'profile-images': object;
};A folder registered in schema.ts but missing from AppStorageSchema is deployed but not typed on the client. Use bracket notation for keys with a hyphen, such as client.storage['profile-images'], and do not use the class name as the key when the folder has an explicit name.
Projects that export AppStorageSchema from schema.ts with the classes as values ('profile-images': ProfileImage) can keep that shape, as long as the client imports it with import type.
Add Storage to the client
Register Storage on the client you already create with RayfinClient.create, using storageService() from @microsoft/rayfin-storage. Storage shares that client's authenticated session; do not create a second client or session for it.
import { RayfinClient } from '@microsoft/rayfin-client';
import { browserSession } from '@microsoft/rayfin-auth';
import { dataService } from '@microsoft/rayfin-data';
import { storageService } from '@microsoft/rayfin-storage';
import type { AppSchema } from '../../rayfin/data/schema';
import type { AppStorageSchema } from '../../rayfin/storage/client-schema';
export const rayfinClient = RayfinClient.create({
baseUrl: import.meta.env.VITE_RAYFIN_API_URL,
publishableKey: import.meta.env.VITE_RAYFIN_PUBLISHABLE_KEY,
auth: browserSession({ storage: true }),
services: {
data: dataService<AppSchema>(),
storage: storageService<AppStorageSchema>(),
},
});
export type AppClient = typeof rayfinClient;Each key in AppStorageSchema becomes a typed folder client: rayfinClient.storage.Attachment, rayfinClient.storage['profile-images']. The storage: true option of browserSession() controls where the sign-in session is persisted; it is unrelated to Rayfin Storage. Install @microsoft/rayfin-auth and @microsoft/rayfin-data yourself — the 2.0 client package no longer brings them in. Code that owns an ApiClient directly can still call createStorageClient(apiClient).
Upload and list files
Address every object by its complete path inside the folder, including the file name. A file at the folder root is addressed by its name alone:
import { rayfinClient } from './rayfinClient';
export async function saveAttachment(file: File): Promise<string> {
const { object } = await rayfinClient.storage.Attachment.upload(file.name, file);
return object.path;
}
export async function listAttachments() {
const { items } = await rayfinClient.storage.Attachment.list({ limit: 20 });
return items;
}The 1.36 prefix option is gone; put directory segments in the path, such as reports/2026/plan.pdf. Removed in 2.0 Upload and download files covers the path rules, every operation and option, typed metadata, and error handling.
Apply Storage configuration
rayfin up applies enabled Storage declarations when it deploys. To apply only Storage — to retry, or to ship folder changes alone — run:
npx rayfin up storage applyDuring development, rayfin dev applies enabled declarations to its Fabric backend when the session starts. After you change a declaration, apply it without restarting the session: New in 2.0
npx rayfin dev storage applyrayfin dev storage apply retries Storage only. It needs a backend that a full rayfin dev run has already set up, it does not create a backend or apply other settings, and it targets the active deployment, so it rejects --workspace and --workspace-id. To switch workspaces, run full rayfin dev with the selector first.
When no @blob folder is registered, apply is skipped with No @blob folder declarations found. Storage configuration was not applied.
Remove a folder
Warning
Removing a folder declaration deletes the folder's data permanently. Once the apply succeeds, the folder is unavailable, and every object in it is deleted, with its metadata, in the background. Deleted folders cannot be restored, and the name cannot be reused until cleanup finishes.
Apply refuses to remove a folder that still holds objects:
Storage apply blocked (409): the following folder(s) are no longer declared but still contain objects and were not removed:
• profile-images
💡 Re-run with --force to authorize removing non-empty folders.Pass --force to rayfin up storage apply or rayfin dev storage apply only after you have reviewed and accepted that data loss. rayfin up --force passes the same authorization to Storage.
Removing the last folder declaration does not remove a deployed folder: with no folders declared, apply is skipped, so the deployed folder stays.
Migrate from the 1.36 experimental Storage
Storage code written for 1.36 needs four changes: remove storage from RAYFIN_FEATURE_FLAGS; import blob, StorageObject, ContentTypes, and the size helpers from @microsoft/rayfin-core instead of @microsoft/rayfin-core/experimental; register storageService() on RayfinClient.create instead of composing ExtendableRayfinClient; and pass complete object paths instead of the prefix option. Migrate from 1.36 Storage walks through each change with before-and-after code and an agent prompt. Keep folder names unchanged while you migrate, so the next apply does not treat a folder as removed.
In my Rayfin 2.0 project, add a Rayfin Storage folder named 'profile-images' for user profile pictures.
1. Read rayfin/rayfin.yml. Storage requires the Data service: if services.data.enabled is not true, ask me before enabling it.
2. If Storage is not set up yet, run `npx rayfin storage init` (npm projects). Otherwise install @microsoft/rayfin-storage at the same exact version as my other @microsoft/rayfin-* packages and set services.data.enabled: true (dialect mssql) and services.storage.enabled: true.
3. Create rayfin/storage/ProfileImage.ts: a class extending StorageObject with @blob({ name: 'profile-images', maxSize: mb(5), allowedContentTypes: [ContentTypes.AnyImage] }) and @role('authenticated', '*', { policy: (claims, item) => claims.sub.eq(item.owner_id) }). Import blob, ContentTypes, mb, role, and StorageObject from @microsoft/rayfin-core — not from @microsoft/rayfin-core/experimental.
4. Add ProfileImage to the runtime schema array in rayfin/storage/schema.ts, and add a 'profile-images': object key to the AppStorageSchema type the client uses, without importing decorated classes into browser code.
5. If my RayfinClient.create call has no storage service yet, add storage: storageService<AppStorageSchema>() from @microsoft/rayfin-storage to its services. Do not create a second client.
6. Call client.storage['profile-images'] with complete object paths such as `avatars/${sub}/${file.name}`, where sub comes from `await client.auth.getClaims()`. There is no prefix option.
7. Apply the folder with `npx rayfin up storage apply`, or `npx rayfin dev storage apply` when a `rayfin dev` session already set up the backend. Never pass --force unless I confirm that I accept data loss.Connector authentication
Choose whether a connector runs as the signed-in user or as the app — auth.type values, defaults and allowed identities per connector type, validation, and the permissions each needs.
Upload and download files
Upload, download, list, and delete Rayfin Storage objects by complete path — path rules, operation options and results, typed metadata, and StorageError handling.