Skip to main content

read_first

Function read_first 

Source
pub fn read_first(
    ctx: &Ctx,
    key: &str,
    length: u64,
) -> impl Future<Output = Result<Option<Vec<u8>>>> + Send + use<>
Expand description

Reads the first length bytes of an object (all of it when shorter), or None when key does not exist.

For analyze on a file uploaded directly: the signature and image size are in the first few KB (use 256 KB for JPEGs with large EXIF blocks), so the Worker never holds the whole file.

Free plan: one R2 class B operation (10M free per month).

§Errors

Error::Internal (500) when the STORAGE binding is missing or R2 fails.

§Examples

use ocre::{Ctx, Result, storage};

async fn dimensions(ctx: &Ctx, key: &str) -> Result<Option<(u32, u32)>> {
    let Some(start) = storage::read_first(ctx, key, 64 * 1024).await? else { return Ok(None) };
    let analysis = storage::analyze(&start);
    Ok(analysis.width.zip(analysis.height))
}