Skip to content

Instantly share code, notes, and snippets.

@giwa
Last active September 22, 2015 21:31
Show Gist options
  • Select an option

  • Save giwa/d7dcd6e9cdd85f002400 to your computer and use it in GitHub Desktop.

Select an option

Save giwa/d7dcd6e9cdd85f002400 to your computer and use it in GitHub Desktop.
Upload a file to Dropbox ref: http://qiita.com/giwa/items/5c7c17f382d57e11405e
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment