Cloudflare Docs
Edit this page on GitHub
Set theme to dark (⇧+D)

Configure boto3 for R2

You must generate an Access Key before getting started. All examples will utilize access_key_id and access_key_secret variables which represent the Access Key ID and Secret Access Key values you generated.

You must configure boto3 to use a preconstructed endpoint_url value. This can be done through any boto3 usage that accepts connection arguments; for example:

import boto3
s3 = boto3.resource('s3',
endpoint_url = 'https://<accountid>',
aws_access_key_id = '<access_key_id>',
aws_secret_access_key = '<access_key_secret>'

You may, however, omit the aws_access_key_id and aws_secret_access_key arguments and allow boto3 to rely on the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables instead.

An example script may look like the following:
import boto3
s3 = boto3.client(
service_name ="s3",
endpoint_url = 'https://<accountid>',
aws_access_key_id = '<access_key_id>',
aws_secret_access_key = '<access_key_secret>',
region_name="<location>", # Must be one of: wnam, enam, weur, eeur, apac, auto
# Get object information
object_information = s3.head_object(Bucket=<R2_BUCKET_NAME>, Key=<FILE_KEY_NAME>)
# Upload/Update single file
s3.upload_fileobj(io.BytesIO(file_content), <R2_BUCKET_NAME>, <FILE_KEY_NAME>)
# Delete object
s3.delete_object(Bucket=<R2_BUCKET_NAME>, Key=<FILE_KEY_NAME>)
$ python
# Buckets:
# - user-uploads
# - my-bucket-name
# Objects:
# - cat.png
# - todos.txt