Skip to content

Instantly share code, notes, and snippets.

@akiross
Forked from PetrGlad/dropbox_upload_large.py
Last active October 25, 2018 11:18
Show Gist options
  • Save akiross/4a01124afb4190aa6c5c7d0a5242a49f to your computer and use it in GitHub Desktop.
Save akiross/4a01124afb4190aa6c5c7d0a5242a49f to your computer and use it in GitHub Desktop.
Upload large files to dropbox
#!/usr/bin/env python3
# This tool helps uploading (large) files on your dropbox, without having to install it.
# First, be sure to create an app for this to run and generate a token:
# https://www.dropbox.com/developers/apps
# Setting up example
# $ python3 -m venv venv_dbx
# $ . ./venv_dbx/bin/activate
# (venv_dbx) $ pip3 install dropbox
# Optionally install also tqdm for progress bars
# (venv_dbx) $ pip3 install dropbox tqdm
# (venv_dbx) $ export DBX_TOKEN="your_api_token"
# (venv_dbx) $ python3 dropbox_upload_large.py files to upload
import os
import sys
import argparse
from pathlib import Path
try:
import dropbox
import dropbox.files
except ModuleNotFoundError:
print("Unable to find dropbox module, please install them.")
try:
import tqdm
except:
tqdm = None
print("No tqdm module found. Progress bars won't be showsn.")
CHUNK_SIZE = 4 * 1024 * 1024
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--token', type=str,
default=os.getenv('DBX_TOKEN', None),
help='Dropbox APIv2 access token')
parser.add_argument('files', nargs='+', type=Path,
help='Files to upload')
return parser.parse_args()
def upload_stream(dbx, readable, dest_path, size):
if tqdm is not None:
progress = tqdm.tqdm(total=size, unit='Bytes', unit_scale=True)
d = readable.read(CHUNK_SIZE)
offset = len(d)
session = dbx.files_upload_session_start(d)
cursor = dropbox.files.UploadSessionCursor(session_id=session.session_id,
offset=offset)
if tqdm is not None:
progress.update(len(d))
while True:
d = readable.read(CHUNK_SIZE)
offset += len(d)
if len(d) == 0:
commit = dropbox.files.CommitInfo(path=dest_path)
if tqdm is not None:
progress.close()
return dbx.files_upload_session_finish(d, cursor, commit)
dbx.files_upload_session_append_v2(d, cursor)
cursor.offset = offset
if tqdm is not None:
progress.update(len(d))
def upload_file(dbx, file_path, dest_path):
with file_path.open('rb') as f:
return upload_stream(dbx, f, dest_path, size=file_path.stat().st_size)
if __name__ == '__main__':
args = parse_args()
if args.token is None:
print("Dropbox token required. Set it with -t or env DBX_TOKEN.")
sys.exit(1)
try:
dbx = dropbox.Dropbox(args.token)
acct = dbx.users_get_current_account()
print("Accessing dropbox as user", acct.name.display_name)
except dropbox.exceptions.DropboxException:
print("Unable to access dropbox: aborting.")
sys.exit(2)
for path in args.files:
if path.exists():
# FIXME /path.name is a bit simplistic
try:
print("Uploading file", path)
meta = upload_file(dbx, path, "/" + path.name)
print("Uploaded", path.name, "with hash", meta.content_hash)
except dropbox.exceptions.ApiError:
print("Unable to upload, skipping.")
else:
print("Non existing path", path)
@akiross
Copy link
Author

akiross commented Oct 25, 2018

TODO? put dropbox key in home config file and search that as well

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