Raspberry Pi Dropbox, Upload Files to Dropbox
Table of Contents
Upload files to Dropbox:
Raspberry Pi Dropbox- Especially in connection with the Raspberry Pi camera, there is often a desire to have automatically uploaded a photo or video file to your own Dropbox account. Various Python scripts can be found on the Internet to make this task straightforward pass the Dropbox API. Unfortunately, it has been shown that such solutions do not function stably over the long term. That is why we explain the “official “, The way recommended by Dropbox.
The basic requirement is of course that you have your own Dropbox account. On the Dropbox developer pages, set up the so-called “App Console” a new Dropbox app:
https://www.dropbox.com/developers/apps
For this example we have given the app the name rapi-uploader. In the Settings page of this app you will now find the Generate button to set an app-specific Generate code (a token) (see Figure1).
Amazon Purchase Links:
Wireless Keyboard and Mouse for raspberry pi:
Night vision Camera for Raspberry Pi:
Oled HDMI touch display for raspberry pi:
Other Tools and Components:
Super Starter kit for Beginners
PCB small portable drill machines
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
The Python functions for communicating with Dropbox are in one Extension package that is easiest to install with pip. The command name pip stands for Pip Installs Python and is its own package management system for Python. We set here to match the Python version available in Raspbian
1 2 3 4 5 |
3.2 pip-3.2 sudo apt - get install python3-pip sudo pip -3.2 install dropbox |
With the previously generated token, we can now test whether the connection is being established to Dropbox succeeds. The following mini-script should provide information about your Dropbox account:
1 2 3 4 5 6 7 |
#! / usr / bin / python3 import dropbox dbc = dropbox. client. DropboxClient ('xxx -token -code - xxx') print ('Account infos:', dbc.account_info ()) |
If everything works out, it’s time for the first upload. The following lines load the File test.jpg from the local directory into the root directory of your Dropbox Accounts high:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#! / usr / bin / python3 # Dropbox - upload.py file import dropbox dbc = dropbox. client. DropboxClient ('xxx -token -code - xxx') fname = 'test .jpg' # open file, f = open (fname, 'rb') response = dbc. put_file (fname, f) # ... upload print ('uploaded:', respone) close () # ... and close |
Universal Dropbox authentication
We have assumed here that your script will only work with your own Dropbox account i.e it will only communicate with your Dropbox account. The starting position is completely different when you use a script Develop to share with other users, each with their own Dropbox accounts.
In this case, you have to include code in the script that gives the user the possibility, the ability to authenticate the respective Dropbox access is directed to a Dropbox page, you have to enter your Dropbox password and then receive a special code for the script. Your script asks you to enter the code and saves it so that it can be accessed again later.
Upload photo with date and time information
The second example is a little more practical: It first creates a photo and loads it then up in the Dropbox directory. The filename of the photo under Dropbox is set composed of rapi and the date, e.g. Rapi 2020-01-25.jpg.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#! / usr / bin / python3 # File dropbox - photo .py import datetime, dropbox, picamera dbc = dropbox. client. DropboxClient ('xxx -token -code - xxx') # Create a photo and save it locally localname = 'tmp. jpg ' camera = picamera. PiCamera () camera. resolution = (1280, 960) camera. capture (localname) camera. close () # Upload a photo f = open (localname, 'rb') today = datetime. date. today () upname = today. strftime ('Rapi -% Y -% m -% d. jpg') dbc. put_file (upname, f) close () |