Cloudflare Images
Cloudflare Images is a Paid Service
- $5 per month per 100k images stored
- $1 per month per 100k images delivered
API v1
Bases: CF
Need to setup a Cloudflare Images account to use. See Cloudflare Images docs. With required variables secured:
Field in .env | Cloudflare API Credential | Where credential found |
---|---|---|
CF_ACCT_ID |
Account ID | https://dash.cloudflare.com/<acct_id>/images/images |
CF_IMG_HASH |
Account Hash | https://dash.cloudflare.com/<acct_id>/images/images |
CF_IMG_TOKEN |
API Secret | Generate / save via https://dash.cloudflare.com/<acct_id>/profile/api-tokens |
Add secrets to .env file and use as follows:
Examples:
Source code in cloudflare_images/api.py
Python | |
---|---|
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
|
Attributes
base_api: str
property
Construct endpoint. See formula.
Examples:
>>> import os
>>> os.environ['CF_ACCT_ID'] = "ABC"
>>> os.environ['CF_IMG_HASH'], os.environ['CF_IMG_TOKEN'] = "DEF", "XYZ"
>>> cf = CloudflareImagesAPIv1()
>>> cf.base_api
'https://api.cloudflare.com/client/v4/accounts/ABC/images/v1'
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
URL endpoint to make requests with the Cloudflare-supplied credentials. |
base_delivery
property
Images are served with the following format: https://imagedelivery.net/<ACCOUNT_HASH>/<IMAGE_ID>/<VARIANT_NAME>
This property constructs the first part: https://imagedelivery.net/<ACCOUNT_HASH>
See Cloudflare docs.
Examples:
import os >>> os.environ['CF_ACCT_ID'] = "ABC" >>> os.environ['CF_IMG_HASH'], os.environ['CF_IMG_TOKEN'] = "DEF", "XYZ" >>> cf = CloudflareImagesAPIv1() >>> cf.base_delivery 'https://imagedelivery.net/DEF'
v2: str
property
See updated list API endpoint.
Examples:
Functions
delete(img_id, *args, **kwargs)
Issue httpx DELETE request to the image.
Source code in cloudflare_images/api.py
get(img_id, *args, **kwargs)
Issue httpx GET request to the image found in storage. Assuming request like
CFImage().get('target-img-id')
, returns a response with metadata:
Examples:
>>> # CFImage().get('target-img-id') commented out since hypothetical
b'{
"result": {
"id": "target-img-id",
"filename": "target-img-id",
"uploaded": "2023-02-20T09:09:41.755Z",
"requireSignedURLs": false,
"variants": [
"https://imagedelivery.net/<hash>/<target-img-id>/public",
"https://imagedelivery.net/<hash>/<target-img-id>/cover",
"https://imagedelivery.net/<hash>/<target-img-id>/avatar",
"https://imagedelivery.net/<hash>/<target-img-id>/uniform"
]
},
"success": true,
"errors": [],
"messages": []
}'
Source code in cloudflare_images/api.py
list_images(per_page=1000, sort_order='desc', continuation_token=None)
See list images API.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
per_page |
int
|
Number of items per page (10 to 10,000). Defaults to 1000. |
1000
|
sort_order |
str
|
Sorting order by upload time (asc | desc). Defaults to "desc". |
'desc'
|
continuation_token |
str | None
|
Continuation token for a next page. List images V2 returns continuation_token. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Response
|
httpx.Response: Contains top-level fields for |
Source code in cloudflare_images/api.py
post(img_id, img, *args, **kwargs)
Issue httpx POST request to upload image.
Source code in cloudflare_images/api.py
upsert(img_id, img)
Ensures a unique id name by first deleting the img_id
from storage and then
uploading the img
.
url(img_id, variant='public')
Generates url based on the Cloudflare hash of the account. The variant
is based on
how these are customized on Cloudflare Images. See also flexible variant docs
Examples:
>>> import os
>>> os.environ['CF_ACCT_ID'] = "ABC"
>>> os.environ['CF_IMG_HASH'], os.environ['CF_IMG_TOKEN'] = "DEF", "XYZ"
>>> cf = CloudflareImagesAPIv1()
>>> cf.url('sample-img', 'avatar')
'https://imagedelivery.net/DEF/sample-img/avatar'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img_id |
str
|
The uploaded ID |
required |
variant |
str
|
The variant created in the Cloudflare Images dashboard. Defaults to "public". |
'public'
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
URL to display the request |
Source code in cloudflare_images/api.py
Django
Bases: Storage
Custom Storage Class based on Django docs instructions
Starting with Django 4.2, add to STORAGES
setting:
Can then define a callable likeso:
from django.core.files.storage import storages
def select_storage(is_remote_env: bool):
return storages["cloudflare_images"] if is_remote_env else storages["default"]
class MyModel(models.Model):
my_img = models.ImageField(storage=select_storage)
Can also refer to it via:
from django.core.files.storage import storages
cf = storages["cloudflare_images"]
# assume previous upload done
id = <image-id-uploaded>
# get image url, defaults to 'public' variant
cf.url(id)
# specified 'avatar' variant, assuming it was created in the Cloudflare Images dashboard / API
cf.url_variant(id, 'avatar')
Source code in cloudflare_images/dj.py
Python | |
---|---|
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 |
|