Andi Ashari

Tech Voyager & Digital Visionary

Working with Cloudflare R2 using the AWS S3 SDK

Working with Cloudflare R2 using the AWS S3 SDK

Cloudflare R2 is setting new standards in the cloud storage landscape, especially for personal projects. Its affordability and compatibility with the AWS S3 SDK position it as a compelling alternative to traditional cloud storage options.

Key Features and Applications of Cloudflare R2

  • Cloud-Native Applications: R2 ensures fast operations and data integrity for cloud-native apps.
  • Web Content: An efficient solution for bloggers or large portals, optimizing storage and delivery of web assets.
  • Podcast Storage: Offers cost-effective storage solutions for podcasters, free from egress fees.
  • Data Lakes: Ideal for storing large volumes of unstructured data, facilitating data analytics.
  • Batch Processes Storage: Serves as a reliable repository for data sets from batch processes, supporting machine learning and more.

Pricing Overview

  • Storage Costs: First 10GB free each month; beyond that, $0.015 per GB-month.
  • Class A Operations: First 1 million requests each month are free; then, $4.50 per million.
  • Class B Operations: First 10 million requests free; afterwards, $0.36 per million requests.
  • Bandwidth: R2’s significant advantage includes no egress bandwidth charges.
  • 💡 Note: Pricing information as of 12 October 2023.

Pricing Comparison with AWS S3

Comparing R2 and AWS S3 for 600GB storage and specific request volumes reveals R2 as the more cost-effective option:

  • Storage Costs: AWS S3 at $13.80 vs. R2 at $9.00.
  • PUT Requests: AWS S3 at $0.50 vs. R2 at $0.045.
  • GET Requests: AWS S3 at $0.12 vs. R2 at $0.108.
  • Total Costs: AWS S3 at $14.42 vs. R2 at $9.15 for the outlined scenario.

Quick Setup Guide

Creating a Bucket

  1. Go to the Cloudflare dashboard.
  2. Select R2 from the sidebar.
  3. Click ‘Create Bucket’.
  4. Enter your bucket name and click ‘Create Bucket’.

Obtaining API Credentials

  1. On the R2 page, click “Manage R2 API Tokens”.
  2. Click ‘Create API Token’, name your token, set permissions, and note your ‘Access Key ID’ and ‘Secret Access Key’.

Integrating with AWS SDK (NodeJS Example)

const s3Client = new S3Client({
    region: 'us-east-1',
    endpoint: 'https://{CLOUDFLARE_ACCOUNT_ID}',
    credentials: {
        accessKeyId: {ACCESS_KEY_ID},
        secretAccessKey: {SECRET_ACCESS_KEY}

const putObjectCommand = new PutObjectCommand({
    Bucket: {YOUR BUCKET NAME},
    Body: JSON.stringify(payload)

const result = await s3Client.send(putObjectCommand);

Potential Use Cases of Cloudflare R2 Storage

R2 opens up new possibilities for various applications:

  • Web Content Repository: Enhances user experience with fast global content delivery.
  • Podcast Hosting: Secure and swift delivery keeps listeners engaged.
  • Data Lakes: Facilitates efficient analysis of vast data sets.
  • Backup & Archive: Safeguards digital assets for future retrieval.
  • Machine Learning Artifacts: Provides storage for data-intensive machine learning projects.
  • CDN Integration: Integrates with CDNs for rapid content delivery.
  • App Storage: Manages dynamic app-related data efficiently.

Cloudflare R2 represents a significant shift in cloud storage, offering a versatile and cost-effective solution for a wide range of applications.