Since, cloudflare R2 is compatible with S3 API, we can simply use boto3 to work with R2 as wll
import logging
import boto3
from botocore.exceptions import ClientError
from botocore.client import Config
def create_presigned_url(bucket_name, object_name, expiration=3600):
s3_client = boto3.client('s3',
endpoint_url='https://<ACCOUNT_ID>.r2.cloudflarestorage.com',
aws_access_key_id='<ACCESS_KEY>',
aws_secret_access_key='<SECRET_KEY>',
config=Config(signature_version='s3v4'),
)
try:
response = s3_client.generate_presigned_url('get_object',
Params={'Bucket': bucket_name,
'Key': object_name},
ExpiresIn=expiration)
except ClientError as e:
logging.error(e)
return None
# The response contains the presigned URL
return response
if you want to use R2 with Django have a look into https://djangotherightway.com/using-cloudflare-r2-with-django-for-storage
Top comments (1)
Thanks so much for this.