---
title: "Upload a large collection"
description: "Upload a local folder tree of thousands of files into a drive, with resumable batches."
canonical_url: "https://docs.flashdrive.space/sdk/bulk-imports"
---
# Upload a large collection

> Upload a local folder tree of thousands of files into a drive, with resumable batches.

These examples use the latest stable release of the Python SDK.

`client.upload()` handles large collections: one call takes up to 100,000 files, and the SDK groups the requests of a multi-file upload into bulk requests of up to 1,000 files each. By default (`route="auto"`), a batch of at least 1,000 files or 1 GB, all of them text files, goes through a bulk import: read `batch.import_status`, as `batch.files` is empty, and pass `journal=` to make it resumable. Pass `route="uploads"` for a receipt per file. `client.imports` plans, runs and resumes bulk imports directly; a stopped import raises `BulkImportError` with the `import_id`, `drive_id` and `journal` to resume from. This page shows how to copy a local folder tree into a drive, keep its folders, and resume after an interruption.

For a few files, see [upload files](https://docs.flashdrive.space/sdk/upload-files). To keep a local folder and a drive in step from a terminal, use [a mount or `flashdrive pull` and `flashdrive push`](https://docs.flashdrive.space/cli/mount-and-sync).

## Check the drive has room

A drive holds up to 500,000 files and 1 TB, and each file can be up to 10,000,000,000 bytes. Read the values the service currently enforces with `client.limits()`:

```python
limits = client.limits()
print(limits.max_drive_documents, limits.max_drive_source_bytes, limits.max_file_bytes)
```

Each client fetches limits at most once an hour; pass `refresh=True` to fetch them again. Treat them as an early warning: the service has the final say, and refuses growth past a drive limit with code `drive_limit_exceeded`, or past your workspace's storage with `storage_capacity_exceeded`.

## Upload a folder tree

```python
import hashlib
from collections import defaultdict
from pathlib import Path
from flashdrive import Flashdrive

root = Path("library")
files = sorted(p for p in root.rglob("*") if p.is_file())

# Group the files by their folder, relative to the root.
by_folder = defaultdict(list)
for path in files:
    by_folder[path.parent.relative_to(root).as_posix()].append(path)

with Flashdrive(drive_id="DRIVE_UUID") as client:
    folder_paths = sorted(p for p in by_folder if p != ".")
    folder_ids = {}
    for start in range(0, len(folder_paths), 500):
        for made in client.folders.ensure(folder_paths[start:start + 500]):
            folder_ids[made.path] = made.id

    for relative, paths in sorted(by_folder.items()):
        client.upload(
            paths,
            folder_id=folder_ids.get(relative),       # None: the top of the drive
            concurrency=10,
            timeout=3600,
            processing_timeout=3600,
            idempotency_key="library-1:" + hashlib.sha256(relative.encode()).hexdigest()[:32],
        )
```

`folders.ensure()` creates every folder path that is missing, reuses the ones that exist, and returns a `FolderPath` (`path` and `id`) for each. It takes up to 500 paths per call. See [folders](https://docs.flashdrive.space/sdk/folders) for the naming rules.

Each `upload()` call above sends one folder's files with a key derived from the folder's path. A key is 1 to 128 visible ASCII characters, so the example hashes the path. Keep the key, the file list and its order the same when you run the script again, so a retry finds the work already done and nothing is uploaded twice. Use a new prefix, such as `library-2`, for a new copy of the collection.

## Resume after an interruption

If the script stops, run it again unchanged. For each folder:

- Files the service already accepted are not uploaded again: the same key and the same ordered file list identify the same uploads.
- Files whose transfer did not finish are sent again.
- An `UploadBatchError` keeps the files that succeeded in `exc.batch.files` and the failures in `exc.failures`; see [upload recovery](https://docs.flashdrive.space/sdk/upload-recovery).

Unfinished upload sessions and request keys last 24 hours. After that, a rerun with the same key starts new uploads, so finish or check an interrupted collection within a day, and follow already-queued files by their saved receipts.

## Choose batch sizes

- Keep each call to a size you are happy to retry. A folder at a time works well; split a very large folder into several calls, each with its own key and a fixed file order.
- `concurrency` (1 to 50, default 3) sets how many files one call transfers at once. Raise it for many small files.
- `timeout` covers the transfers of the whole call and `processing_timeout` the processing after them. Raise both for large batches.
- Several calls running at the same time share the drive's upload capacity. The SDK waits for capacity on its own and retries with the same key.

Uploading the same file again without its `external_id` creates a second file. To update files you uploaded before, see [replace files](https://docs.flashdrive.space/sdk/replace-and-label).

## Writes while you upload

Other people and agents can keep working in the drive while a large upload runs. To see what changed while your script ran, read the drive's [change feed](https://docs.flashdrive.space/sdk/files).


## Sitemap

See the full [sitemap](https://docs.flashdrive.space/sitemap.md) for all pages.
