R2Client
is a Python client library for interacting with Cloudflare R2 storage, facilitating easy and quick HTTP requests to manage files within an R2 bucket. This framework leverages Python's native packages to sign and send requests, making it straightforward to integrate into existing Python projects.
- File upload and download
- Listing files and folders within a bucket
To install r2client
, simply use pip:
pip install r2client
Here's how to get started with r2client
:
First, import R2Client
and initialize it with your credentials:
from r2client.R2Client import R2Client as r2
# Initialize the R2Client
client = r2(
access_key='<ACCESS_KEY>',
secret_key='<SECRET_KEY>',
endpoint='<ENDPOINT> (example: "https://***.r2.cloudflarestorage.com")'
)
To upload a file to your R2 bucket:
bucket_name = 'your-bucket-name'
local_file_path = 'path/to/your/local/file'
r2_file_key = 'desired/path/in/bucket'
client.upload_file(bucket_name, local_file_path, r2_file_key)
To download a file from your R2 bucket:
file_key = 'path/to/the/file/in/bucket'
local_file_name = 'path/to/save/the/downloaded/file'
client.download_file(bucket_name, file_key, local_file_name)
To list files in a specific bucket:
files_dict = client.list_files(bucket_name)
print(files_dict)
To list folders within a bucket:
folders = client.list_folders(bucket_name)
print(folders)
Contributions are welcome! Please feel free to submit a pull request or create an issue for any bugs or feature requests.
This project is licensed under the MIT License - see the LICENSE file for details.