Cloudflare R2
Python wrapper on Cloudflare R2. See Cloudflare API.
Access Bucket
Bases: CF
Add secrets to .env file:
Field in .env | Cloudflare API Credential | Where credential found |
---|---|---|
CF_ACCT_ID |
Account ID | https://dash.cloudflare.com/<acct_id>/r2 |
CF_R2_REGION |
Default Region: apac |
See options |
R2_ACCESS_KEY_ID |
Key | When R2 Token created in https://dash.cloudflare.com/<acct_id>/r2/overview/api-tokens |
R2_SECRET_ACCESS_KEY |
Secret | When R2 Token created in https://dash.cloudflare.com/<acct_id>/r2/overview/api-tokens |
Examples:
>>> import os
>>> os.environ['CF_ACCT_ID'] = "ACT"
>>> os.environ['R2_ACCESS_KEY_ID'] = "ABC"
>>> os.environ['R2_SECRET_ACCESS_KEY'] = "XYZ"
>>> r2 = CloudflareR2()
>>> type(r2.resource)
<class 'boto3.resources.factory.s3.ServiceResource'>
Source code in cloudflare_r2/main.py
Attributes
resource
property
Access to buckets via instance, e.g. r2.resource.Bucket('<name>')
Functions
Common Actions on Bucket
Bases: CloudflareR2
Helper function that can be assigned to each bucket.
Note AWS API reference vs. R2
Examples:
>>> import os
>>> os.environ['CF_ACCT_ID'] = "ACT"
>>> os.environ['R2_ACCESS_KEY_ID'] = "ABC"
>>> os.environ['R2_SECRET_ACCESS_KEY'] = "XYZ"
>>> obj = CloudflareR2Bucket(name='test')
>>> type(obj.bucket)
<class 'boto3.resources.factory.s3.Bucket'>
Source code in cloudflare_r2/main.py
Python | |
---|---|
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
|
Functions
all_items()
Using pagination conventions from s3 and r2, get all prefixes found in
the bucket name. Note this aggregates all fetch()
calls, specifically limiting
the response to the "Contents" key of each fetch()
call. Such key will
contain a list of dict-based prefixes.
Returns:
Type | Description |
---|---|
list[dict] | None
|
list[dict] | None: Get objects form the bucket |
Source code in cloudflare_r2/main.py
download(key, local_file)
With a r2-bucket key
, download its contents to local_file
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
Origin file to download |
required |
local_file |
str
|
Where to download, how to name downloaded file |
required |
Source code in cloudflare_r2/main.py
fetch(*args, **kwargs)
Each bucket contain content prefixes but can only be fetched by batches. Each batch is limited to a max of 1000 prefixes. Without arguments included in this call, will default to the first 1000 keys.
See details in boto3 list-objects-v2 API docs
Source code in cloudflare_r2/main.py
filter_content(filter_suffix, objects_list)
classmethod
Filter objects based on a filter_suffix
from either:
- List of objects from
self.all_items()
; or - Contents key of
self.fetch()
. Note that each Contents field offetch
is a dict object, each object will contain a Key field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filter_suffix |
str
|
Prefix terminates with what suffix |
required |
objects_list |
list[dict]
|
List of objects previously fetched |
required |
Yields:
Type | Description |
---|---|
Iterator[dict]
|
Iterator[dict]: Filtered |
Source code in cloudflare_r2/main.py
get(key, *args, **kwargs)
Assumes the key prefix exists in the bucket. See helper for boto3 get_object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
Should exist in the bucket. |
required |
Returns:
Type | Description |
---|---|
dict | None
|
dict | None: Returns |
Source code in cloudflare_r2/main.py
get_root_prefixes()
See adapted recipe from boto3 re: top-level prefixes.
Returns:
Type | Description |
---|---|
list[str]: Matching prefixes in the root of the bucket. |
Source code in cloudflare_r2/main.py
upload(file_like, key, *args, **kwargs)
Upload local file_like
contents to r2-bucket path key
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_like |
str | Path
|
Local file |
required |
key |
str
|
Remote location Defaults to {}. |
required |