Skip to content

Instantly share code, notes, and snippets.

@barbietunnie
Created November 21, 2020 03:53
Show Gist options
  • Save barbietunnie/d670d5601151129cbc02fbac3800e399 to your computer and use it in GitHub Desktop.
Save barbietunnie/d670d5601151129cbc02fbac3800e399 to your computer and use it in GitHub Desktop.
Upload Large Files with Dropbox v2

Upload Large Files with Dropbox v2

To upload large files with Dropbox, you need to use upload sessions.

The utility function below can be used for this purpose.

It uses the Dropbox Python SDK to upload the local file specified as file_path to the remote path specified by dest_path. It also chooses whether or not to use an upload session based on the size of the file.

import os
import dropbox
from tqdm import tqdm

def upload(
    access_token,
    file_path,
    target_path,
    timeout=900,
    chunk_size=4 * 1024 * 1024,
):
    dbx = dropbox.Dropbox(access_token, timeout=timeout)
    with open(file_path, "rb") as f:
        file_size = os.path.getsize(file_path)
        chunk_size = 4 * 1024 * 1024
        if file_size <= chunk_size:
            print(dbx.files_upload(f.read(), target_path))
        else:
            with tqdm(total=file_size, desc="Uploaded") as pbar:
                upload_session_start_result = dbx.files_upload_session_start(
                    f.read(chunk_size)
                )
                pbar.update(chunk_size)
                cursor = dropbox.files.UploadSessionCursor(
                    session_id=upload_session_start_result.session_id,
                    offset=f.tell(),
                )
                commit = dropbox.files.CommitInfo(path=target_path)
                while f.tell() < file_size:
                    if (file_size - f.tell()) <= chunk_size:
                        print(
                            dbx.files_upload_session_finish(
                                f.read(chunk_size), cursor, commit
                            )
                        )
                    else:
                        dbx.files_upload_session_append(
                            f.read(chunk_size),
                            cursor.session_id,
                            cursor.offset,
                        )
                        cursor.offset = f.tell()
                    pbar.update(chunk_size)

Sample Usage:

access_token = '<enter your application access token here>'
source_file = "<source file path>"
dest_file = "<destination file path>"

upload_v2(access_token, source_file, dest_file)

Credits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment