Skip to main content

head

Function head 

Source
pub fn head(
    ctx: &Ctx,
    key: &str,
) -> impl Future<Output = Result<Option<StoredObject>>> + Send + use<>
Expand description

Describes the object at key without reading it: size, content type, ETag, upload time; None when it does not exist.

Active Storage’s blob.byte_size/service.exist? in one call. Direct uploads are checked with it (attach_direct_upload), and purges read the upload time.

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

§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};

// GET /files/{*key}/size
async fn size(State(ctx): State<Ctx>, Path(key): Path<String>) -> Result<String> {
    let object = storage::head(&ctx, &key).await?.or_404()?;
    Ok(format!("{} ({}), uploaded at {}", storage::human_size(object.size), object.content_type, object.uploaded_at))
}