Metadata-Version: 2.4
Name: storage3
Version: 3.0.0a2
Summary: Supabase Storage client for Python.
Author: Joel Lee, Leon Fedden, Daniel Reinón García, Leynier Gutiérrez González, Anand Krishna
Author-email: Joel Lee <joel@joellee.org>, Leon Fedden <leonfedden@gmail.com>, Daniel Reinón García <danielreinon@outlook.com>, Leynier Gutiérrez González <leynier41@gmail.com>, Anand Krishna <anand2312@proton.me>
License-Expression: MIT
License-File: LICENSE
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Operating System :: OS Independent
Requires-Dist: pydantic>=2.11.7
Requires-Dist: yarl>=1.20.1
Requires-Dist: supabase-utils==3.0.0a2
Requires-Dist: pyiceberg[pyarrow]>=0.11.0 ; extra == 'iceberg'
Maintainer: Leonardo Santiago
Maintainer-email: Leonardo Santiago <leonardo.santiago@supabase.io>
Requires-Python: >=3.12
Project-URL: documentation, https://supabase.com/docs/reference/python/storage-createbucket
Project-URL: homepage, https://github.com/supabase/supabase-py/tree/main/src/storage
Project-URL: repository, https://github.com/supabase/supabase-py
Project-URL: changelog, https://github.com/supabase/supabase-py/tree/main/CHANGELOG.md
Provides-Extra: iceberg
Description-Content-Type: text/markdown

# storage3

Python client for Supabase Storage. Most users reach it through the
[Supabase client](https://github.com/supabase/supabase-py/blob/main/src/supabase/README.md)
as `client.storage`, which sets the headers for you.

## Installation

Needs an HTTP client adapter, such as httpx:

```bash
uv add storage3 "supabase_utils[httpx]"
# or
pip install storage3 "supabase_utils[httpx]"
```

## Supabase Service Compatibility

Only the [latest Storage release](https://github.com/supabase/storage/releases)
is guaranteed to work. Older releases are supported on a best-effort basis. If
you self-host, upgrade Storage when you upgrade this package.

| Feature                                     | Minimum Storage version |
| ------------------------------------------- | ----------------------- |
| `list()`                                    | v1.19.0                 |
| Paging, search and sort in `list_buckets()` | v1.25.0                 |

Older releases ignore the `list_buckets()` arguments and return every bucket.

## Usage

The client takes the storage URL, the headers to send and the HTTP session.
Leaving the context manager closes the session:

```python
import asyncio

from httpx import AsyncClient
from storage3 import AsyncStorageClient
from supabase_utils.http.adapters.httpx import AsyncHttpxSession

url = "https://<project_ref>.supabase.co/storage/v1/"
key = "<your api key>"
headers = {"apikey": key, "Authorization": f"Bearer {key}"}


async def main() -> None:
    session = AsyncHttpxSession(AsyncClient())
    async with AsyncStorageClient(url, headers, session) as storage_client:
        print(await storage_client.list_buckets())


asyncio.run(main())
```

`SyncStorageClient` with an `HttpxSession` works the same way without `await`.
The examples below use the async `storage_client`.

### Upload

Set the MIME type with `content_type`. It defaults to
`text/plain;charset=UTF-8`. A path, an open binary file or an iterator over
bytes is streamed, so a large file is never held in memory:

```python
from pathlib import Path

await storage_client.from_("bucket").upload(
    "folder/file.png", Path("file.png"), content_type="image/png"
)
```

### Resumable upload

`resumable_upload` sends a large file in 6 MiB chunks with the
[tus protocol](https://tus.io). A failed upload continues from the last chunk.
The `store` keeps the upload URL, so the upload can continue after a restart:

```python
import shelve

from storage3 import ResumableUploadPausedError

with shelve.open("uploads") as store:
    upload = storage_client.from_("bucket").resumable_upload(
        "videos/intro.mp4", "intro.mp4", content_type="video/mp4", store=store
    )
    try:
        response = await upload.run()
    except ResumableUploadPausedError:
        print(f"Paused after {upload.offset} of {upload.size} bytes")
```

`upload.pause()` stops after the current chunk and `run()` continues.
`await upload.cancel()` deletes the upload on the server.

### Streaming download

`download_stream` gives the status and headers first, then the body as it
arrives. `write_to` replaces the file only once the whole body arrived.
`read(max_bytes=...)` raises `StorageDownloadTooLargeError` past the cap:

```python
bucket = storage_client.from_("bucket")
async with bucket.download_stream("videos/intro.mp4") as download:
    print(download.status, download.content_length)
    await download.write_to("intro.mp4")
```

### Progress

`upload`, `update`, `upload_to_signed_url`, `download` and `download_stream`
take an `on_progress` callback. The total is `None` when it is unknown. The
count restarts on a retry, and the callback can run in a worker thread:

```python
from pathlib import Path


def on_progress(moved: int, total: int | None) -> None:
    print(f"{moved} of {total or '?'} bytes")


await storage_client.from_("bucket").upload(
    "videos/intro.mp4", Path("intro.mp4"), on_progress=on_progress
)
```

### Byte ranges

`download` and `download_stream` take an `offset` and a `length`. If the server
ignores the range, `download` raises `StorageRangeIgnoredError`:

```python
first_bytes = await storage_client.from_("bucket").download(
    "videos/intro.mp4", length=512
)
```

To resume a download, pass the size of the partial file as `offset` and
`resume=True` to `write_to`. Pass the `etag` of the first download as
`if_match`, so a changed file raises `StorageApiError` with the code
`PreconditionFailed` instead of mixing two versions:

```python
from pathlib import Path


async def resume(partial: Path, etag: str) -> None:
    async with storage_client.from_("bucket").download_stream(
        "videos/intro.mp4", offset=partial.stat().st_size, if_match=etag
    ) as download:
        await download.write_to(partial, resume=True)
```

### File versions

A bucket with `versioning_status="ENABLED"` keeps old versions when a file is
overwritten or removed. List them with `noncurrent_versions` and the delete
markers with `delete_markers`:

```python
await storage_client.create_bucket("documents", versioning_status="ENABLED")
documents = storage_client.from_("documents")

page = await documents.list("reports/", noncurrent_versions="include")
for entry in page.objects:
    print(entry.name, entry.version, entry.archived_at)
```

Pass `version_id` to read a version, or `source_version_id` to `copy` or `move`
one. Moving a version to its own path restores it. `remove` takes a
`FileVersion` to delete one version:

```python
from storage3.types import FileVersion

documents = storage_client.from_("documents")
old = await documents.download("reports/q3.pdf", version_id="<version id>")
await documents.move(
    "reports/q3.pdf", "reports/q3.pdf", source_version_id="<version id>"
)
await documents.remove([FileVersion("reports/q3.pdf", "<version id>")])
```

`versioning_status="SUSPENDED"` stops keeping new versions. A bucket cannot go
back to `DISABLED`.
