
Working with Cloudflare R2 using the AWS S3 SDK
When Cloudflare recently unveiled R2, it presented a game-changing storage solution that particularly resonates with personal projects. Not only is it remarkably affordable (often translating to zero costs for moderate users), but its compatibility with the AWS S3 SDK also makes it a compelling substitute for traditional cloud storage solutions.
Introducing Cloudflare R2
Cloudflare R2 isn't just another storage solution; it's a paradigm shift for developers. Designed to house substantial quantities of unstructured data, its unique selling proposition is the omission of exorbitant egress bandwidth fees that usually accompany many cloud storage providers.
The versatility of R2 shines through in various applications:
Cloud-Native Applications: With the world moving towards cloud-first strategies, R2 provides a robust backend storage for cloud-native apps, ensuring swift operations and data integrity.
Web Content: Whether you're a blogger or a large news portal, R2 seamlessly stores and delivers web assets, ensuring optimal load times.
Podcast Storage: For emerging podcasters or those with an established listener base, R2 offers a storage reservoir without the usual egress fees, making content delivery efficient and affordable.
Data Lakes: R2 is a boon for businesses looking to harness the power of data analytics. Its ability to hold vast swathes of unstructured data makes it a prime candidate for data lakes, aiding in analytics and big data operations.
Batch Processes Storage: Large batch processes, especially in fields like machine learning, churn out vast data sets. R2 offers a reliable repository for such outputs, be it machine learning model artifacts or expansive data sets.
Pricing Overview
Storage Costs: Free for the first 10GB every month. Beyond that, it's $0.015 per GB-month.
Class A Operations: Free for the first 1 million requests monthly, then billed at $4.50 per million requests. These are operations that typically change the state.
Class B Operations: 10 million requests are free every month, then billed at $0.36 per million requests. These operations are usually for reading the current state.
Bandwidth: There are no egress bandwidth charges.
Pricing information obtained at 12 Oct 2023
Quick Setup Guide
Embarking on the R2 journey is straightforward:
Creating a Bucket
Navigate to the Cloudflare dashboard.
Select R2 from the left sidebar.
Click on 'Create Bucket'.
Provide your desired bucket name.
Click the 'Create Bucket' button.
Obtaining API Credentials
Still on the R2 page, find "Manage R2 API Tokens" on the right sidebar.
Click it and then select 'Create API Token'.
Name your token and set the appropriate permissions.
After clicking 'Create API Token', you'll see 'Access Key ID' and 'Secret Access Key'. Ensure you save these – they won't be displayed again.
Integrating with AWS SDK (NodeJS Example)
- First, install the S3 SDK client:
npm install @aws-sdk/client-s3
const s3Client = new S3Client({
region: 'us-east-1',
endpoint: 'https://{CLOUDFLAREACCOUNTID}.r2.cloudflarestorage.com',
credentials: {
accessKeyId: {ACCESSKEYID},
secretAccessKey: {SECRETACCESSKEY}
}
});
// Example of putting an object
const putObjectCommand = new PutObjectCommand({
Bucket: {YOUR BUCKET NAME},
Key: {PATH YOU PREFERRED},
Body: JSON.stringify(payload)
});
const result = await s3Client.send(putObjectCommand);
console.log(result);
Potential Use Cases of Cloudflare R2 Storage
R2 is not just about storage; it's about possibilities:
Web Content Repository: Websites thrive on speed. With R2, store your web assets and deliver them globally at lightning speeds, enhancing user experience.
Podcast Hosting: Podcasts are the new radio. R2 ensures your episodes are stored securely and delivered swiftly, ensuring listeners remain hooked.
Data Lakes: In the age of data, R2's capability to act as a data lake ensures businesses can analyze vast data sets efficiently, driving actionable insights.
Backup & Archive: In the digital world, data is gold. R2 ensures your digital gold is safely backed up and archived, ready for retrieval when needed.
Machine Learning Artifacts: Machine learning is data-hungry. Post analysis, the generated artifacts find a safe haven in R2, ready for future iterations or deployments.
CDN Integration: Speed is of the essence. Integrate R2 with popular CDNs to ensure your content races to its destination.
App Storage: Apps are dynamic entities. Whether it's user-generated content, logs, or dynamic updates, R2 handles it with aplomb, ensuring smooth app operations.
In the world of cloud storage, R2 is a breath of fresh air. Dive in, and you might find it to be the solution you've been searching for!