Skip to main content

R2Connect

PyPI version

The R2Connect Python module provides a convenient interface for performing common operations on AWS S3 buckets while being compatible with Cloudflare's R2 service. It allows you to create, delete, upload, download and delete objects in an S3 bucket. It also allows creating and deleting buckets, handling various exceptions that might occur during these operations.

Table of Contents

Installation

You can install the R2Connect module using pip:

pip install r2connect

Initialisation

Before initialising an R2Client class, make sure to set the following environment variables:

  • ENDPOINT_URL: The endpoint URL for your AWS S3 or Cloudflare R2 service.
  • ACCESS_KEY: Your AWS S3 or Cloudflare R2 access key.
  • SECRET_KEY: Your AWS S3 or Cloudflare R2 secret key.
  • REGION: The AWS S3 region (Cloudflare doesn't require a region but defaults to us-east-1)

To initialise an R2Client class, follow the example below:

from r2connect.r2client import R2Client

try:
    r2_client = R2Client()
except r2connect.exceptions.cloudflare.r2.MissingConfig as error:
    # A required environment variable is missing
    print(error)

Usage

To use the R2Client class, follow the examples below:

Create a Bucket

With Cloudflare's R2 service, only us-east-1 can be used. R2 uses the region for AWS compatibility only.

from r2connect.r2client import R2Client

# Initialise the R2Client class (as shown in the previous section)
# ...

bucket_name = "my-new-bucket"
region = "us-east-1"  # Must be us-east-1 when using Cloudflare R2

try:
    r2_client.create_bucket(bucket_name, region)
except r2connect.exceptions.cloudflare.r2.BucketAlreadyExists as error:
    print(f"The specified bucket already exists: {bucket_name}")
except Exception as error:
    print(error)

Delete a Bucket

A bucket can be deleted in two ways. If the bucket is not empty you can attempt a safe delete which will only delete if the bucket is empty. You can also set the force_delete flag to True which will delete the bucket and its contents. Below is an example for each:

Safe delete a bucket
from r2connect.r2client import R2Client

# Initialise the R2Client class (as shown in the previous section)
# ...

bucket_name = "my-existing-bucket"

try:
    r2_client.delete(bucket_name)
except r2connect.exceptions.cloudflare.r2.BucketDoesNotExist as error:
    print(f"The specified bucket does not exist: {bucket_name}")
except r2connect.exceptions.cloudflare.r2.BucketIsNotEmpty as error:
    print(f"The specified bucket is not empty, cannt safe delete: {bucket_name}")
except Exception as error:
    print(error)
Force delete a bucket
from r2connect.r2client import R2Client

# Initialise the R2Client class (as shown in the previous section)
# ...

bucket_name = "my-existing-bucket"

try:
    r2_client.delete(bucket_name, force_delete=True)
except r2connect.exceptions.cloudflare.r2.BucketDoesNotExist as error:
    print(f"The specified bucket does not exist: {bucket_name}")
except Exception as error:
    print(error)

Upload a File

from r2connect.r2client import R2Client

# Initialise the R2Client class (as shown in the previous section)
# ...

bucket_name = "my-existing-bucket"
file_path = "path/to/you/file.txt"
object_name = "file.txt"

try:
	r2_client.upload_file(file_path, object_name, bucket_name)
except r2connect.exceptions.cloudflare.r2.BucketDoesNotExist as error:
	print(f"The specified bucket does not exist: {bucket_name}")
except r2connect.exceptions.cloudflare.r2.ObjectAlreadyExists as error:
	print(f"An object with the same object_key already exists: {object_name}")
except Exception as error:
	print(error)

Download a File

A save filepath can be specified but is not required. If one isn't provided, the file will be saved in the same execution level with the user_id as the prefix and the filename as the suffix.

from r2connect.r2client import R2Client

# Initialise the R2Client class (as shown in the previous section)
# ...

bucket_name = "my-existing-bucket"
download_file_path = "path/to/you/file.txt"
object_name = "file.txt"

try:
	r2_client.download_file(object_name, bucket_name, download_file_path)
except r2connect.exceptions.cloudflare.r2.ObjectDoesNotExist as error:
	print(f"The specified file does not exist: {object_name}")
except r2connect.exceptions.cloudflare.r2.BucketDoesNotExist as error:
	print(f"The specified bucket does not exist: {bucket_name}")
except Exception as error:
	print(error)

Delete a File

from r2connect.r2client import R2Client

# Initialise the R2Client class (as shown in the previous section)
# ...

bucket_name = "my-existing-bucket"
object_name = "file.txt"

try:
	r2_client.delete_file(object_name, bucket_name)
except r2connect.exceptions.cloudflare.r2.ObjectDoesNotExist as error:
	print(f"The specified object does not exist in this bucket: {object_name}")
except r2connect.exceptions.cloudflare.r2.BucketDoesNotExist as error:
	print(f"The specified bucket does not exist: {bucket_name}")
except Exception as error:
	print(error)

Exception Handling

The R2Connect module provides exception handling for various scenarios.

  • BucketAlreadyExists: Raised when attempting to create a bucket that already exists.
  • BucketDoesNotExist: Raised when the specified bucket does not exist.
  • BucketIsNotEmpty: Raised when trying to delete a non-empty bucket without specifying force_delete=True
  • ObjectDoesNotExist: Raised when attempting operation on a non-existing object.
  • ObjectAlreadyExists: Raised when trying to upload an object with the same name as an existing object.
  • MissingConfig: Raised when trying to initialise an R2Client object without the required environment variables.

Metadata

Release files for r2connect 1.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for r2connect 1.1.2
File Size Uploaded
r2connect-1.1.2.tar.gz 19.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for r2connect 1.1.2
File Interpreter ABI Platform
r2connect-1.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 39.4 kB

Release files / r2connect-1.1.2.tar.gz

Download URL r2connect-1.1.2.tar.gz
Size 19.6 kB
Tags Source
SHA-256 checksum
How to use checksums
be60cd5785588d3a1465a0e40b1eaad7708d9f7a33143bc4ee4118c5c2be1860
BLAKE2b-256 checksum
How to use checksums
08d419c74e5938eab6ca74ac41863f7c767d816b7151cdff2a82a01143998c5b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Release files / r2connect-1.1.2-py3-none-any.whl

Download URL r2connect-1.1.2-py3-none-any.whl
Size 19.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6a07cb94da44ef3712d4e1f5629a75cc76248fe1f6a4afc04d140542c5fbd8ca
BLAKE2b-256 checksum
How to use checksums
a081aa69e908b1e5b45ee5a2f90e7c0ad6e96ec1e2db63fa24c00252e78fc882
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.0

Release history Release notifications | RSS feed

This release

1.1.2 This release

2 release files

1.1.1

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page