Why Move from AWS S3 to Cloudflare R2? Advantages, Pricing Comparison, and Migration Guide

Amazon S3 is a leading object storage service, but its pricing model, particularly for data egress, often becomes a significant burden for businesses with high outbound data needs. Cloudflare R2, a relatively new option, offers an attractive alternative with its simplified pricing and performance benefits.

In this blog, we will explore why you should consider moving from AWS S3 to Cloudflare R2, compare their pricing with real-world examples, and provide a step-by-step migration guide with Node.js code.

Table of Contents:

  1. Why choose Cloudflare R2 over AWS S3?
  2. Pricing comparison: AWS S3 vs. Cloudflare R2
  3. When AWS S3 gets expensive?
  4. How to migrate from AWS S3 to Cloudflare R2?

Why choose Cloudflare R2 over AWS S3?

  1. No Egress Charges: AWS S3 charges $0.09 per GB for data transferred out of its network (first 10TB/month). In contrast, Cloudflare R2 eliminates egress charges entirely, making it far more cost-effective for high outbound data use cases.
  2. S3-Compatible API: Cloudflare R2 uses the same API conventions as AWS S3, allowing you to migrate with minimal code changes. This compatibility makes the transition straightforward and quick.
  3. Cost Efficiency: R2 offers competitive storage costs at $0.015 per GB per month compared to S3’s $0.023 per GB. This pricing difference adds up significantly for large-scale storage needs.
  4. Global CDN Integration: R2 integrates seamlessly with Cloudflare’s global content delivery network (CDN), ensuring faster data access for users worldwide while reducing latency.
  5. No Complex Tiers: Unlike AWS S3's various storage tiers (Standard, Infrequent Access, Glacier), R2 provides a single-tier storage model with consistent performance and no retrieval penalties.

Pricing comparison: AWS S3 vs. Cloudflare R2

To understand the financial benefits of R2, let’s compare its pricing to AWS S3 with a practical example.

Feature AWS S3 (Standard) Cloudflare R2
Storage Cost $0.023/GB per month $0.015/GB per month
Data Egress Cost $0.09/GB (first 10TB) $0 (unlimited)
API Requests (PUT) $0.005 per 1,000 requests $0.005 per 1,000 requests
API Requests (GET) $0.0004 per 1,000 requests $0.004 per 10,000 requests

Sample Pricing Scenario

Example Usage:

  • Storage: 1TB
  • Monthly Data Egress: 5TB
  • PUT Requests: 500,000
  • GET Requests: 10,000,000
Provider Storage Cost Egress Cost PUT/GET Requests Total Cost
AWS S3 $23.00 $450.00 $4.20 $477.20
Cloudflare R2 $15.00 $0.00 $2.50 $17.50

When AWS S3 gets expensive?

1. Media Streaming Services:

Streaming platforms serving high-quality content, such as 4K videos or live streams, often incur exorbitant egress charges. For example:

  • Daily Data Served: 2TB
  • Monthly Data Served: 60TB
  • AWS S3 Egress Cost: $5,400/month
  • R2 Egress Cost: $0

2. Public Datasets or Downloads:

Organizations offering free datasets or software downloads can face massive outbound data costs. For example:

  • Dataset Size: 100GB
  • Monthly Downloads: 1,000 users
  • Total Data Egress: 100TB
  • AWS S3 Egress Cost: $9,000/month
  • R2 Egress Cost: $0

3. Cross-Region Data Sharing:

For globally distributed systems that share data across regions, AWS S3’s inter-region transfer costs quickly escalate:

  • Data Transferred Between Regions: 20TB/month
  • AWS S3 Egress Cost: $1,800/month
  • R2 Egress Cost: $0

By eliminating egress charges, Cloudflare R2 is an ideal solution for businesses with high outbound data needs.

How to migrate from AWS S3 to Cloudflare R2?

Migrating your data from S3 to R2 is straightforward thanks to R2’s S3-compatible API. Here is a step-by-step guide.

Step 1: Set up Cloudflare R2

  1. Log in to your Cloudflare dashboard and navigate to the R2 section.
  2. Create an R2 bucket and note down the Access Key ID, Secret Access Key, and Bucket URL.

Step 2: Install required dependencies

Install the necessary Node.js packages for AWS S3 and R2.

npm install aws-sdk @aws-sdk/client-s3

Step 3: Copy data from S3 to R2

Node.js Script for Migration:

const AWS = require('aws-sdk');
const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3');

// AWS S3 Configuration
const s3 = new AWS.S3({
  accessKeyId: 'YOUR_AWS_ACCESS_KEY',
  secretAccessKey: 'YOUR_AWS_SECRET_KEY',
  region: 'YOUR_AWS_REGION',
});

// Cloudflare R2 Configuration
const r2Client = new S3Client({
  region: 'auto',
  endpoint: 'https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com',
  credentials: {
    accessKeyId: 'YOUR_R2_ACCESS_KEY',
    secretAccessKey: 'YOUR_R2_SECRET_KEY',
  },
});

async function migrateData(s3BucketName, r2BucketName) {
  try {
    // List objects from S3
    const { Contents } = await s3.listObjectsV2({ Bucket: s3BucketName }).promise();

    for (const object of Contents) {
      const { Key } = object;

      // Get the object from S3
      const data = await s3.getObject({ Bucket: s3BucketName, Key }).promise();

      // Upload the object to R2
      const putCommand = new PutObjectCommand({
        Bucket: r2BucketName,
        Key,
        Body: data.Body,
        ContentType: data.ContentType,
      });
      await r2Client.send(putCommand);

      console.log(`Migrated: ${Key}`);
    }

    console.log('Migration completed successfully!');
  } catch (error) {
    console.error('Error during migration:', error);
  }
}

// Replace with your bucket names
migrateData('your-s3-bucket', 'your-r2-bucket');

Step 4: Update your application

After migrating your data, update your application to use R2 by configuring the new endpoint and credentials.

const { S3Client } = require('@aws-sdk/client-s3');

// Update to use R2
const r2Client = new S3Client({
  region: 'auto',
  endpoint: 'https://YOUR_ACCOUNT_ID.r2.cloudflarestorage.com',
  credentials: {
    accessKeyId: 'YOUR_R2_ACCESS_KEY',
    secretAccessKey: 'YOUR_R2_SECRET_KEY',
  },
});

// Use r2Client for all future operations

Step 5: Test your application

Thoroughly test your application to ensure functionality such as uploads, downloads, and deletes work seamlessly with R2.

Conclusion:

Cloudflare R2 is a compelling alternative to AWS S3, especially for businesses with high egress data needs. With its predictable pricing model, S3-compatible API, and integrated global CDN, R2 offers cost efficiency without compromising performance. By following the steps outlined in this guide, you can migrate seamlessly and start reaping the benefits of Cloudflare R2. Switch to R2 today and say goodbye to unexpected egress charges.

Atatus

#1 Solution for Logs, Traces & Metrics

tick-logo APM

tick-logo Kubernetes

tick-logo Logs

tick-logo Synthetics

tick-logo RUM

tick-logo Serverless

tick-logo Security

tick-logo More

Pavithra Parthiban

Pavithra Parthiban

A technical content writer specializing in monitoring and observability tools, adept at making complex concepts easy to understand.
Chennai