# Object storage

> The Bitiful S4 object storage API: query a bucket's capacity, file count, and real-time access logs, with authentication, error format, and pagination examples.

Query an S4 bucket's capacity and file count, plus real-time access logs for the last 3 days.

Uploading, downloading, and listing files don't go through this API. Use an [S3 SDK](https://docs.bitiful.com/en/developer/sdk/intro.md).

## Authentication

Every endpoint is authenticated with the `Authorization` request header, whose value is your console API token (no `Bearer` prefix). Get one at [Console › API Token](https://console.bitiful.com/apiToken). For details, see [API tokens](https://docs.bitiful.com/en/console/api-token.md).

```bash
curl -H "Authorization: <API Token>" \
  "https://api.bitiful.com/s4/data/<bucket-name>/usage"
```

> **Warning**
>
> An API token can access the resources in your account. Use it on the server only, and never put it in frontend code or a public repository.

## Base URL

```text
https://api.bitiful.com
```

## Error format

When a request fails, the matching HTTP status code and JSON are returned, and `message` is the error description:

```json
{"message":"鉴权失败"}
```

| Status code | Meaning                                                                                                                              | What to do                                                                                    |
| ----------- | ------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------- |
| 401         | The token is missing or invalid                                                                                                      | Check the request header, confirm there's no `Bearer`, and that the token hasn't been deleted |
| 403         | An invalid parameter, the bucket doesn't belong to the current account, the query time is out of range, or the account is restricted | Fix it according to `message`                                                                 |
| 500         | Server error                                                                                                                         | Try again later                                                                               |

## Example: query capacity

```bash
curl -H "Authorization: <API Token>" \
  "https://api.bitiful.com/s4/data/my-bucket/usage"
```

Capacity is in bytes. `tmp_*` are temporary files produced by multipart uploads, and they're also counted in `total_*`.

## Example: pull recent error logs

```bash
curl -G "https://api.bitiful.com/s4/data/my-bucket/logs" \
  -H "Authorization: <API Token>" \
  --data-urlencode "start_time=$(date +%s)000000000" \
  --data-urlencode "codes=4XX,5XX" \
  --data-urlencode "limit=100"
```

`start_time` is the time **upper bound** and is required. Logs are returned from newest to oldest, and each one is earlier than `start_time`.

## Example: page through all logs

```python title="logs.py"
import time
import requests

URL = "https://api.bitiful.com/s4/data/my-bucket/logs"
HEADERS = {"Authorization": "<API Token>"}

cursor = time.time_ns()                        # Page backward from now
oldest = cursor - 3 * 24 * 3600 * 10**9        # Only the last 3 days can be queried

while cursor > oldest:
    resp = requests.get(URL, headers=HEADERS, params={"start_time": cursor, "limit": 1000}, timeout=60)
    resp.raise_for_status()
    logs = resp.json()["data"]
    if not logs:
        break
    for log in logs:
        print(log["access_time"], log["code"], log["method"], log["path"])
    cursor = logs[-1]["access_time"]           # The last entry is the earliest on this page
```

> **Note**
>
> `access_time` is an integer in nanoseconds, about 1.8 × 10^18, which exceeds the exact range of a JavaScript `Number`. When paging in JavaScript, use a JSON parser that supports big integers, or handle it with `BigInt`.
