|
| 1 | +# Cloudflare R2 |
| 2 | + |
| 3 | +[Cloudflare R2](https://www.cloudflare.com/developer-platform/products/r2/) is Cloudflare's object storage solution, designed to be compatible with the S3 API. Some developers may choose to use Cloudflare R2 because it has no egress fees. |
| 4 | + |
| 5 | +It's easy to read and write data to and from Cloudflare R2 with obstore's [`S3Store`][obstore.store.S3Store] with three steps: |
| 6 | + |
| 7 | +1. [Create an API token](https://dash.cloudflare.com/?to=/:account/r2/api-tokens) with read or read/write access to one or more buckets. |
| 8 | + |
| 9 | + Copy the `Access Key ID` and `Secret Access Key` to use in your code. |
| 10 | + |
| 11 | +  |
| 12 | + |
| 13 | +2. On the general settings of a bucket, take note of the `S3 API` URL. In my case it's `https://f0b62eebfbdde1133378bfe3958325f6.r2.cloudflarestorage.com/kylebarron-public`. |
| 14 | + |
| 15 | +  |
| 16 | + |
| 17 | +3. Pass this information to [`S3Store.from_url`][obstore.store.S3Store.from_url]: |
| 18 | + |
| 19 | + ```py |
| 20 | + from obstore.store import S3Store |
| 21 | + |
| 22 | + access_key_id = "..." |
| 23 | + secret_access_key = "..." |
| 24 | + store = S3Store.from_url( |
| 25 | + "https://f0b62eebfbdde1133378bfe3958325f6.r2.cloudflarestorage.com/ kylebarron-public", |
| 26 | + access_key_id=access_key_id, |
| 27 | + secret_access_key=secret_access_key, |
| 28 | + ) |
| 29 | + store.list_with_delimiter() |
| 30 | + ``` |
| 31 | + |
| 32 | + Or you can construct a store manually with the `endpoint` and `bucket` parameters: |
| 33 | + |
| 34 | + ```py |
| 35 | + from obstore.store import S3Store |
| 36 | + |
| 37 | + access_key_id = "..." |
| 38 | + secret_access_key = "..." |
| 39 | + bucket = "kylebarron-public" |
| 40 | + endpoint = "https://f0b62eebfbdde1133378bfe3958325f6.r2.cloudflarestorage. com" |
| 41 | + store = S3Store( |
| 42 | + bucket, |
| 43 | + access_key_id=access_key_id, |
| 44 | + secret_access_key=secret_access_key, |
| 45 | + endpoint=endpoint, |
| 46 | + ) |
| 47 | + store.list_with_delimiter() |
| 48 | + ``` |
0 commit comments