File Storage
For storing large files outside the database, configure the fileStore interface. This is useful for handling file uploads, temporary files, and other binary data that shouldn’t be stored in the database.
Configuration
Section titled “Configuration”The fileStore interface requires three methods:
storeFile(content)- Store a file and return a unique referencegetFile(fileRef)- Retrieve a file by its referencedeleteFile(fileRef)- Delete a file by its reference
Platform Examples
Section titled “Platform Examples”Generic Implementation
Section titled “Generic Implementation”configuration.fileStore = { async storeFile(content) { // Store file and return reference return fileReference; }, async getFile(fileRef) { // Retrieve file by reference return fileBuffer; }, async deleteFile(fileRef) { // Delete file by reference }};AWS S3
Section titled “AWS S3”import AWS from 'aws-sdk';
const s3 = new AWS.S3();
configuration.fileStore = { async storeFile(content) { const fileId = `file_${Date.now()}_${Math.random()}`; await s3.putObject({ Bucket: 'my-bucket', Key: fileId, Body: content }).promise(); return fileId; }, async getFile(fileId) { const result = await s3.getObject({ Bucket: 'my-bucket', Key: fileId }).promise(); return result.Body; }, async deleteFile(fileId) { await s3.deleteObject({ Bucket: 'my-bucket', Key: fileId }).promise(); }};S3 Setup:
- Install AWS SDK:
Terminal window npm i aws-sdkTerminal window pnpm add aws-sdkTerminal window yarn add aws-sdk - Configure AWS credentials (environment variables, IAM role, or credentials file)
- Create an S3 bucket in your AWS account
- Grant appropriate permissions to your application
Cloudflare R2
Section titled “Cloudflare R2”The Cloudflare Workers adapter sets this file store for you when an R2 bucket is bound as FILES. Write it yourself only when you wire the module by hand:
import { env } from 'cloudflare:workers';import type { ClientConfig } from '@crowdin/app-project-module';
const configuration: ClientConfig = { // ... other config fileStore: { getFile: async (fileId: string): Promise<Buffer> => { const object = await env.FILES.get(fileId); if (!object) { throw new Error(`File not found: ${fileId}`); } return Buffer.from(await object.arrayBuffer()); }, storeFile: async (content: Buffer): Promise<string> => { const fileId = `file_${crypto.randomUUID()}`; await env.FILES.put(fileId, content); return fileId; }, deleteFile: async (fileId: string): Promise<void> => { await env.FILES.delete(fileId); } }};R2 Setup:
- Create bucket:
npx wrangler r2 bucket create crowdin-app-files - Make files expire after one day:
npx wrangler r2 bucket lifecycle add crowdin-app-files expire-files --expire-days 1 --force - Add to
wrangler.jsonc:"r2_buckets": [{"binding": "FILES","bucket_name": "crowdin-app-files"}]
See the Cloudflare Workers deployment guide for a complete example.