Skip to main content
An application’s filesystem on Sevalla resets on every deployment, so files that users upload belong in a Sevalla object storage bucket. FastAPI talks to the bucket through its S3-compatible API with boto3, the AWS SDK for Python.

Before you begin

Create a 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. Keep it private: the code in this guide shares each file through a presigned URL that expires.

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 selected, and click Add environment variables.
The variables apply with the next deployment. Add them before you deploy the code: the application doesn’t start without them.

Install boto3

Install boto3 and update requirements.txt:

Add the S3 client

Create the client from environment variables. Cloudflare R2, which runs Sevalla’s object storage, uses the region auto:
storefront/storage.py

Upload and list files

Add two routes to storefront/main.py. Uploads use FastAPI’s UploadFile, which fastapi[standard] supports. The routes are plain def functions, so FastAPI runs boto3’s blocking calls in a thread pool:
storefront/main.py
Each URL works for 1 hour. list_objects_v2 returns up to 1,000 objects per call; pass ContinuationToken for more. See presigned URLs. For a public bucket, link files on its public domain instead; see public URLs. Commit and push the changes. If Auto-deploy is off, click Deploy now on the application’s Overview tab.

Verify

Upload a file and list the bucket, with your application’s domain:
The first response is {"filename":"hello.txt"}. The second lists hello.txt with a url: open it, and the page reads Hello from Sevalla. In the sidebar, click Object storage, click the bucket, and the Objects tab lists hello.txt. See Manage objects.