Crate axum_static_s3

Source
Expand description

A simple static file service for AWS S3 using Axum.

This will retrieve the files from S3 and serve them as responses using a tower Service. This is useful for serving static files from S3 in a serverless environment, when the static files are independent of the application.

This provides local fallback for development (local axum invocation) as well.

§Basic Usage

use axum::{Router, routing::get};
use axum_static_s3::S3OriginBuilder;
 
 
#[tokio::main]
async fn main() {
    // Build the S3 origin
    let s3_origin = S3OriginBuilder::new()
        .bucket("my-static-files-bucket")
        .prefix("static/")
        .prune_path(1)      // Remove the first request path component ()
        .max_size(1024 * 1024 * 12) // 12MB
        .build()
        .expect("Failed to build S3 origin");
 
    // Create the router with the S3 static file handler
    let app = Router::new()
        .nest_service("/static/", s3_origin);
 
    // Start the server
    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000")
        .await
        .unwrap();
    axum::serve(listener, app).await.unwrap();
}

§Features

  • trace: Enable tracing of the S3 requests.

Structs§

S3Origin
S3OriginBuilder