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"
computer_path="C:/Users/snigd/Downloads/IMG_20151008_210540_1444318623808.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!

Output:

[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:

Leave a Reply

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