Spaces:
Paused
Paused
| """ | |
| Cloudflare R2 Client | |
| """ | |
| import boto3 | |
| from botocore.config import Config | |
| class R2Client: | |
| def __init__(self, account_id, bucket, access_key, secret_key, public_domain): | |
| self.s3 = boto3.client( | |
| "s3", | |
| endpoint_url=f"https://{account_id}.r2.cloudflarestorage.com", | |
| region_name="auto", | |
| aws_access_key_id=access_key, | |
| aws_secret_access_key=secret_key, | |
| config=Config(signature_version="s3v4") | |
| ) | |
| self.bucket = bucket | |
| self.public_domain = public_domain | |
| def upload_file(self, local_path: str, key: str): | |
| self.s3.upload_file(local_path, self.bucket, key) | |
| return f"{self.public_domain}/{key}" | |
| def list_objects(self, prefix: str = ""): | |
| response = self.s3.list_objects_v2(Bucket=self.bucket, Prefix=prefix) | |
| return response.get("Contents", []) |