LoginSignup
2
2

More than 5 years have passed since last update.

Upload a file to Dropbox

Last updated at Posted at 2015-09-22

Here is a sample code.

Please install Dropbox module from here. https://github.com/dropbox/dropbox-sdk-python

import sys
import os
import argparse

import dropbox
from dropbox.files import WriteMode
from dropbox.exceptions import ApiError

# Add OAuth2 access token here.
# You can generate one for yourself in the App Console.
# See <https://blogs.dropbox.com/developers/2014/05/generate-an-access-token-for-your-own-account/>
TOKEN = os.environ['DBTOKEN']


# Uploads contents of local_file to Dropbox
def backup(local_file, backup_path):
    with open(local_file, 'rb') as f:
        # We use WriteMode=overwrite to make sure that the settings in the file
        # are changed on upload
        try:
            dbx.files_upload(f, backup_path, mode=WriteMode('overwrite'))
        except ApiError as err:
            # This checks for the specific error where a user doesn't have
            # enough Dropbox space quota to upload this file
            if (err.error.is_path() and
                    err.error.get_path().error.is_insufficient_space()):
                sys.exit("Cannot back up; insufficient space.")
            elif err.user_message_text:
                print(err.user_message_text)
                sys.exit()
            else:
                print(err)
                sys.exit()

parser = argparse.ArgumentParser(description='Upload a file to Dropbox')
parser.add_argument('--file', '-f', help='file path')
parser.add_argument('--dbpath', '-d', help='dropbox path')

if __name__ == '__main__':
    # Create an instance of a Dropbox class, which can make requests to the API.
    dbx = dropbox.Dropbox(TOKEN)
    args = parser.parse_args()
    if not args.file:
        print("Please specify the file" +
                "Usage: uploadb.py -f file/path -d dropbox/path")
        exit(1)
    local_file = args.file
    if not args.dbpath:
        dbpath = '/' + local_file
    else:
        dbpath = args.dbpath

    # Create a backup of the current settings file
    backup(local_file, dbpath)

Motivation

Somehow scp and sftcp are sooo slow.(I am in country side of France now) But http access is fine, though it is not so fast. I guess there are some listriction for specific port. I want get a file from my server in Japan, which I can ssh to. I upload a file to dropbox and download it to my machine.

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2