Uploading files using Dropbox-API in Python

This post is all about how to upload files to Dropbox using Dropbox-API in Python. Dropbox is a cloud storage service. It is free and a basic Dropbox account has 2GB of storage space. Check out the dropbox documentation here.

To those of you wondering where this API can be used, consider the following example: Suppose you want to make a Surveillance Application. And, you want to upload the pictures captured to the cloud in real-time.
For uploading these files, we can use the Dropbox-API in Python. We are using Python because it already has a built-in library for Dropbox.

Setting up Dropbox account:

Firstly, we need to create a Dropbox account here.
how to upload files to Dropbox using Dropbox-API in Python

Click on ‘Create apps’ and create your account.

After your account is created successfully, you’ll need to provide the specifications of your app, like the type of dropbox you require, the app name, etc.
Uploading files using Dropbox-API in Python

Then click on ‘Create App’.

Now you’ll be redirected to a page with your Application Key, access token, etc.
Dropbox Application Key

Finally, copy your access token and we’re all set to write our code.

Installing Dropbox library for Python:

Python provides a built-in library for dropbox. We will now see how to install this library. Type the following command into the command line. And, that’s it!

pip install dropbox

Writing the Python code: Upload file using Dropbox API

First, we import the dropbox library.

import dropbox

Next, we initialize different parameters. They include the Dropbox access token, which you got after creating your account. Then, the dropbox path, which is the path where you want to save your file. Lastly, the computer path, which is the path on your computer where the file is saved.

dropbox_access_token= "<Your_Access_Token>"    #Enter your own access token
dropbox_path= "/Cat Watching Test/cat1.jpg"

After this, we need to link the our dropbox account to the application. Hence, we create a client object.

client = dropbox.Dropbox(dropbox_access_token)
print("[SUCCESS] dropbox account linked")

Finally, we can upload the file from the computer path to the dropbox path. We first open the file in “rb” mode and read its contents. Then we upload these contents to the dropbox using the files_upload() method.

client.files_upload(open(computer_path, "rb").read(), dropbox_path)
print("[UPLOADED] {}".format(computer_path))

And We’re Done!


[SUCCESS] dropbox account linked
[UPLOADED] C:/Users/snigd/Downloads/IMG_20151008_210540_1444318623808.jpg
We can finally open the dropbox app now to check if the file is in fact uploaded.
open the dropbox Python

You can use the Dropbox API with many OpenCV programs to upload the frames captured to the cloud. To do this, you just need to include the above code to the loop which captures each frame. You can choose to only upload certain frames by adding an if condition before the above code is added to the program.

Check out OpenCV programs to apply this cool Dropbox addition to it:

One response to “Uploading files using Dropbox-API in Python”

  1. Bilal says:

    Hello,I tried this code but its not working for me. Its very confusing that what is the access token and also dropbox path is confusing me . So please can you help me with this?
    I will send you my program. can u explain that what is access token and how i put in my program?

Leave a Reply

Your email address will not be published. Required fields are marked *