Upload a file to Dropbox

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.

Recommended Posts

Upload a file to Dropbox
Write standard output to a file
How to create a config file
File upload to Azure Storage (Python)
Upload photos from LINE Bot to Dropbox
Script to create a Mac dictionary file
How to disguise a ZIP file as a PNG file
Upload text file to rental server by ftp
Upload a large number of images to Wordpress
How to create a JSON file in Python
Save the object to a file with pickle
I wrote a script to upload a WordPress plugin
Export Python3 version OpenCV KeyPoint to a file
How to upload to a shared drive using pydrive
I want to write to a file with Python
How to read a file in a different directory
I want to upload a Django app to heroku
File upload with django
Parse a JSON string written to a file in Python
Created a module to monitor file and URL updates
Convert a text file with hexadecimal values to a binary file
What to do when gdal_merge creates a huge file
A memorandum to run a python script in a bat file
I want to randomly sample a file in Python
How to turn a .py file into an .exe file
How to convert a mel spectrogram back to a wav file
Python script to create a JSON file from a CSV file
Upload to a shared drive with Google Drive API V3
Output a binary dump in binary and revert to a binary file
Publish / upload a library created in Python to PyPI
How to put a hyperlink to "file: // hogehoge" with sphinx-> pdf
[python] Change the image file name to a serial number
A road to intermediate Python
Conveniently upload to kaggle Dataset
A super introduction to Linux
How to run a Python file at a Windows 10 command prompt
A Python script that saves a clipboard (GTK) image to a file.
Convert HTML to text file
Add a dictionary to MeCab
Change the standard output destination to a file in Python
How to call a function
How to import a file anywhere you like in Python
Send a signal to subprocess
Parse JSON file to object
Read and write a file
How to hack a terminal
Create a dummy data file
Write and read a file
Export a gzip-compressed text file
How to use a file other than .fabricrc as a configuration file
[Python] How to output a pandas table to an excel file
File upload with Flask + jQuery
Try to select a language
Convert genbank file to gff file
How to upload a file to Cloud Storage using Python [Make a fixed point camera with Raspberry PI # 1]
Output the key list included in S3 Bucket to a file
How to paste a CSV file into an Excel file using Pandas
I tried to convert a Python file to EXE (Recursion error supported)
Make it possible to output a log to a file with go echo
Process Splunk execution results using Python and save to a file
Conditional branch due to the existence of a shell script file