Available on crate features
storage-azure or storage-gcs or storage-local or storage-s3 only.Expand description
Files on models (18.35.0, #94): an Attachment is a model field
whose bytes live in a Storage backend (local disk, S3, Azure, GCS)
and whose description — key, file name, content type, size — lives in
one TEXT column.
use rust_web_server::attachment::Attachment;
use rust_web_server::storage::{LocalStorage, Storage};
let storage = LocalStorage::new(dir.to_str().unwrap()).with_base_url("/uploads");
let avatar = Attachment::store(&storage, "avatars", "me.png", "image/png", b"\x89PNG...").unwrap();
assert!(avatar.key.starts_with("avatars/"));
assert_eq!(b"\x89PNG...".to_vec(), avatar.read(&storage).unwrap());
// In a model: `pub avatar: Option<Attachment>` — stored as text.
let column = avatar.encode();
assert_eq!(Some(avatar.clone()), Attachment::decode(&column));
avatar.delete(&storage).unwrap();Keys are <prefix>/<id>/<file name> (id: 32 hex digits, time first) (the name with anything but
letters, digits, ., - and _ replaced by _), so uploads never
overwrite each other. Deleting a row doesn’t delete its file: call
Attachment::delete where you delete the row.
The column holds key=…&name=…&type=…&size=…, percent-encoded. In a
#[derive(Model)] struct the field is Attachment or
Option<Attachment> (model-* features), with a TEXT column.
Structs§
- Attachment
- A stored file’s description. See the module docs.