upload file to my dropbox from python script upload file to my dropbox from python script python python

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 dropboxclass 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 dropboxclient = 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: ', responsefolder_metadata = client.metadata('/')print 'metadata: ', folder_metadataf, 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 pathlibimport dropboximport re# the source filefolder = pathlib.Path(".")    # located in this folderfilename = "test.txt"         # file namefilepath = folder / filename  # path object, defining the file# target location in Dropboxtarget = "/Temp/"              # the target foldertargetfile = target + filename   # the target path and file name# Create a dropbox object using an API v2 keyd = dropbox.Dropbox(your_api_access_token)# open the file and upload itwith 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 linklink = d.sharing_create_shared_link(targetfile)# url which can be sharedurl = link.url# link which directly downloads by replacing ?dl=0 with ?dl=1dl_url = re.sub(r"\?dl\=0", "?dl=1", url)print (dl_url)