Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 35 ยท Storage and Databases

Spaces: S3-Compatible Object Storage on DigitalOcean

Store files in DigitalOcean Spaces: buckets, access keys, endpoints, public and private objects, presigned URLs, CDN, CORS and lifecycle rules, with boto3 and rclone examples.

Spaces is DigitalOcean's object storage. Instead of a disk with folders, you get buckets that hold objects (files plus metadata) addressed by a key, reachable over HTTPS. Spaces implements the S3 API, so the huge ecosystem of S3 tools, SDKs and libraries works with it by changing only the endpoint. It is ideal for user uploads, images and videos, static assets, backups and build artifacts.

Object storage versus block storage

Spaces (object) Volume (block)
Access HTTP API, from anywhere Mounted filesystem on one Droplet
Sharing Many clients at once One Droplet at a time
Capacity Effectively unlimited, pay for what you store Fixed size you choose
Edit a file Replace the whole object Edit in place
Best for Uploads, media, backups, static sites Databases, app data needing a filesystem

Key concepts

  • Bucket (a "Space"): a named container in one region, for example my-shop-assets in fra1. Names are unique within the region's namespace.
  • Object key: the object's full name, such as images/2026/logo.png. There are no real folders; the slashes are just part of the key, and tools show them as folders using a prefix and delimiter.
  • Endpoint: https://<region>.digitaloceanspaces.com (for example https://fra1.digitaloceanspaces.com), or the virtual-host style https://my-shop-assets.fra1.digitaloceanspaces.com.
  • Access keys: a key and a secret generated under API then Spaces Keys. Keys can be limited to specific buckets and permissions. Like tokens, the secret is shown once.
  • Permissions: objects are private by default; you can make individual objects or a bucket's listing public.
  • CDN: you can enable a built-in content delivery network, which caches objects at edge locations and serves them from a faster *.cdn.digitaloceanspaces.com hostname (or your own domain).

Spaces is billed as a subscription that includes a base amount of storage and outbound transfer, with overage charged per GiB; check the pricing page for current numbers.

Using it from Python (boto3)

Python
import boto3

session = boto3.session.Session()
client = session.client(
    "s3",
    region_name="fra1",
    endpoint_url="https://fra1.digitaloceanspaces.com",
    aws_access_key_id="<SPACES_KEY>",          # read from the environment in real code
    aws_secret_access_key="<SPACES_SECRET>",
)

client.upload_file("logo.png", "my-shop-assets", "images/logo.png",
                   ExtraArgs={"ContentType": "image/png", "ACL": "private"})

for obj in client.list_objects_v2(Bucket="my-shop-assets", Prefix="images/").get("Contents", []):
    print(obj["Key"], obj["Size"])

url = client.generate_presigned_url(
    "get_object",
    Params={"Bucket": "my-shop-assets", "Key": "images/logo.png"},
    ExpiresIn=3600,
)
print(url)   # a temporary link, valid for one hour

This code needs credentials and network access, so it is read-only here. Never hard-code the key and secret: use environment variables or a secret manager.

Command-line options: s3cmd, the AWS CLI with --endpoint-url, or rclone, which is excellent for syncing:

Terminal
rclone sync ./backup do-spaces:my-shop-assets/backups --progress
aws s3 ls s3://my-shop-assets --endpoint-url https://fra1.digitaloceanspaces.com

Private files and presigned URLs

Do not make a bucket public just to let users download their own invoices. Keep objects private and hand out a presigned URL: a normal URL with a signature and an expiry time, generated by your server using the secret key. Anyone holding the link can fetch (or, for an upload URL, PUT) that one object until it expires. Browsers can upload straight to Spaces with a presigned PUT or POST, so large files never pass through your application server.

sequenceDiagram
  participant B as Browser
  participant A as Your app
  participant S as Spaces
  B->>A: I want to upload avatar.png
  A->>S: sign upload URL (local computation)
  A-->>B: presigned URL, expires in 5 min
  B->>S: PUT avatar.png (directly)
  S-->>B: 200 OK

Your app signs; the browser uploads. The secret key never leaves the server.

CORS, CDN and lifecycle rules

  • CORS: if a web page on https://shop.example.com loads fonts or calls Spaces from JavaScript, the bucket needs a CORS rule allowing that origin and the methods you use.
  • CDN: turn it on for public assets, then set Cache-Control metadata on uploads (for example public, max-age=31536000, immutable for fingerprinted files). Purge the CDN cache when you replace an object under the same key.
  • Lifecycle rules: automatically delete objects after N days, remove incomplete multipart uploads, or expire old versions. They keep temporary exports and logs from filling your bill.

Stand-in: prefixes, folders and lifecycle

Listing with a prefix and a delimiter is how "folders" appear. This stand-in implements the same logic on an in-memory list, plus a lifecycle rule evaluator. It uses no network.

Python Stand-in: list objects like S3 (prefix and delimiter)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Stand-in: which objects does a lifecycle rule expire?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Good practices

  • One bucket per purpose and environment (shop-prod-uploads, shop-staging-uploads), each with its own limited access key.
  • Never expose the access key to browsers; use presigned URLs.
  • Set correct Content-Type and cache headers at upload time.
  • Use versioning or separate backups for irreplaceable data; deleting an object is permanent without them.
  • Put the bucket in the region closest to your Droplets and users; enable the CDN for global public assets.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What protocol does Spaces implement so that S3 tools work with it?

    Point an S3 client at the Spaces endpoint and it generally works.
  2. 2. Are there real folders in a bucket?

    Object storage is flat; 'folders' are a display convention based on key prefixes and a delimiter.
  3. 3. How should a web app let a user download a private invoice?

    A presigned URL grants temporary access to one object without exposing credentials.
  4. 4. What would a CORS rule on a bucket be needed for?

    Browsers enforce the same-origin policy; CORS headers from the bucket permit specific origins.

Summary

  • Spaces is S3-compatible object storage: buckets, keys, endpoints per region, access keys, private-by-default objects, optional CDN.
  • Use SDKs such as boto3 (with endpoint_url), rclone or the AWS CLI; keep keys in a secret store.
  • Presigned URLs give temporary, scoped access; lifecycle rules and CORS keep the bucket tidy and usable from browsers.
  • Use Volumes for filesystems, Spaces for files served or shared at scale.

Next: Snapshots and Backups