@computesdk/r2
v1.1.1
Published
Cloudflare R2 storage provider for ComputeSDK - S3-compatible object storage
Maintainers
Readme
@computesdk/r2
Cloudflare R2 storage provider for ComputeSDK - S3-compatible object storage.
Installation
npm install @computesdk/r2Usage
import { r2 } from '@computesdk/r2';
// Create storage instance
const storage = r2({
accessKeyId: process.env.R2_ACCESS_KEY_ID,
secretAccessKey: process.env.R2_SECRET_ACCESS_KEY,
accountId: process.env.R2_ACCOUNT_ID
// Or use endpoint directly:
// endpoint: 'https://your-account-id.r2.cloudflarestorage.com'
});
// Upload a file
await storage.upload('my-bucket', 'path/to/file.txt', 'Hello, World!');
// Download a file
const result = await storage.download('my-bucket', 'path/to/file.txt');
console.log(result.data.toString());
// Delete a file
await storage.delete('my-bucket', 'path/to/file.txt');
// List objects
const list = await storage.list('my-bucket', { prefix: 'path/to/' });
for (const obj of list.objects) {
console.log(`${obj.key}: ${obj.size} bytes`);
}Configuration
| Option | Type | Required | Default | Description |
|--------|------|----------|---------|-------------|
| accessKeyId | string | No* | R2_ACCESS_KEY_ID env | R2 access key |
| secretAccessKey | string | No* | R2_SECRET_ACCESS_KEY env | R2 secret key |
| accountId | string | No† | R2_ACCOUNT_ID env | Cloudflare account ID |
| endpoint | string | No† | - | Full R2 endpoint URL |
*Either provide in config or set environment variables.
†Either provide accountId (endpoint will be auto-constructed) or provide full endpoint.
Environment Variables
export R2_ACCESS_KEY_ID=your-access-key
export R2_SECRET_ACCESS_KEY=your-secret-key
export R2_ACCOUNT_ID=your-account-idAPI
See the @computesdk/s3 documentation for full API details.
Getting R2 Credentials
- Go to Cloudflare Dashboard
- Navigate to R2 > Manage R2 API Tokens
- Create a new API token with Object Read & Write permissions
- Note the Access Key ID, Secret Access Key, and Account ID
License
MIT
