pub fn read(
ctx: &Ctx,
key: &str,
) -> impl Future<Output = Result<Option<Vec<u8>>>> + Send + use<>Expand description
Reads a whole object into memory, or returns None when key does not exist.
Meant for files the Worker itself processes (parsing an uploaded CSV,
attaching a file to an email). To send a file to a browser use
serve, which streams it without copying it
into WebAssembly and handles 304 and Range.
Free plan: one R2 class B operation (10M free per month); the bytes are copied into WebAssembly memory (a Worker has 128 MB).
§Errors
Error::Internal (500) when the STORAGE
binding is missing (the message shows the STORAGE entry to add
to cloudflare.config.ts) or R2 fails.
§Examples
use axum::extract::{Path, State};
use ocre::{Ctx, OptionExt, Result, storage};
// Counts the lines of an uploaded CSV.
async fn line_count(State(ctx): State<Ctx>, Path(key): Path<String>) -> Result<String> {
let bytes = storage::read(&ctx, &key).await?.or_404()?;
Ok(bytes.split(|&b| b == b'\n').filter(|line| !line.is_empty()).count().to_string())
}