Every image on this blog is uploaded through the admin editor or the yw CLI, stored in a private Cloud Storage
bucket, and served by the same Rust binary that renders the page. No public bucket, no CDN configuration, no Google
SDK. Here is how that fits together.
A trait with two backends#
The rest of the app never talks to Cloud Storage. It talks to a FileStore: bytes in, bytes out, by path.
1/// Bytes by path. Paths are always built by the app2/// (`{date}/{uuid}/file.{ext}`), never taken from a request.3pub trait FileStore: std::fmt::Debug + Send + Sync {4 fn put<'a>(&'a self, path: &'a str, bytes: Vec<u8>, content_type: &'a str)5 -> StoreFuture<'a, ()>;6 fn get<'a>(&'a self, path: &'a str) -> StoreFuture<'a, Vec<u8>>;7}
There are two implementations. LocalStore writes to a folder (atomically, through a temporary file and a rename)
and is what tests and local runs use. GcsStore is used on staging and production. One environment variable,
YW_FILES_BACKEND=local|gcs, picks between them at start-up, and a missing bucket or key file stops the app before
it serves a single request.
Cloud Storage is just HTTP#
The official client libraries are large, and I need two calls. The JSON API covers both: a media upload and a
download with alt=media. The only real work is authentication, and the gcp_auth crate does that from a
service-account key file:
1let account = gcp_auth::CustomServiceAccount::from_file(credentials)?;2let token = account.token(&[GCS_SCOPE]).await?;34http.post(format!("https://storage.googleapis.com/upload/storage/v1/b/{bucket}/o"))5 .query(&[("uploadType", "media"), ("name", path)])6 .bearer_auth(token.as_str())7 .header(CONTENT_TYPE, content_type)8 .body(bytes)9 .send()10 .await?;
gcp_auth caches the token and refreshes it before it expires, so each call is one request. The service account
can only read and write objects in that one bucket, and its key sits on the server with 600 permissions.
On download the object name is part of the URL path, so 2026-11-20/…/file.png must become
2026-11-20%2F…%2Ffile.png. Forget it and Cloud Storage answers 404 for files that are plainly there.
Never trust the upload#
Before anything is stored, the bytes are checked – not the file name, and not the request's content type:
- not empty, and at most 10 MB;
- the type is read from the bytes and must be PNG, JPEG, GIF or WebP;
- width and height must be readable and non-zero.
The SHA-256 of the bytes is recorded too. Uploading the same image twice returns the file that already exists instead of storing a second copy.
Serving: private bucket, public pages#
Images are served at /files/{id}.{ext} on the blog's own host. The handler finds the file record, and answers only
if a published post uses that file; anything else is the same plain 404 as a missing page. So an image in a
draft stays private even if someone guesses its id.
A published file never changes – a new image is a new id – so the response is cached for good:
1Content-Type: image/png2Cache-Control: public, max-age=31536000, immutable3X-Content-Type-Options: nosniff
A draft opened through a signed preview link is the exception: there the same route answers with
Cache-Control: no-store, so nothing private lands in a shared cache.
Was it worth skipping the SDK?#
For two calls, yes. The whole backend is about a hundred lines that I can read in one sitting, the dependency tree stayed small, and because the rest of the app only knows the trait, every test runs against a temporary folder with no network at all.
← All posts · Yaroslav Zhurbilo