Skip to main content

Storage

Trait Storage 

Source
pub trait Storage: Send + Sync {
    // Required methods
    fn put(
        &self,
        key: &str,
        data: &[u8],
        content_type: &str,
    ) -> Result<String, StorageError>;
    fn get(&self, key: &str) -> Result<Vec<u8>, StorageError>;
    fn delete(&self, key: &str) -> Result<(), StorageError>;
    fn url(&self, key: &str) -> String;

    // Provided method
    fn put_stream(
        &self,
        key: &str,
        reader: &mut dyn Read,
        content_type: &str,
    ) -> Result<String, StorageError> { ... }
}
Available on crate features storage-azure or storage-gcs or storage-local or storage-s3 only.
Expand description

Backend-independent object storage.

Implement this trait to plug a custom backend (a database-backed store, …) into the same handler code that already works against LocalStorage, S3Storage, AzureBlobStorage and GcsStorage.

Required Methods§

Source

fn put( &self, key: &str, data: &[u8], content_type: &str, ) -> Result<String, StorageError>

Store data under key, overwriting any existing object at that key. Returns the key that was stored (backends that normalize or namespace keys may return a different string than the one passed in).

Source

fn get(&self, key: &str) -> Result<Vec<u8>, StorageError>

Retrieve the bytes stored under key.

Source

fn delete(&self, key: &str) -> Result<(), StorageError>

Delete the object stored under key. Backends generally treat deleting a missing key as a no-op success, matching S3’s semantics.

Source

fn url(&self, key: &str) -> String

A URL for retrieving key. For LocalStorage this is only meaningful if a base URL was configured (e.g. because the directory is also served as static files); for S3Storage it’s the object’s HTTPS URL. This performs no I/O and never fails.

Provided Methods§

Source

fn put_stream( &self, key: &str, reader: &mut dyn Read, content_type: &str, ) -> Result<String, StorageError>

Store everything reader yields under key — for a body too large to hold in memory, e.g. a streamed upload (Request::body read as a stream, #31). LocalStorage writes as it reads and S3Storage uploads in parts (S3 multipart upload), so memory stays at one part; the default reads everything first and calls Storage::put.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§