Object Storage (S3/R2/GCS) — DevOps

Object storage (S3, R2, GCS, Spaces, MinIO) keeps files as objects inside buckets (storage containers), and everything is reached through an HTTP API. A key…

This morning notes-api fails to save new notes, because Postgres reports that the disk is full. What filled web-1's disk turns out not to be the database but the attachments folder: photos and PDFs that people uploaded over the months.

Object storage moves files like those attachments off the server. It keeps files as objects inside a bucket (a storage container). Each object has a unique name (its key), the file's contents and metadata (information about the file). Everything is reached through an HTTP API, not through folders on a server's disk. AWS S3 started it, and many providers now use the same API (S3-compatible): Cloudflare R2, DigitalOcean Spaces, Google Cloud Storage, or MinIO, which you can run yourself. Picture the place at a museum where you leave your bag. You hand over your bag, the attendant gives you a numbered ticket, and the bag can only be collected with that number. The storage room has no aisles or named cupboards, just shelves with numbers. In this picture, the storage room is the bucket, the bag is an object, and its number is the key. If a friend is going to collect your bag, the attendant can give a ticket stamped with the time and signed with the attendant's initials. The ticket is valid until that time. If the number is crossed out and replaced, the initials no longer match. That stamped ticket is a presigned URL, a temporary link to one object: the time stamp is its lifetime, and the initials are its signature.

Why attachments move off web-1's disk

Since the first lesson, notes-api's attachments have lived in /srv/notes-api/storage/attachments on web-1. That works, until these problems appear:

A bucket solves all four problems. Its capacity is practically unlimited (you pay per GB). Every server reads the same bucket. Files can be uploaded and downloaded directly without going through Node. The provider also keeps several copies of each object.

Bucket, key and object

This is notes-api's code that uploads one person's avatar to a bucket on S3:

import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";

const s3 = new S3Client({ region: "eu-west-2" }); // London; credentials from env

await s3.send(new PutObjectCommand({
  Bucket: "notes-attachments",
  Key: `avatars/${userId}.jpg`,
  Body: fileBuffer,
  ContentType: "image/jpeg",
}));

These are the contents of the notes-attachments bucket in the Lab:

Key Size Storage class
avatars/7.jpg 38,204 bytes STANDARD
attachments/a4c2e9f1.jpg 248,913 bytes STANDARD
attachments/7f3a9c1e.jpg 1,204,877 bytes STANDARD
backup/notes-2026-10-08.sql.gz 410,112 bytes STANDARD

Public or private

A new S3 bucket is private, and its Block Public Access setting is on by default. A request that isn't signed with the credentials gets 403 Forbidden with <Code>AccessDenied</Code>. Only the server holding the credentials can read and write.

Making part of a bucket public (anyone may GET) takes two deliberate steps: turn off Block Public Access, then write a bucket policy (the bucket's access rules) that allows s3:GetObject (reading objects). That suits files meant for everyone, like avatars on a public profile or frontend assets (the site's CSS, JavaScript and image files), usually served through a CDN. Attachments on private notes stay private.

Presigned URLs: tickets that expire

A private file can still be shared for a short while through a presigned URL. A presigned URL is a normal URL to that object, plus when it was made, how long it lasts, and a signature the server calculates from its credentials. Anyone holding the URL may GET that object until it expires.

This is notes-api's code that makes a presigned URL for one attachment, valid for 300 seconds:

import { GetObjectCommand } from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";

const url = await getSignedUrl(s3, new GetObjectCommand({
  Bucket: "notes-attachments",
  Key: "attachments/a4c2e9f1.jpg",
}), { expiresIn: 300 }); // 5 minutes

This is the resulting URL, part by part:

Part Contents What it means
host + path notes-attachments.s3.eu-west-2.amazonaws.com/attachments/a4c2e9f1.jpg the bucket and the key
X-Amz-Credential AKIA.../20261009/eu-west-2/s3/aws4_request whose key signed it
X-Amz-Date 20261009T030000Z made at 03:00:00 UTC
X-Amz-Expires 300 valid for 300 seconds
X-Amz-Signature 64 hex characters an HMAC-SHA256 of the method, the key and every parameter above

HMAC-SHA256, in the table's last row, is a formula that mixes the secret key (AWS_SECRET_ACCESS_KEY) with the method, the object's key and the URL's parameters. Without the secret key, nobody can make the same signature. S3 holds the same secret key, so S3 calculates the signature again from the URL that arrives, then compares it with X-Amz-Signature.

When the URL is opened, S3 also calculates its time limit: X-Amz-Date + X-Amz-Expires = 03:05:00 UTC. Opened at 03:06:00, the answer is 403 Forbidden with <Code>AccessDenied</Code><Message>Request has expired</Message>. Changing X-Amz-Expires to 3600 doesn't help. The signature was calculated from 300, so S3 answers <Code>SignatureDoesNotMatch</Code> (the signature doesn't match). Swapping the key in the URL for the key of someone else's attachment also ends in SignatureDoesNotMatch. In the Lab, the signature is calculated with the same SigV4 (Signature Version 4, AWS's way of signing requests), and you can try these three cases.

Uploading straight from the browser

For uploads, the server creates a presigned URL with PutObjectCommand, and the browser sends the file straight to the bucket:

  1. Browser: POST /attachments/presign with the file's name and size.
  2. notes-api checks the login, the size and the file type, picks a random key (attachments/9b1e4c77.mp4), then answers with a PUT presigned URL with expiresIn: 300.
  3. Browser: fetch(url, { method: "PUT", body: file }) straight to the bucket.
  4. The browser tells notes-api the key, and notes-api stores it in the attachments table.

The 500 MB video never passes through the API server, so the server isn't overwhelmed. The bucket needs a CORS rule (permission for requests from pages on another domain) that allows PUT from https://notes.example.com, because that page sends the file to the bucket's domain.

Lifecycle rules and storage classes

The storage class sets an object's price and access speed:

Storage class For Note
Standard files opened often: attachments, avatars low latency, the highest storage price
Infrequent Access rarely opened but must be quick when needed cheaper to store, a fee each time it is retrieved
Glacier / Archive long-term backups that are almost never opened the cheapest, retrieval is slow (minutes to hours)

A lifecycle rule moves or deletes objects automatically by their age. The daily backups from the Database DevOps lesson can be copied to the bucket with keys that start with backup/. Then the rules below manage two prefixes (the start of a key), tmp/ and backup/:

{
  "Rules": [
    { "ID": "delete-tmp", "Filter": { "Prefix": "tmp/" }, "Status": "Enabled",
      "Expiration": { "Days": 1 } },
    { "ID": "archive-backup", "Filter": { "Prefix": "backup/" }, "Status": "Enabled",
      "Transitions": [{ "Days": 30, "StorageClass": "GLACIER" }],
      "Expiration": { "Days": 365 } }
  ]
}

⚠️ Attachments that leak, or get overwritten

TL;DR: Object storage (S3, R2, GCS, Spaces, MinIO) keeps files as objects inside buckets (storage containers), and everything is reached through an HTTP API. A key is an object's unique name inside the bucket, written like a path (avatars/${userId}.jpg), not a real folder and not a secret key. Buckets are private by default (Block Public Access is on). Only assets meant for everyone are made public, usually through a CDN. A presigned URL gives temporary access to a private object: X-Amz-Date (when it was made, in UTC) + X-Amz-Expires (its lifetime) set its limit. The SigV4 signature in that URL means its key, method and lifetime can't be changed: past the limit, S3 answers Request has expired, and a changed URL gets SignatureDoesNotMatch. Big uploads use a presigned PUT with expiresIn: 300 (5 minutes), so the file goes straight to the bucket without passing through the server. The storage class sets the price and access speed: Standard for files opened often, Infrequent Access for files opened rarely, Glacier/Archive for long-term backups. Lifecycle rules move objects to another storage class and delete them automatically by their age.

⚡ Bucket, key and object

Write:

await s3.send(new PutObjectCommand({
  Bucket: "notes-attachments",
  Key: `avatars/${userId}.jpg`,
  Body: fileBuffer,
  ContentType: "image/jpeg",
}));
Guess first: this code uploads a file to a bucket (a storage container in object storage) with the key avatars/${userId}.jpg. Do you have to create an avatars/ folder before uploading?

No. avatars/ is just part of the object's name. A key is the object's unique name inside the bucket, written like a path, not a real folder and not a secret key.

Meaning: Object storage keeps each file as an object inside a bucket. This code uploads one object to the bucket notes-attachments on S3 (AWS's object storage service). An object = the file's contents (Body) + metadata (information about the file, such as ContentType). Uploading to the same key overwrites the old object.

⚡ A bucket is private

Meaning: By default, a bucket is private: Block Public Access (a setting that refuses public access) is on. A request that isn't signed with the credentials (the server's access keys) gets 403 Forbidden with <Code>AccessDenied</Code>. Only assets meant for everyone are made public, usually through a CDN.

⚡ Presigned URLs: tickets that expire

Write:

const url = await getSignedUrl(s3, new GetObjectCommand({
  Bucket: "notes-attachments",
  Key: "attachments/a4c2e9f1.jpg",
}), { expiresIn: 300 }); // 5 minutes
Guess first: a presigned URL is a temporary link to a private object. This one was made at 03:00 UTC, with a lifetime X-Amz-Expires of 300 seconds and a signature calculated from the URL's contents. You change X-Amz-Expires to 3600 to make it last longer. What does S3 answer?

SignatureDoesNotMatch (the signature doesn't match). The signature was calculated from 300, so changing the key, the method or the lifetime means the signature no longer matches.

Meaning: expiresIn: 300 in the code becomes X-Amz-Expires in the URL. X-Amz-Date (when the URL was made, in UTC) + X-Amz-Expires is the limit of its lifetime. If the URL is opened after that limit, S3 answers Request has expired.

⚡ Uploading straight to the bucket

Meaning: For uploads, the server only checks the login, the size and the file type, then gives the browser a presigned PUT URL with expiresIn: 300 (valid for 5 minutes). The browser sends the file straight to the bucket, so a 500 MB video never passes through the server. Use random keys, so two people's files don't overwrite each other.

What you will learn