| 1 | # R2 |
| 2 | |
| 3 | [R2](https://developers.cloudflare.com/r2/) is object storage. A bucket holds |
| 4 | unstructured blobs of any size, and an application reads or writes a blob by |
| 5 | name. An application uses R2 for a file that a request handler must keep: an |
| 6 | upload, an image, a backup, a build artifact, or a large data export. A Worker |
| 7 | reaches a bucket through an `r2_buckets` binding, so `env.BUCKET` gives the |
| 8 | bucket API without a credential in the Worker code. The objects live outside the |
| 9 | Worker isolate, in the fleet object store that the node already uses. Read the |
| 10 | [Workers API reference](https://developers.cloudflare.com/r2/api/workers/workers-api-reference/) |
| 11 | for the full method list. |
| 12 | |
| 13 | ## Example |
| 14 | |
| 15 | The [R2 example](../../examples/r2) reads, writes, and deletes objects in an R2 |
| 16 | bucket. |
| 17 | |
| 18 | <!-- celld-example: r2 --> |
| 19 | |
| 20 | ## The object model |
| 21 | |
| 22 | A bucket contains objects, and each object has a key. A key is a string of 1 to |
| 23 | 1024 bytes. The namespace is flat, so a slash in a key is only a character, and |
| 24 | `list()` folds a prefix into a group when a caller passes a `delimiter`. See |
| 25 | [Objects](https://developers.cloudflare.com/r2/objects/) for the upstream model. |
| 26 | |
| 27 | An object carries a body and a record. The record holds `httpMetadata` (the |
| 28 | content headers and a cache expiry), `customMetadata` (the application's own |
| 29 | string map), `checksums`, a `storageClass`, a `size`, an `uploaded` time, an |
| 30 | `etag`, and a `version`. celld stores the body as a plain object in the fleet |
| 31 | bucket and writes the five content headers as real headers, so another tool can |
| 32 | read the object. The parts that an object store has no place for travel as one |
| 33 | JSON value in a reserved metadata entry, and an object without that entry still |
| 34 | reads correctly. |
| 35 | |
| 36 | celld takes the `version` from the object store. Most object stores report no |
| 37 | version identifier, so the ETag becomes the version. Two writes of identical |
| 38 | bytes therefore produce the same version, and an application must not use a |
| 39 | version to count writes. |
| 40 | |
| 41 | ## Where celld stores an object |
| 42 | |
| 43 | celld does not run its own blob service, because celld already runs on one. An |
| 44 | R2 binding therefore uses the fleet bucket that the node opened with `--bucket`, |
| 45 | and it does not use a second store or a second credential. The durability of an |
| 46 | R2 binding is the durability of that object store, and it is not the durability |
| 47 | of Cloudflare R2. A node that starts without a fleet bucket has nowhere to put a |
| 48 | blob, so every R2 method on such a node fails with an explicit error. |
| 49 | |
| 50 | Each binding is a logical bucket with its own `bucket_name`, and celld gives |
| 51 | each one a reserved key space in the single fleet bucket. A call to `get()`, |
| 52 | `put()`, `head()`, or `delete()` turns the key into `r2/<bucket_name>/<key>` |
| 53 | before the call reaches the store. The `bucket_name` comes from the deployment |
| 54 | manifest, which validates it, so two bindings cannot collide and no binding can |
| 55 | read or overwrite the fleet's own keys. |
| 56 | |
| 57 | The object store keeps some keys in a percent-encoded form. This applies to a |
| 58 | key with a non-ASCII character, a control character, or one of the characters |
| 59 | ``% \ { } ^ ` [ ] " < > ~ # | * ?``. For example, `přehled.html` becomes the |
| 60 | object `r2/<bucket_name>/p%C5%99ehled.html`. The store also keeps each empty |
| 61 | segment of a key as `%`, so `photos/` becomes the object |
| 62 | `r2/<bucket_name>/photos/%`. Therefore, the keys `a/b`, `/a/b`, `a//b`, and |
| 63 | `a/b/` are four different objects, as in Cloudflare R2. `list()` decodes each |
| 64 | key, so a listed key is the same key that `put()` received. However, `list()` |
| 65 | sorts the keys by the encoded form, and Cloudflare R2 sorts them by UTF-8 |
| 66 | bytes. Therefore, a key with one of these characters can have a different |
| 67 | position in a celld listing. `list()` also compares the `startAfter` option |
| 68 | with the encoded form, so a celld listing can return a key that Cloudflare R2 |
| 69 | skips, or skip a key that Cloudflare R2 returns. For example, Cloudflare R2 |
| 70 | returns the key `a~` after `startAfter: "aa"`, because `~` sorts after `a`. |
| 71 | celld compares `a%7E` with `aa` and skips the key. A `cursor` from a celld |
| 72 | listing does not have this problem, because celld made it. |
| 73 | |
| 74 | A tool other than celld can write an object under `r2/<bucket_name>/` with a |
| 75 | percent-encoded key that is not UTF-8, such as `a%FF`. No R2 key can name this |
| 76 | object, so `list()` fails when it reaches the object. |
| 77 | |
| 78 | celld 0.5.1 and earlier removed each empty segment from a key. An object that |
| 79 | such a version wrote under a key like `photos/` stays under `photos`, and |
| 80 | `list()` returns it as `photos`. |
| 81 | |
| 82 |  |
| 83 | |
| 84 | `get(key)` returns an `R2ObjectBody`, or `null` when the key is absent. The body |
| 85 | arrives as a `ReadableStream` from the host, so a large object never has to fit |
| 86 | in the isolate heap. `get()` also accepts a range, and celld accepts every range |
| 87 | spelling that R2 accepts: an `offset`, a `length`, a `suffix`, or a `Range` |
| 88 | header. `put(key, value, options)` accepts a string, an `ArrayBuffer`, a typed |
| 89 | array, a `Blob`, or a `ReadableStream`. celld writes a small body in one |
| 90 | request, and a streamed body that grows past 8 MiB becomes a multipart upload to |
| 91 | the object store. `delete(keys)` removes up to 1000 keys in one call, and |
| 92 | `head(key)` returns the record without the body. |
| 93 | |
| 94 | celld serves a bucket through the binding only. There is no public bucket URL, |
| 95 | no presigned URL, and no S3 endpoint into an R2 binding, so an application must |
| 96 | put a Worker in front of the bytes it wants to publish. |
| 97 | |
| 98 | ## Conditional requests and multipart uploads |
| 99 | |
| 100 | A conditional operation takes an `onlyIf`, either as an `R2Conditional` or as a |
| 101 | `Headers` object with `If-Match`, `If-None-Match`, `If-Modified-Since`, or |
| 102 | `If-Unmodified-Since`. A refused `get()` answers the record with no body, and a |
| 103 | refused `put()` answers `null`. celld passes the precondition to the object |
| 104 | store, which applies it. A streamed body larger than 8 MiB becomes a multipart |
| 105 | upload, and a multipart completion takes no precondition, so celld refuses a |
| 106 | conditional write of such a body instead of dropping the condition. |
| 107 | |
| 108 | `createMultipartUpload(key)` returns a handle, `uploadPart(number, value)` sends |
| 109 | one part, and `complete(parts)` assembles the object. Read |
| 110 | [Multipart upload](https://developers.cloudflare.com/r2/objects/multipart-objects/) |
| 111 | for the part-size and part-number rules. celld holds the upload in the node that |
| 112 | opened it, because the handle that the object store returns cannot be derived |
| 113 | again from an upload id. An upload therefore cannot resume on another node or |
| 114 | after a restart. |
| 115 | |
| 116 | celld hands the parts to the object store in ascending order, because the store |
| 117 | appends them. A part that arrives before its predecessor waits in memory, and |
| 118 | that backlog can reach 256 MiB before celld refuses a further part. A part that |
| 119 | is still waiting can be replaced by a second upload of the same number. A part |
| 120 | that already went to the store cannot be replaced, so celld refuses that write |
| 121 | rather than losing the bytes without a report. |
| 122 | |
| 123 | ## Differences from Cloudflare |
| 124 | |
| 125 | - An R2 binding uses the fleet bucket under `r2/<bucket_name>/`. |
| 126 | - The five content headers are stored as headers. `customMetadata`, |
| 127 | `cacheExpiry`, the checksums, and the storage class are stored together as |
| 128 | one JSON value, under the `celld-r2` user-metadata name (`celld_r2` on |
| 129 | Azure Blob Storage, which refuses a hyphen in a metadata name). |
| 130 | - An object another tool wrote still reads through the binding. Its user |
| 131 | metadata becomes its `customMetadata`, and its headers become its |
| 132 | `httpMetadata`. Use `celld r2 put` to write the complete record. |
| 133 | - `celld r2 get|head|put|delete|list` replaces `wrangler r2 object`. The |
| 134 | command reads the fleet bucket directly, so it needs no running node. |
| 135 | - An object `version` equals its content ETag, so identical content produces |
| 136 | the same version. |
| 137 | - `ssecKey` and `jurisdiction` are unavailable. |
| 138 | - A conditional write cannot use a streamed body larger than 8 MiB. |
| 139 | - `createMultipartUpload()` does not accept a checksum. |
| 140 | - A multipart upload cannot resume on another node or after a restart. celld |
| 141 | also cannot replace a part that the object store already holds. |
| 142 | - Out-of-order parts can use at most 256 MiB of memory, and completion cannot |
| 143 | change the stored part order. |
| 144 | |
| 145 | The [Cloudflare compatibility](../cloudflare-compat.md#services) page lists the |
| 146 | runtime APIs and the unsupported services. |