Skip to main content

Module storage

Module storage 

Source
Available on crate features storage-azure or storage-gcs or storage-local or storage-s3 only.
Expand description

File / object storage abstraction.

FormMultipartData::parse() hands back raw bytes with no place to put them. This module gives handlers a single Storage trait so the same upload code works against local disk in development and an S3-compatible bucket (AWS S3, Cloudflare R2, MinIO), Azure Blob Storage or Google Cloud Storage in production.

§Local disk (requires the storage-local feature)

use rust_web_server::storage::{LocalStorage, Storage};

let dir = std::env::temp_dir().join("rws-storage-doctest");
let store = LocalStorage::new(dir.to_str().unwrap()).with_base_url("/uploads");

let key = store.put("avatars/42.png", b"...png bytes...", "image/png")?;
assert_eq!("/uploads/avatars/42.png", store.url(&key));

let bytes = store.get(&key)?;
store.delete(&key)?;

§S3-compatible object storage (requires the storage-s3 feature)

use rust_web_server::storage::{S3Storage, Storage};

// Reads RWS_S3_BUCKET, RWS_S3_REGION, RWS_S3_ACCESS_KEY, RWS_S3_SECRET_KEY,
// and optionally RWS_S3_ENDPOINT (for R2 / MinIO / any S3-compatible host).
let store = S3Storage::from_env()?;

store.put("avatars/42.png", b"...png bytes...", "image/png")?;
let bytes = store.get("avatars/42.png")?;
store.delete("avatars/42.png")?;

§Azure Blob Storage (requires the storage-azure feature)

use rust_web_server::storage::{AzureBlobStorage, Storage};

// Reads RWS_AZURE_ACCOUNT, RWS_AZURE_CONTAINER, and optionally
// RWS_AZURE_ACCOUNT_KEY (falls back to Managed Identity when unset) and
// RWS_AZURE_ENDPOINT (for Azurite / a private endpoint).
let store = AzureBlobStorage::from_env()?;

store.put("avatars/42.png", b"...png bytes...", "image/png")?;
let bytes = store.get("avatars/42.png")?;
store.delete("avatars/42.png")?;

Structs§

AzureBlobConfig
Connection details for an Azure Blob Storage container.
AzureBlobStorage
Azure Blob Storage object storage.
GcsConfig
Connection details for a Google Cloud Storage bucket.
GcsStorage
Google Cloud Storage, through its JSON API and the outbound HTTP client (crate::http_client::Client) — no Google SDK.
LocalStorage
Stores objects as files under a root directory on local disk.
S3Config
Connection details for an S3-compatible bucket.
S3Storage
S3-compatible object storage (AWS S3, Cloudflare R2, MinIO, …).
StorageError
Error returned by all Storage operations.

Traits§

Storage
Backend-independent object storage.