Skip to main content

read_range

Function read_range 

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

Reads length bytes of an object from offset (fewer at its end), or returns None when key does not exist.

For files read piece by piece without holding them whole: a video’s index at its end, a ZIP’s central directory, the header of a large file. read_first reads from the start.

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

§Errors

Error::Internal (500) when the STORAGE binding is missing, or R2 fails (an offset past the end of the object is a failure of R2’s).

§Examples

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

// The last 64 KB of a file (a ZIP's central directory).
async fn tail(ctx: &Ctx, key: &str) -> Result<Option<Vec<u8>>> {
    let Some(object) = storage::head(ctx, key).await? else { return Ok(None) };
    let length = object.size.min(64 * 1024);
    storage::read_range(ctx, key, object.size - length, length).await
}