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>.r2.cloudflarestorage.com', 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:
main.pyimport boto3
s3 = boto3.resource('s3', endpoint_url = 'https://<accountid>.r2.cloudflarestorage.com', aws_access_key_id = '<access_key_id>', aws_secret_access_key = '<access_key_secret>'
)
print('Buckets:')
for bucket in s3.buckets.all(): print(' - ', bucket.name)
bucket = s3.Bucket('my-bucket-name')
print('Objects:')
for item in bucket.objects.all(): print(' - ', item.key)
$ python main.py
# Buckets:
# - user-uploads
# - my-bucket-name
# Objects:
# - cat.png
# - todos.txt