How to Upload Files to R2 Using Python and Boto3: A Comprehensive Guide

I mostly use Go or Node.js for my projects, but I recently had to work on a small Python project that required uploading files to R2.
I found the process to be quite straightforward, thanks to the Boto3 library. In this guide, I'll walk you through the process of uploading files to R2 using Python and Boto3.

Prerequisites

  • Python
  • boto3 library installed (pip install boto3)
  • An R2 account with Cloudflare
  • Your R2 credentials (Access Key ID and Secret Access Key)

Step 1: Setting Up Your Environment

First, let's set up our Python environment and import the necessary libraries:
python
import boto3
import os
from botocore.exceptions import ClientError

Step 2: Configuring Boto3 for R2

Next, we'll create a Boto3 client configured for R2:
python
account_id = '<account_id>'
access_key_id = '<access_key_id>'
access_key_secret = '<access_key_secret>
then we'll create a Boto3 client configured for R2:
python
r2 = boto3.client('s3',
    endpoint_url=f'https://{account_id}.r2.cloudflarestorage.com',
    aws_access_key_id=access_key_id,
    aws_secret_access_key=access_key_secret
)
this will create a Boto3 client that is configured to interact with R2.
Make sure to replace <account_id>, <access_key_id>, and <access_key_secret> with your actual R2 account ID, access key ID, and secret access key, respectively.

Step 3: Uploading Files to R2

Now that we have our Boto3 client configured, we can start uploading files to R2. Let's define a function that will handle the file upload:
python
def upload_file(file_name, bucket_name, object_name=None):
    if object_name is None:
        object_name = file_name
 
    try:
        r2.upload_file(file_name, bucket_name, object_name)
    except ClientError as e:
        print(f"An error occurred: {e}")
        return False
    return True

Step 4: Uploading a File

Now, let's upload a file to R2 using the upload_file function we defined earlier:
python
file_path = '/path/to/your/file.txt'
bucket_name = 'my-bucket'
 
if upload_file(file_path, bucket_name):
    print(f"File {file_path} uploaded successfully to {bucket_name}")
else:
    print(f"File upload failed")
this will upload the file located at file_path to the bucket my-bucket on R2. If the upload is successful, you'll see a success message; otherwise, you'll see an error message.

Conclusion

In this guide, we walked through the process of uploading files to R2 using Python and Boto3. By following these steps, you can streamline your file upload process and enhance your cloud storage capabilities. I hope you found this guide helpful, and I encourage you to explore the Boto3 library further to unlock even more functionality for interacting with R2 and other cloud storage solutions.

References