Skip to main content
Django saves uploaded files (media) to the local filesystem by default, and Sevalla resets an application’s filesystem on every deployment. With django-storages, Django saves them in a Sevalla object storage bucket instead, through its S3-compatible API, so every instance and every deployment sees the same files.

Before you begin

Create a bucket

1

Create the bucket

In the sidebar, click Object storage, then click Create object storage. Enter storefront-media as the Name, choose the Location closest to the application, and click Create object storage.
2

Decide who can read the files

Keep the bucket private to serve each file through a link that expires. To serve files to anyone with a permanent URL, click the bucket’s Settings tab, click Enable public access under Public access, then click Enable public access in the dialog.

Add the bucket details to the application

The values come from the bucket’s Settings tab. See Connect with the S3 API for each one.
A bucket's Settings tab with the display name, location, bucket name, ID and endpoint, and the access key and secret keyA bucket's Settings tab with the display name, location, bucket name, ID and endpoint, and the access key and secret key
1

Open the environment variables

In the sidebar, click Applications, then click the application. Click the Environment variables tab.
2

Add the variables

Click Add environment variables. Add the variables, keep Available during runtime and Available during buildtime selected, and click Add environment variables.
The build needs the variables too, because collectstatic loads the settings. They apply with the next deployment, so add them before you deploy the new settings.

Install django-storages

Install the package with its S3 extra, which adds boto3, and update requirements.txt:

Configure the storage

In storefront/settings.py, point the default storage at the bucket. Keep the staticfiles storage from the deploy guide:
storefront/settings.py
Every FileField and ImageField now saves to the bucket, and file.url returns a link that works for 1 hour (django-storages’ default querystring_expire is 3600 seconds). See presigned URLs. For a public bucket, return permanent URLs on the bucket’s public domain instead. Add the S3_PUBLIC_DOMAIN variable to the application, then add two options:
storefront/settings.py
See public URLs. Commit and push the changes. If Auto-deploy is off, click Deploy now on the application’s Overview tab.

Verify

Click the application’s Web terminal tab, choose the Web process, and save a test file through Django’s storage:
The command prints the file’s URL. Open it: the page reads Hello from Sevalla. In the sidebar, click Object storage, click the bucket, and the Objects tab lists hello.txt. To remove it, run default_storage.delete('hello.txt') the same way, or delete it on the Objects tab. See Manage objects.

Serve static files from the bucket

WhiteNoise with the Sevalla CDN serves static files well for most projects. To serve them from the bucket instead, make the bucket public and give media and static files their own folders with location:
storefront/settings.py
Then remove whitenoise.middleware.WhiteNoiseMiddleware from MIDDLEWARE. The build’s collectstatic now uploads the static files to the bucket, so builds take longer, and every variable must stay Available during buildtime. Media files are public too, because public access applies to the whole bucket.