Gestion du stockage Cloudflare R2

VérifiéPrudence

Gérez les buckets et objets de stockage Cloudflare R2 avec Wrangler CLI. Créez des buckets, téléchargez/téléchargez des fichiers, configurez des domaines personnalisés et les politiques CORS.

Spar Skills Guide Bot
DevOpsIntermédiaire
7002/06/2026
Claude CodeCursorWindsurf
#cloudflare#r2#object-storage#wrangler-cli#bucket-management

Recommandé pour

Notre avis

Gérez les buckets et objets du stockage objet Cloudflare R2 via l'interface en ligne de commande Wrangler.

Points forts

  • Couvre toutes les opérations principales de R2 : buckets, objets, CORS, domaines personnalisés.
  • Fournit des exemples de commandes et configurations clairs.
  • Inclut des modèles de téléchargement en masse et des notes de migration.

Limites

  • Nécessite l'installation et l'authentification de Wrangler CLI.
  • Limitée aux opérations de base ; les fonctionnalités avancées comme la migration Sippy sont seulement mentionnées.
  • Ne couvre pas l'intégration avec Workers au-delà de la liaison wrangler.toml.
Quand l'utiliser

Lorsque vous devez gérer des buckets et objets Cloudflare R2 via CLI, en particulier pour des opérations automatisées ou en masse.

Quand l'éviter

Lorsque vous avez besoin de permissions fines ou de fonctionnalités avancées de compatibilité S3 au-delà des opérations de base.

Analyse de sécurité

Prudence
Score qualité92/100

The skill uses Bash to run Cloudflare Wrangler commands for managing R2 storage, including bucket and object deletion. While these are legitimate operations, they carry a risk of accidental or malicious data loss if misused. No injection or exfiltration risks are apparent.

Points d'attention
  • Uses Bash to execute wrangler CLI commands that can delete R2 buckets and objects, potentially causing data loss.

Exemples

Create an R2 bucket
Create a new R2 bucket named 'my-bucket' using Wrangler CLI.
Upload directory to R2
Upload all files from the ./assets directory to my-bucket/assets, preserving filenames.
Configure CORS for R2 bucket
Configure CORS rules for my-bucket to allow requests from https://example.com with GET and PUT methods.

name: r2 description: Manage Cloudflare R2 object storage buckets and objects using Wrangler CLI. Use when working with R2 buckets, uploading/downloading files, configuring custom domains, CORS, lifecycle policies, or managing R2 storage. Trigger keywords: R2, bucket, object storage, Cloudflare storage, wrangler r2. allowed-tools: Bash, Read, Grep, Glob

Cloudflare R2 Storage Management

This skill provides commands and patterns for managing Cloudflare R2 object storage using the Wrangler CLI.

Prerequisites

  • Wrangler CLI installed (npm install -g wrangler or use npx wrangler)
  • Authenticated with Cloudflare (wrangler login)

Quick Reference

Bucket Operations

# Create a bucket
wrangler r2 bucket create <BUCKET_NAME>

# List all buckets
wrangler r2 bucket list

# Delete a bucket (must be empty)
wrangler r2 bucket delete <BUCKET_NAME>

Object Operations

# Upload an object
wrangler r2 object put <BUCKET>/<KEY> --file <LOCAL_FILE> --remote

# Download an object
wrangler r2 object get <BUCKET>/<KEY> --file <LOCAL_FILE> --remote

# Delete an object
wrangler r2 object delete <BUCKET>/<KEY> --remote

Custom Domains

# Add custom domain to bucket
wrangler r2 bucket domain add <BUCKET_NAME> --domain <DOMAIN> --zone-id <ZONE_ID>

# List custom domains
wrangler r2 bucket domain list <BUCKET_NAME>

# Remove custom domain
wrangler r2 bucket domain remove <BUCKET_NAME> --domain <DOMAIN>

CORS Configuration

# Set CORS rules
wrangler r2 bucket cors put <BUCKET_NAME> --file cors.json

# Get CORS configuration
wrangler r2 bucket cors get <BUCKET_NAME>

# Clear CORS configuration
wrangler r2 bucket cors delete <BUCKET_NAME>

Example cors.json:

{
  "CORSRules": [
    {
      "AllowedOrigins": ["https://example.com", "https://*.example.com"],
      "AllowedMethods": ["GET", "PUT", "POST", "DELETE", "HEAD"],
      "AllowedHeaders": ["*"],
      "MaxAgeSeconds": 3000
    }
  ]
}

Event Notifications

# Enable notifications to a queue
wrangler r2 bucket notification create <BUCKET_NAME> \
  --event-type object-create \
  --queue <QUEUE_NAME>

Bucket Lock Rules

# List lock rules
wrangler r2 bucket lock list <BUCKET_NAME>

Detailed Operations

For detailed API reference and advanced usage, see REFERENCE.md.

Common Patterns

Bulk Upload Directory

# Upload all files from a directory
for file in ./assets/*; do
  wrangler r2 object put my-bucket/assets/$(basename "$file") --file "$file" --remote
done

Migrate from S3 (Sippy)

R2 supports incremental migration from S3 using Sippy. Configure via the Cloudflare dashboard or API.

Public Access via Custom Domain

  1. Create bucket: wrangler r2 bucket create my-bucket
  2. Get zone ID for your domain from Cloudflare dashboard
  3. Add custom domain: wrangler r2 bucket domain add my-bucket --domain assets.example.com --zone-id <ZONE_ID>
  4. Objects are now accessible at https://assets.example.com/<key>

wrangler.toml Binding

To use R2 in Workers, add to wrangler.toml:

[[r2_buckets]]
binding = "MY_BUCKET"
bucket_name = "my-bucket-name"

Important Notes

  • Bucket names: lowercase, numbers, hyphens only (3-63 chars, no leading/trailing hyphens)
  • Use --remote flag to interact with remote R2 (default is local dev)
  • R2 is S3-compatible - AWS SDK works with R2 endpoint
  • No egress fees for R2
Skills similaires