| 1 | # R2 |
| 2 | |
| 3 | [R2](https://developers.cloudflare.com/r2/) is object storage. A Worker reaches |
| 4 | a bucket through an `r2_buckets` binding. celld stores the objects in the fleet |
| 5 | bucket that the node opened with `--bucket`, so R2 has the durability of that |
| 6 | object store. On a node without a fleet bucket, every R2 method fails. Read the |
| 7 | [Workers API reference](https://developers.cloudflare.com/r2/api/workers/workers-api-reference/) |
| 8 | for the API. |
| 9 | |
| 10 | ## Example |
| 11 | |
| 12 | The [R2 example](../../examples/r2) reads, writes, and deletes objects in an R2 |
| 13 | bucket. |
| 14 | |
| 15 | <!-- celld-example: r2 --> |
| 16 | |
| 17 | ## API |
| 18 | |
| 19 | - `get(key, { range, onlyIf })` returns an `R2ObjectBody` with a streamed body, |
| 20 | or `null`. A range is an `offset`, a `length`, a `suffix`, or a `Range` |
| 21 | header. |
| 22 | - `head(key)` returns the object record without the body. |
| 23 | - `put(key, value, { httpMetadata, customMetadata, onlyIf })` takes a string, |
| 24 | an `ArrayBuffer`, a typed array, a `Blob`, or a `ReadableStream`. A refused |
| 25 | condition returns `null`. |
| 26 | - `delete(keys)` removes one key or a list of keys. |
| 27 | - `list({ prefix, delimiter, cursor, startAfter })` returns a page of objects. |
| 28 | - `createMultipartUpload(key)` returns a handle. `uploadPart(number, value)` |
| 29 | sends one part, and `complete(parts)` assembles the object. |
| 30 | |
| 31 | ## Keys and storage |
| 32 | |
| 33 | Each binding has its own `bucket_name`, and celld stores key `<key>` as the |
| 34 | object `r2/<bucket_name>/<key>`. A key is 1 to 1024 bytes. A streamed `put()` |
| 35 | body that grows past 8 MiB becomes a multipart upload. `delete()` removes up to |
| 36 | 1000 keys in one call. celld has no public bucket URL, no presigned URL, and no |
| 37 | S3 endpoint, so put a Worker in front of the bytes to publish them. |
| 38 | |
| 39 | The object store keeps some keys in a percent-encoded form: a key with a |
| 40 | non-ASCII character, a control character, or one of |
| 41 | ``% \ { } ^ ` [ ] " < > ~ # | * ?``. For example, `přehled.html` becomes |
| 42 | `r2/<bucket_name>/p%C5%99ehled.html`. Each empty segment becomes `%`, so |
| 43 | `photos/` becomes `r2/<bucket_name>/photos/%`, and `a/b`, `/a/b`, `a//b`, and |
| 44 | `a/b/` are four different objects, as in Cloudflare R2. `list()` returns the |
| 45 | decoded key, but it sorts by the encoded form and compares `startAfter` with the |
| 46 | encoded form. Cloudflare R2 sorts by UTF-8 bytes. For example, Cloudflare R2 |
| 47 | returns `a~` after `startAfter: "aa"`, but celld compares `a%7E` with `aa` and |
| 48 | skips the key. A `cursor` from a celld listing does not have this problem. |
| 49 | |
| 50 | `list()` fails when it reaches an object that another tool wrote with a |
| 51 | percent-encoded key that is not UTF-8, such as `a%FF`. |
| 52 | |
| 53 | celld 0.5.1 and earlier removed each empty segment from a key. An object that |
| 54 | such a version wrote under `photos/` stays under `photos`, and `list()` returns |
| 55 | it as `photos`. |
| 56 | |
| 57 | ## Multipart uploads |
| 58 | |
| 59 | A multipart upload lives on the node that opened it. Read |
| 60 | [Multipart upload](https://developers.cloudflare.com/r2/objects/multipart-objects/) |
| 61 | for the part rules. A part that arrives before its predecessor waits in memory, |
| 62 | up to 256 MiB, and celld then refuses a further part. A waiting part can be |
| 63 | replaced. A part that already went to the object store cannot, and celld |
| 64 | refuses that write. |
| 65 | |
| 66 | ## Differences from Cloudflare |
| 67 | |
| 68 | - An R2 binding uses the fleet bucket under `r2/<bucket_name>/`. |
| 69 | - The five content headers are stored as headers. `customMetadata`, |
| 70 | `cacheExpiry`, the checksums, and the storage class are stored together as |
| 71 | one JSON value, under the `celld-r2` user-metadata name (`celld_r2` on |
| 72 | Azure Blob Storage, which refuses a hyphen in a metadata name). |
| 73 | - An object another tool wrote still reads through the binding. Its user |
| 74 | metadata becomes its `customMetadata`, and its headers become its |
| 75 | `httpMetadata`. Use `celld r2 put` to write the complete record. |
| 76 | - `celld r2 get|head|put|delete|list` replaces `wrangler r2 object`. The |
| 77 | command reads the fleet bucket directly, so it needs no running node. |
| 78 | - An object `version` equals its content ETag, so identical content produces |
| 79 | the same version. |
| 80 | - `ssecKey` and `jurisdiction` are unavailable. |
| 81 | - A conditional write cannot use a streamed body larger than 8 MiB. |
| 82 | - `createMultipartUpload()` does not accept a checksum. |
| 83 | - A multipart upload cannot resume on another node or after a restart. celld |
| 84 | also cannot replace a part that the object store already holds. |
| 85 | - Out-of-order parts can use at most 256 MiB of memory, and completion cannot |
| 86 | change the stored part order. |
| 87 | |
| 88 | The [Cloudflare compatibility](../cloudflare-compat.md#services) page lists the |
| 89 | runtime APIs and the unsupported services. |