Add Object Storage
Create a Tigris Storage Buckets
fly storage create
? Select Organization: fly-ephemeral (fly-ephemeral)
? Choose a name, use the default, or leave blank to generate one:
Your project (summer-grass-2004) is ready.
Set one or more of the following secrets on your target app.
BUCKET_NAME: summer-grass-2004
AWS_ENDPOINT_URL_S3: https://fly.storage.tigris.dev
AWS_ACCESS_KEY_ID: tid_xxxxxx
AWS_SECRET_ACCESS_KEY: tsec_xxxxxx
Public buckets
By default, buckets are private. If you need to serve public assets like images or JavaScript files, create a public bucket:
fly storage create --public
You can also make a public bucket private.
fly storage update mybucket --private
Currently, buckets must be public or private. ACL settings will be ignored by the Tigris API.
To verify that everything has gone well, we can check whether the appropriate secrets have been set for our app:
fly secrets list
Connecting to the Bucket
The de-facto library for interacting with s3 storage is boto3, let’s add it to the project:
poetry add boto3
Now we can initialize the client:
import boto3
S3_URL = os.getenv("AWS_ENDPOINT_URL_S3")
svc = boto3.client('s3', endpoint_url=S3_URL)
The AWS credentials will be automatically extracted from the environment.
Let’s plug that into our app:
@app.get("/")
async def read_root():
buckets = svc.list_buckets()
return {"buckets": [bucket["Name"] for bucket in buckets["Buckets"]]}
At this point you can interact with the bucket through the boto3
interface; refer to the docs for all the possibilities.
When you re-deploy your app you should see a list of the buckets you have access to:
fly deploy
Take a look at the gist of this setup to get a full picture.