upload file to my dropbox from python script

The answer of @Christina is based on Dropbox APP v1, which is deprecated now and will be turned off on 6/28/2017. (Refer to here for more information.)

APP v2 is launched in November, 2015 which is simpler, more consistent, and more comprehensive.

Here is the source code with APP v2.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import dropbox

class TransferData:
    def __init__(self, access_token):
        self.access_token = access_token

    def upload_file(self, file_from, file_to):
        """upload a file to Dropbox using API v2
        """
        dbx = dropbox.Dropbox(self.access_token)

        with open(file_from, 'rb') as f:
            dbx.files_upload(f.read(), file_to)

def main():
    access_token = '******'
    transferData = TransferData(access_token)

    file_from = 'test.txt'
    file_to = '/test_dropbox/test.txt'  # The full path to upload the file to, including the file name

    # API v2
    transferData.upload_file(file_from, file_to)

if __name__ == '__main__':
    main()

The source code is hosted on GitHub, here.


Important Note: this answer is deprecated since dropbox uses v2 API now.
See the answer of @SparkAndShine for current API version solution

Thanks to @smarx for the answer above! I just wanted to clarify for anyone else trying to do this.

  1. Make sure you install the dropbox module first of course, pip install dropbox.

  2. Create an app under your own dropbox account in the "App Console". (https://www.dropbox.com/developers/apps)

  3. Just for the record I created my App with the following:

    a. App Type as "Dropbox API APP".

    b. Type of data access as "Files & Datastores"

    c. Folder access as "My app needs access to files already on Dropbox". (ie: Permission Type as "Full Dropbox".)

  4. Then click the "generate access token" button and cut/paste into the python example below in place of <auth_token>:



import dropbox

client = dropbox.client.DropboxClient(<auth_token>)
print 'linked account: ', client.account_info()

f = open('working-draft.txt', 'rb')
response = client.put_file('/magnum-opus.txt', f)
print 'uploaded: ', response

folder_metadata = client.metadata('/')
print 'metadata: ', folder_metadata

f, metadata = client.get_file_and_metadata('/magnum-opus.txt')
out = open('magnum-opus.txt', 'wb')
out.write(f.read())
out.close()
print metadata


Here's my approach using API v2 (and Python 3). I wanted to upload a file and create a share link for it, which I could email to users. It's based on sparkandshine's example. Note I think the current API documentation has a small error which sparkandshine has corrected.

import pathlib
import dropbox
import re

# the source file
folder = pathlib.Path(".")    # located in this folder
filename = "test.txt"         # file name
filepath = folder / filename  # path object, defining the file

# target location in Dropbox
target = "/Temp/"              # the target folder
targetfile = target + filename   # the target path and file name

# Create a dropbox object using an API v2 key
d = dropbox.Dropbox(your_api_access_token)

# open the file and upload it
with filepath.open("rb") as f:
   # upload gives you metadata about the file
   # we want to overwite any previous version of the file
   meta = d.files_upload(f.read(), targetfile, mode=dropbox.files.WriteMode("overwrite"))

# create a shared link
link = d.sharing_create_shared_link(targetfile)

# url which can be shared
url = link.url

# link which directly downloads by replacing ?dl=0 with ?dl=1
dl_url = re.sub(r"\?dl\=0", "?dl=1", url)
print (dl_url)

import dropbox
access_token = '************************'
file_from = 'index.jpeg'  //local file path
file_to = '/Siva/index.jpeg'      // dropbox path
def upload_file(file_from, file_to):
    dbx = dropbox.Dropbox(access_token)
    f = open(file_from, 'rb')
    dbx.files_upload(f.read(), file_to)
upload_file(file_from,file_to)