Upload file on Linux (CLI) to Dropbox (via bash/sh)?

Roger picture Roger · Feb 8, 2017 · Viewed 15.5k times · Source

I need to save (and overwrite) a file via the cron (hourly) to my dropbox account. The file needs to be stored in a predefined location (which is shared with some other users).

I have seen the possibility to create a Dropbox App, but that create its own dropbox folder.

Also looked at Dropbox Saver but that seems for browsers.

I was thinking (hoping) something super lightweight, a long the lines of CURL, so i don't need to install libraries. Just a simple sh script would be awesome. I only need to PUT the file (overwrite), no need to read (GET) it back.

Was going thru the dropbox developer API documentation, but kind of got lost.

Anybody a good hint?

Answer

Greg picture Greg · Feb 8, 2017

First, since you need to access an existing shared folder, register a "Dropbox API" app with "Full Dropbox" access:

https://www.dropbox.com/developers/apps/create

Then, get an access token for your account for your app. The easiest way is to use the "Generate" button on your app's page, where you'll be sent after you create the app. It's also accessible via the App Console.

Then, you can upload to a specified path via curl as shown in this example:

This uploads a file from the local path matrices.txt in the current folder to /Homework/math/Matrices.txt in the Dropbox account, and returns the metadata for the uploaded file:

echo "some content here" > matrices.txt

curl -X POST https://content.dropboxapi.com/2/files/upload \
    --header "Authorization: Bearer <ACCESS_TOKEN>" \
    --header "Dropbox-API-Arg: {\"path\": \"/Homework/math/Matrices.txt\"}" \
    --header "Content-Type: application/octet-stream" \
    --data-binary @matrices.txt

<ACCESS_TOKEN> should be replaced with the OAuth 2 access token.