-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathadd-object-storage.html.markerb
More file actions
56 lines (39 loc) · 1.44 KB
/
add-object-storage.html.markerb
File metadata and controls
56 lines (39 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
---
title: "Add Object Storage"
layout: framework_docs
objective: How to setup S3-compatible object storage and interact with it.
order: 1
---
## Create a Tigris Storage Buckets
<%= partial "/docs/partials/tigris/tigris-storage-create" %>
To verify that everything has gone well, we can check whether the appropriate secrets have been set for our app:
```cmd
fly secrets list
```
## Connecting to the Bucket
The de-facto library for interacting with s3 storage is [boto3](https://pypi.org/project/boto3/+external), let's add it to the project:
```cmd
uv add boto3
```
Now we can initialize the client:
```python
import boto3
S3_URL = os.getenv("AWS_ENDPOINT_URL_S3")
svc = boto3.client('s3', endpoint_url=S3_URL)
```
<div class="callout">
The AWS credentials will be automatically extracted from the environment.
</div>
Let's plug that into our app:
```python
@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](https://boto3.amazonaws.com/v1/documentation/api/latest/index.html+external) for all the possibilities.
When you re-deploy your app you should see a list of the buckets you have access to:
```cmd
fly deploy
```
Take a look at [the gist](https://gist.github.com/fliepeltje/5be7d0a6f1ada057b1eb3aa357cd4961+external) of this setup to get a full picture.