Table of Contents
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 organisation, 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 devices like windows, mac, or Linux.
Things You Need
- Raspberry Pi 4
- 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’
- Select the Dropbox API, type of access you need, and name of your app. My app name is ‘MyApp1010’. Hit ‘Create App’ when done
- You will be redirected to your apps settings. Click on Generate Access Token.
- Copy the generated token to a text file. In my case, this file’s name is key.txt.
Also check new Micro bit Collections (Stem Bit – The Programmable blocks kit for micro:bit)
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 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.