Store Data With Object Storage
Validated on 31 May 2023 · Last edited on 10 Oct 2023
Spaces is S3-compatible object storage: somewhere to put files that are not code, such as user uploads, images, backups and build artifacts. Because the API is S3-compatible, most tools and libraries written for S3 work against a Space by changing the endpoint.
A bucket is the container your files live in. You create one, generate a pair of keys to reach it with, and then read and write to it from the control panel, a client such as s3cmd, or your application.
Create a Bucket
Open the control panel, click Create and choose Spaces Object Storage.
- Datacenter region. Pick the region closest to whoever reads the files most. This cannot be changed later.
- Storage type. Standard storage suits frequently accessed files and supports every feature. Cold storage costs less for archives you rarely read. This choice is also permanent.
- CDN. Optionally enable the built-in CDN, which caches your files at edge locations. CDN integration is available for standard storage buckets only.
- Name. Between 3 and 63 characters, lowercase letters, numbers and dashes only, starting with a letter or a number.
- Project. Assign the bucket to a project, then click Create a Spaces Bucket.
Create Access Keys
Anything reaching the bucket programmatically needs a key pair. In the control panel, open Spaces Object Storage, go to the Access Keys tab and click Create Access Key. Choose whether the key has full access or is limited to particular buckets, give it a name, and create it.
Upload and Manage Files
The control panel has a file browser for dragging files in and setting each one public or private, which is enough to get started. For anything repeatable, use a client.
s3cmd is a command-line client for S3-compatible storage. Point it at DigitalOcean rather than AWS when it asks for an endpoint.
- Run the configuration wizard and give it your access key, your secret key, and the endpoint for your bucket’s region, in the form
<region>.digitaloceanspaces.com.s3cmd --configure - List the buckets on the account to check it worked.
s3cmd ls - Upload a file. Add
--acl-publicif the file should be readable by anyone with the URL.s3cmd put example.png s3://your-bucket-name/ --acl-public
Two things to know about automating this. A bucket can be created with s3cmd mb s3://your-bucket-name or by sending a PUT request to the S3-compatible endpoint. Cold storage buckets are the exception: they cannot be created through the API or the CLI, only in the control panel.
Once files are in the bucket you can:
- Serve them straight from the bucket URL, or through the CDN if you enabled it.
- Put a custom domain and certificate in front of the bucket so URLs use your own hostname.
- Keep private files private and hand out time-limited presigned URLs instead of making them public.
Spaces is durable, but it is still one copy of your data in one place. For anything you cannot lose, keep a backup outside Spaces as well.