Skip to content
ebai101Public

About

asyncio Python implementation of the Dropbox HTTP API

Resources

Stars

2 stars

Watchers

1 watching

Forks

Repository files navigation

aiodbx

An asyncio-native Dropbox API v2 client for Python. aiodbx follows the naming pattern of the official Dropbox Python SDK, while providing an awaitable API with streaming file downloads.

This library is in very early development. The current release implements a small number of file API operations, plus a generic RPC method for calling endpoints that are not implemented yet.

Requirements

  • Python 3.11+
  • A Dropbox API access token, or an OAuth 2.0 app key, app secret, and refresh token

An app token with narrow permissions and scoped access to an app folder is recommended while evaluating the library. For long-running or offline use, see OAuth 2.0 with refresh tokens.

Quick start

Set an access token in your environment:

export AIODBX_ACCESS_TOKEN='your-access-token'

Upload bytes, download the file, and verify that Dropbox reports the expected content hash:

from __future__ import annotations

import asyncio
import hashlib
import os

from anyio import Path

from aiodbx import AsyncDropbox


def dropbox_content_hash(data: bytes) -> str:
    """Calculate Dropbox's content hash for an in-memory payload."""
    block_size = 4 * 1024 * 1024
    overall = hashlib.sha256()

    for offset in range(0, len(data), block_size):
        block = data[offset : offset + block_size]
        overall.update(hashlib.sha256(block).digest())

    return overall.hexdigest()


async def main():
    token = os.environ["AIODBX_ACCESS_TOKEN"]
    remote_path = "/aiodbx-example.txt"
    local_path = Path("aiodbx-example.txt")
    content = b"Hello from aiodbx!\n"

    async with AsyncDropbox(token) as dbx:
        uploaded = await dbx.files_upload(
            content,
            remote_path,
            mode="overwrite",
            content_hash=dropbox_content_hash(content),
        )
        print(f"Uploaded {uploaded['path_display']}")

        downloaded = await dbx.files_download_to_path(
            remote_path,
            local_path,
            overwrite=True,
        )
        print(f"Downloaded {downloaded['path_display']}")

    assert await local_path.read_bytes() == content
    print("Upload and download verified.")


asyncio.run(main())

OAuth 2.0 with refresh tokens

A generated access token expires after a few hours. For long-running or offline use, authorize the app once, store the returned refresh token, and let the client mint access tokens on demand.

One-time authorization

Build the authorization URL, send the user to it, then exchange the code from the redirect for a refresh token.

from __future__ import annotations

import asyncio
import os
import secrets

from aiodbx import oauth_authorization_url, oauth_exchange_code


async def main():
    state = secrets.token_urlsafe(32)
    url = oauth_authorization_url(
        os.environ["DROPBOX_APP_KEY"],
        state=state,
        redirect_uri="https://example.com/dropbox/callback",
        scopes=("account_info.read", "files.content.write"),
    )
    print(f"Open this URL and approve the app: {url}")

    # Dropbox redirects to your callback with ?code=...&state=...
    # Verify the returned state before exchanging the code.
    refresh_token = await oauth_exchange_code(
        input("Paste the authorization code: ").strip(),
        app_key=os.environ["DROPBOX_APP_KEY"],
        app_secret=os.environ["DROPBOX_APP_SECRET"],
        redirect_uri="https://example.com/dropbox/callback",
    )
    print(f"Store this refresh token: {refresh_token}")


asyncio.run(main())

oauth_authorization_url always requests token_access_type=offline, so the exchange returns a refresh token. Pass the same code_verifier to both calls to add PKCE S256. The token request authenticates with HTTP Basic and the app secret, so this path targets confidential apps. Public clients without a secret are not supported yet.

Using the refresh token

Pass the app key, app secret, and refresh token as keywords. The client mints an access token on the first request and renews it before it expires.

from __future__ import annotations

import asyncio
import os

from aiodbx import AsyncDropbox


async def main():
    async with AsyncDropbox(
        app_key=os.environ["DROPBOX_APP_KEY"],
        app_secret=os.environ["DROPBOX_APP_SECRET"],
        refresh_token=os.environ["DROPBOX_REFRESH_TOKEN"],
    ) as dbx:
        account = await dbx.users_get_current_account()
        print(account["name"]["display_name"])


asyncio.run(main())

Concurrent calls share one refresh. The client also refreshes and retries once when Dropbox rejects a request with expired_access_token. A fixed token still works. Pass AsyncDropbox("sl...") as before.

If Dropbox no longer accepts the refresh token, the client raises DropboxOAuthError, a subclass of DropboxAuthenticationError. Catch it, run the authorization flow again, and store the new refresh token.

Downloading files

files_download() is a direct wrapper for Dropbox's /2/files/download endpoint. It returns an async context manager so the HTTP response is always closed correctly.

from __future__ import annotations

import asyncio
import os

from aiodbx import AsyncDropbox


async def main():
    async with AsyncDropbox(os.environ["AIODBX_ACCESS_TOKEN"]) as dbx:
        async with dbx.files_download("/reports/latest.csv") as download:
            print(download.metadata["name"])

            async for chunk in download.iter_bytes(chunk_size=1024 * 1024):
                # Write the chunk to an async destination, hash it, or process it.
                print(f"received {len(chunk)} bytes")


asyncio.run(main())

In most use cases, you'll want to save to a file. This will download the file to a temp path and atomically replace the destination on success:

await dbx.files_download_to_path(
    "/reports/latest.csv",
    "latest.csv",
)

Uploading files

Use files_upload_path() to upload one local file. It uses Dropbox's simple upload endpoint for files within Dropbox's simple-upload limit and uses a managed upload session for larger files.

The helper reads large sources in bounded chunks. It owns the upload session only for the duration of the call; it does not persist resumable upload state or retry a body-bearing request after an ambiguous transport or server failure.

from __future__ import annotations

import asyncio
import os

from anyio import Path

from aiodbx import AsyncDropbox


async def main():
    source = Path("dist/release.tar.zst")

    async with AsyncDropbox(os.environ["AIODBX_ACCESS_TOKEN"]) as dbx:
        metadata = await dbx.files_upload_path(
            source,
            "/releases/release.tar.zst",
            mode="overwrite",
        )

    print(f"Uploaded {metadata['path_display']} ({metadata['size']} bytes)")


asyncio.run(main())

Use files_upload() when the content is already available in-memory:

metadata = await dbx.files_upload(
    b"generated report\n",
    "/reports/latest.txt",
    mode="overwrite",
)

Batch-uploading local files

Use files_upload_paths() to stream and batch-commit several local files. Pass each source/destination pair as an UploadPath.

from __future__ import annotations

import asyncio
import os

from anyio import Path

from aiodbx import AsyncDropbox, UploadPath


async def main():
    uploads = [
        UploadPath(
            Path("dist/client-one.tar.zst"),
            "/releases/client-one.tar.zst",
        ),
        UploadPath(
            Path("dist/client-two.tar.zst"),
            "/releases/client-two.tar.zst",
        ),
    ]

    async with AsyncDropbox(os.environ["AIODBX_ACCESS_TOKEN"]) as dbx:
        result = await dbx.files_upload_paths(
            uploads,
            mode="overwrite",
            chunk_size=4 * 1024 * 1024,
        )

    assert result[".tag"] == "complete"

    for entry in result["entries"]:
        if entry[".tag"] == "success":
            metadata = entry["success"]
            print(f"Uploaded {metadata['path_display']}")
        else:
            print(f"Batch entry failed: {entry}")


asyncio.run(main())

Every files_upload_paths() member uses a Dropbox upload session, including small and empty files. The helper streams sources sequentially in bounded chunks, closes every session before finalization, then commits the sessions through Dropbox's batch session API.

The helper returns the result of the /upload_session/finish_batch request. This request may contain failed entries even if it's successful; be sure to inspect the contents of result["entries"].

Like files_upload_path(), this helper does not persist session IDs and does not retry body-bearing upload-session requests after an ambiguous failure. It does retry safe status polling for Dropbox batch jobs according to the client retry policy.

Advanced upload sessions

Use the low-level files_upload_session methods for more granular control over the upload session lifetime:

  • files_upload_session_start()
  • files_upload_session_append_v2()
  • files_upload_session_finish()
  • files_upload_session_finish_batch()
  • files_upload_session_finish_batch_check()

Each body-bearing session call accepts one in-memory bytes-like block. aiodbx does not automatically retry a start, append, or finish request after a timeout, connection failure, or transient server response because Dropbox may have received the block and advanced the remote session state.

first = b"first block"
middle = b"middle block"
last = b"final block"

started = await dbx.files_upload_session_start(first)

cursor = {
    "session_id": started["session_id"],
    "offset": len(first),
}

await dbx.files_upload_session_append_v2(cursor, middle)
cursor["offset"] += len(middle)

metadata = await dbx.files_upload_session_finish(
    cursor,
    {
        "path": "/uploads/example.bin",
        "mode": "overwrite",
        "autorename": False,
        "mute": False,
        "strict_conflict": False,
    },
    last,
)

Requesting unimplemented endpoints

AsyncDropbox.rpc() provides access to Dropbox JSON-RPC endpoints that do not yet have a first-class aiodbx method:

result = await dbx.rpc(
    "/2/users/get_space_usage",
    {},
)

rpc() sends a JSON-object payload to api.dropboxapi.com and returns a raw JSON-object response. It uses the same token, timeouts, retry policy, and exception mapping as named client methods.

It does not support Dropbox content-upload, content-download, or long-poll endpoints. Those endpoint types use different wire formats and should be accessed through dedicated methods.

Listing a shared link

Create a SharedLink with its URL and optional password, then retrieve the link root metadata and list entries relative to that root:

from aiodbx import SharedLink

link = SharedLink("https://www.dropbox.com/s/example/shared-file")
metadata = await dbx.sharing_get_shared_link_metadata(link)
print(metadata["name"], metadata[".tag"])

async for entry in dbx.files_list_folder_iter("", shared_link=link):
    print(entry["name"])

The path is relative to the link root, and shared-link listings are non-recursive only. Entries may omit or mis-express path_display, so build paths from each entry's name. Listing requires the files.metadata.read OAuth scope. Metadata also requires sharing.read.

Implementation table

Single endpoint methods

Method Dropbox endpoint
users_get_current_account() /2/users/get_current_account
sharing_get_shared_link_metadata() /2/sharing/get_shared_link_metadata
files_get_metadata() /2/files/get_metadata
files_list_folder() /2/files/list_folder
files_list_folder_continue() /2/files/list_folder/continue
files_download() /2/files/download
files_upload() /2/files/upload
files_create_folder_v2() /2/files/create_folder_v2
files_delete_v2() /2/files/delete_v2
files_upload_session_start() /2/files/upload_session/start
files_upload_session_append_v2() /2/files/upload_session/append_v2
files_upload_session_finish() /2/files/upload_session/finish
files_upload_session_finish_batch() /2/files/upload_session/finish_batch
files_upload_session_finish_batch_check() /2/files/upload_session/finish_batch/check

Helpers

These helpers orchestrate the above methods for convenience, and do not correspond directly to single endpoints.

Method Behavior
files_list_folder_iter Iterates all pages from files_list_folder. Accepts a shared_link and does not walk subfolders
files_download_to_path Streams a Dropbox file to a local path atomically
files_upload_path Uploads one local file, using simple upload or a managed session
files_upload_paths Streams local files into sessions and batch-commits them

About

asyncio Python implementation of the Dropbox HTTP API

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages