Skip to content

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.

The fileStore interface requires three methods:

  • storeFile(content) - Store a file and return a unique reference
  • getFile(fileRef) - Retrieve a file by its reference
  • deleteFile(fileRef) - Delete a file by its reference
index.js
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
}
};
index.js
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:

  1. Install AWS SDK:
    Terminal window
    npm i aws-sdk
  2. Configure AWS credentials (environment variables, IAM role, or credentials file)
  3. Create an S3 bucket in your AWS account
  4. Grant appropriate permissions to your application

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:

worker/app.ts
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:

  1. Create bucket: npx wrangler r2 bucket create crowdin-app-files
  2. Make files expire after one day: npx wrangler r2 bucket lifecycle add crowdin-app-files expire-files --expire-days 1 --force
  3. Add to wrangler.jsonc:
    "r2_buckets": [
    {
    "binding": "FILES",
    "bucket_name": "crowdin-app-files"
    }
    ]

See the Cloudflare Workers deployment guide for a complete example.