> ## Documentation Index
> Fetch the complete documentation index at: https://docs.servbase.net/llms.txt
> Use this file to discover all available pages before exploring further.

# Servbase Object Storage (Beta)

> Store files in public or private buckets, upload from the dashboard or the API, and connect a bucket to your app with ready-made environment variables.

> [!NOTE]
>
> Object storage is in beta.

Object storage keeps files such as images, uploads, backups and exports in **buckets**. Each bucket is either **private** (files are reached only with an API key) or **public** (anyone with a file's link can read it, which suits website assets).

## Create a bucket

1. Open **Object storage** in the sidebar and select **Create bucket**.
2. Enter a name: 3 to 63 lowercase letters, digits and hyphens, unique in the workspace.
3. The bucket is **private** by default. To make it public, open the bucket's **Settings** tab and turn on public access.

A bucket can be deleted once it is empty.

## Work with files in the dashboard

Inside a bucket you can:

* upload files with **Upload** or by dragging them onto the list;
* create folders (a folder appears once a file is uploaded into it);
* download, copy a public link (public buckets), and delete files or whole folders.

The panel at the top of the file list shows the bucket's **Public URL** for the folder you are viewing (public buckets) and its **API endpoint**, each with a copy button.

## Public and private buckets

| | Private bucket | Public bucket |
| - | - | - |
| Read a file | API key with `storage:read` | Anyone with the link, no key |
| Upload or delete | API key with `storage:write` | API key with `storage:write` |
| Typical use | User uploads, documents, backups | Images, CSS, downloads for a website |

Uploading and deleting always require a key, also for public buckets.

### Public file links

Every file in a public bucket has its own link:

```text theme={null}
https://bucket.servbase.site/sb<bucket-id>/<key>
```

Use it directly in a website:

```html theme={null}
<img src="https://bucket.servbase.site/sb<bucket-id>/images/logo.png" alt="Logo">
```

Public links can be loaded by any website, and they are cached for five minutes.

### Private files

Private files are read through the API with a key. Never put a key in browser code; read the file on your server and send it to the visitor, or keep the bucket private and serve files through your app.

## Connect a bucket to your app

The easiest way to use object storage from an app is to connect a bucket in the app's settings. Servbase then adds the configuration to the app for you.

1. Open your app and go to **Settings**, then **Object storage**.
2. Choose the **bucket** and the **access**: **Read and write**, or **Read only**.
3. Select **Connect bucket**.

Servbase creates an API key that works **only for that bucket** and adds these variables to the app's **Variables**:

| Variable | Value |
| - | - |
| `SERVBASE_STORAGE_ENDPOINT` | The bucket's API address, for example `https://api.servbase.net/bkt/0b9f6c2e-1d3a-4c5b-8e7f-123456789abc` |
| `SERVBASE_STORAGE_BUCKET` | The bucket name |
| `SERVBASE_STORAGE_KEY` | The bucket's API key, stored as a secret |
| `SERVBASE_STORAGE_PUBLIC_URL` | Base of public file links (public buckets only) |

A running app restarts automatically so it picks up the new variables. A static site reads them on its next deployment.

* **Change** switches to another bucket or access level; the old key is revoked.
* **Disconnect** revokes the key and removes the variables.
* Only the workspace owner can connect or disconnect a bucket, because it creates an API key.
* The key appears on the **API** page as `App storage: <app domain>`. If you delete it there, the settings page shows that the connection stopped working.

## Use the bucket in your code

Every request sends the key as a bearer token. Paths below are relative to `SERVBASE_STORAGE_ENDPOINT`.

| Action | Request |
| - | - |
| Upload or replace a file | `PUT /objects/<key>` with the file as the body and its `Content-Type` |
| Download a file | `GET /objects/<key>` (add `?download=true` to get it as an attachment) |
| List files | `GET /objects?prefix=folder/&limit=100&cursor=` |
| Delete files | `POST /delete` with `{"keys": ["a.png"]}` or `{"prefix": "folder/"}` |

```javascript Node.js theme={null}
const { SERVBASE_STORAGE_ENDPOINT: endpoint, SERVBASE_STORAGE_KEY: key } = process.env;
const auth = { Authorization: `Bearer ${key}` };

// Upload a file (for example from an upload form)
await fetch(`${endpoint}/objects/uploads/avatar.png`, {
  method: "PUT",
  headers: { ...auth, "Content-Type": "image/png" },
  body: fileBuffer,
});

// List a folder
const { objects } = await (await fetch(`${endpoint}/objects?prefix=uploads/`, { headers: auth })).json();

// Download a file
const file = await fetch(`${endpoint}/objects/uploads/avatar.png`, { headers: auth });
const bytes = Buffer.from(await file.arrayBuffer());
```

```python Python theme={null}
import os
import requests

endpoint = os.environ["SERVBASE_STORAGE_ENDPOINT"]
auth = {"Authorization": f"Bearer {os.environ['SERVBASE_STORAGE_KEY']}"}

# Upload a file
with open("avatar.png", "rb") as f:
    requests.put(f"{endpoint}/objects/uploads/avatar.png", data=f,
                 headers={**auth, "Content-Type": "image/png"}).raise_for_status()

# List a folder
objects = requests.get(f"{endpoint}/objects", params={"prefix": "uploads/"}, headers=auth).json()["objects"]

# Download a file
data = requests.get(f"{endpoint}/objects/uploads/avatar.png", headers=auth).content
```

```php PHP theme={null}
<?php
$endpoint = getenv('SERVBASE_STORAGE_ENDPOINT');
$key = getenv('SERVBASE_STORAGE_KEY');

// Upload a file from a form field named "photo"
$ch = curl_init("$endpoint/objects/uploads/" . rawurlencode($_FILES['photo']['name']));
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'PUT',
    CURLOPT_POSTFIELDS => file_get_contents($_FILES['photo']['tmp_name']),
    CURLOPT_HTTPHEADER => ["Authorization: Bearer $key", 'Content-Type: ' . $_FILES['photo']['type']],
    CURLOPT_RETURNTRANSFER => true,
]);
curl_exec($ch);
```

```bash curl theme={null}
curl -X PUT -H "Authorization: Bearer $SERVBASE_STORAGE_KEY" \
  -H "Content-Type: image/png" --data-binary @avatar.png \
  "$SERVBASE_STORAGE_ENDPOINT/objects/uploads/avatar.png"
```

### Example: show uploaded images from a public bucket

When the connected bucket is public, build image links from `SERVBASE_STORAGE_PUBLIC_URL` instead of downloading the files:

```javascript Express theme={null}
app.get("/gallery", async (req, res) => {
  const auth = { Authorization: `Bearer ${process.env.SERVBASE_STORAGE_KEY}` };
  const list = await fetch(`${process.env.SERVBASE_STORAGE_ENDPOINT}/objects?prefix=uploads/`, { headers: auth });
  const { objects } = await list.json();
  const images = objects.map((o) => `${process.env.SERVBASE_STORAGE_PUBLIC_URL}/${o.key}`);
  res.send(images.map((src) => `<img src="${src}" width="200">`).join(""));
});
```

### Example: serve a private file to signed-in users

```python Flask theme={null}
import os, requests
from flask import Flask, Response, abort

app = Flask(__name__)

@app.get("/invoices/<name>")
def invoice(name):
    if not current_user_can_see(name):  # your own access check
        abort(403)
    r = requests.get(f"{os.environ['SERVBASE_STORAGE_ENDPOINT']}/objects/invoices/{name}",
                     headers={"Authorization": f"Bearer {os.environ['SERVBASE_STORAGE_KEY']}"})
    if r.status_code == 404:
        abort(404)
    return Response(r.content, content_type=r.headers["Content-Type"])
```

## Use the API without connecting an app

For scripts or CI, create an API key on the **API** page with the storage scopes it needs (`storage:read`, `storage:write`, `storage:delete`, and `storage:buckets` to create or change buckets), for all buckets or only specific ones. Every endpoint, with examples, is in the [Object storage API reference](https://docs.servbase.net/api/object-storage.md):

```bash theme={null}
# List buckets
curl -H "Authorization: Bearer $SERVBASE_API_KEY" https://api.servbase.net/storage/buckets

# Create a public bucket
curl -X POST -H "Authorization: Bearer $SERVBASE_API_KEY" -H "Content-Type: application/json" \
  -d '{"name": "my-assets", "public": true}' https://api.servbase.net/storage/buckets
```

A key created by connecting an app is limited to its bucket: it cannot reach other buckets or create, change or delete buckets.

## Limits

The **Object storage** page shows how much of your workspace's storage quota is used and the maximum size of one object. Keys are limited to 600 requests per minute.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.