Raspberry Pi Dropbox Integration - SB Components

Raspberry Pi Dropbox Integration

0 comments

This article aims at using the Dropbox SDK for python on Raspberry. This generation prefers to keep all the data handy DVDs, pen drive, Bluetooth is old fashion. Everyone is moving to cloud storage whether it is an individual or organization, then why should our devices depend on physical memory? We can store any datalog, images, videos or any other data directly to the cloud. 

In this article, we’ll upload and download a file to and from dropbox using python on Raspberry Pi. You can follow these steps on any other device like windows, mac, or Linux.

Things You Need

  • Raspberry Pi
  • Working Internet Connection
  • Dropbox account

The Initial Setup

Open Dropbox Account:

  • Sign In or Sign up into https://www.dropbox.com/ 
  • Go App Console https://www.dropbox.com/developers/apps, and click on ‘Create App’

SB Components

  • Select the Dropbox API, type of access you need, and the name of your app. My app name is ‘MyApp1010’. Hit ‘Create App’ when done

SB Components

  • You will be redirected to your apps settings. Click on Generate Access Token.

SB Components

    • Copy the generated token to a text file. In my case, this file’s name is key.txt.

    Raspberry Pi Setup:

    • Power up the Raspberry Pi and get access to the Desktop or Terminal.
    • Connect the Raspberry Pi with an internet connection.
    • Via ‘Terminal’ install dropbox python SDK using pip as-

                        pip3 install dropbox

Follow these instructions: 

https://www.dropbox.com/developers/documentation/python?_tk=pilot_lp&_ad=sdk6&_camp=python#install

Code

After the Dropbox account is set and Raspberry Pi is ready with the dropbox module installed.

Create a file with the name of your choice. Import the dropbox module-

import dropbox

Create an instance of the Dropbox object. To instantiate, pass in the access token for the account you want to link-

dbx = dropbox.Dropbox('YOUR_ACCESS_TOKEN')

Or 

Read the key from the text file. You can also encrypt the key for more security.

with open("key.txt") as key:

    k = key.readline()

    Access_Token = k.split()[0]

 

# instance of the Dropbox object

dbx = dropbox.Dropbox(Access

_Token)

 

To check the account you have connected to use users_get_current_account() function:

                                  dbx.users_get_current_account()

It will return the app metadata.

The following code lists the directories, Upload a file to your dropbox account associated app, downloads a file, and creates a folder on dropbox directory.

import dropbox

# key extraction

with open("key.txt") as key:

   k = key.readline()

   Access_Token = k.split()[0]

 

# instance of the Dropbox object

dbx = dropbox.Dropbox(Access_Token)

 

if __name__ == "__main__":

   # Test it out to make sure you've linked the right account

   dbx.users_get_current_account()

 

   # List all of the contents in the directory

   # use '' for user's root Dir

   dirName = "/MyDir"

   for entry in dbx.files_list_folder(dirName).entries:

       print(entry.name)

 

   # Upload a file

   # filename = 'TempFile.txt'

   dropbox_Path = '/' + filename

   with open(filename, 'rb') as file:

       # Dropbox Write modes

       # files.WriteMode.add

       # files.WriteMode.overwrite

       # files.WriteMode.update

       file_ = file.read()

       FileMetadata = dbx.files_upload(file_, path=dropbox_Path,

                                       mode=dropbox.files.WriteMode.overwrite,

                                       autorename=True)

       print(FileMetadata)

 

   # Create Folder

   dir_Path = '/NewFolder'

   dir_Metadata = dbx.files_create_folder(dir_Path, autorename=True)

   print(dir_Metadata)

 

   # Download File

   download_Path = "home/pi/Downloads"

   dropbox_Path = '/TempFile.txt'

   Metadata = dbx.files_download_to_file(download_Path, dropbox_Path)

   print(Metadata)

You can use different methods to use and control your Dropbox account. Add any sensor logs, Images or videos to your Dropbox account via a python script. 

Follow the dropbox for python documentation for accessing more features of dropbox module from your python module: https://dropbox-sdk-python.readthedocs.io/en/latest/index.html

Conclusion

You can also use APIs provided by dropbox, the steps to configure that may vary but it will be fun. This module provided by dropbox has everything you will need to manage and control your Dropbox account. This module is made for integrating your apps directly to dropbox. Enjoy using cloud storage on your Raspberry Pi log sensor data from the Raspberry Pi or create your own spy box which synchronizes everything to your dropbox.

 

Hope this article helped you in Raspberry Pi Dropbox Integration.

Check our trending products below: 

Micro bit Starter kit
Buy Raspberry Pi 4 Clear Cases


Auto-Run Python Program on Raspberry Pi Startup

Voice Control Home Automation using PiRelay

Leave a comment

Please note, comments need to be approved before they are published.