---
# System prepended metadata

title: Use the Linux shell script to upload/download/delete the Dropbox files
tags: [Linux]

---

# Use the Linux shell script to upload/download/delete the Dropbox files

Dropbox 有提供API來作上傳/下載/刪除 ...等操作。所以此篇文章是參考了 Github 上 [Dropbox Uploader](https://github.com/andreafabrizi/Dropbox-Uploader) 的專案來玩玩看。

PS:Dropbox API 有v1和v2，Dropbox Uploader專案已是v2。


## 安裝

Step 01: 先下載 Dropbox Uploader

- 可以使用git的方式下載整個Dropbox uploader專案來取得腳本(`dropbox_uploader.sh`)
```shell=
$ git clone https://github.com/andreafabrizi/Dropbox-Uploader
```

- 或是使用curl來下載Dropbox uploader腳本(dropbox_uploader.sh)
```shell=    
$ curl "https://raw.githubusercontent.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh" -o dropbox_uploader.sh
```

在dropbox_uplader.sh中，dropbox登入的參數，預設會儲存在配置檔案`~/.dropbox_uploader`。這個檔案會在執行dopbox_uploader時自動產生，我們待會會用到這個檔案。
```
#Default configuration file
CONFIG_FILE=~/.dropbox_uploader
```

Step 02: 開啟 Dropbox Developer API
使用瀏覽器打開`https://www.dropbox.com/developers/apps`，登入dropbox，點擊`Create app`。新建一個應用專案。

![](https://i.imgur.com/0ecDvow.png)

如下圖選擇和填入對應的資訊，點擊 `Create app`
- **Choose an API** 中選擇API的模式(對應不同的有效操作範圍)，有2種模式：
    - App folder: 限制在一個專屬於這個app所使用folder下。
    - Full Dropbox： 整個Dropbox範圍

建議選用`App folder`模式比較安全。

- **Name your app** 為此App專案取名。屆時會在`Dropbox/應用程式/`下新增一個同名的folder。
![](https://i.imgur.com/EAmVXwG.png)
![](https://i.imgur.com/qr98umq.png)

新增成功的畫面，取得API的
- App key: kmg07gljaah66q0
- App secret: v25pltlop6ka2in
![](https://i.imgur.com/mCNr3QF.png)

Step 03: 設定API細部權限(Permission)

如下圖，本次是設定檔案目錄的相關操作權限(read/write)，設定好權限，點擊`Submit`
![](https://i.imgur.com/EqQVU4w.png)

Step 04: 執行dropbox_uploader.sh腳本，第一次執行(腳本沒有抓到設定檔`CONFIG_FILE=~/.dropbox_uploader`)會有提示，讓我們一步一步輸入所需資訊並取得Dropbox驗證

![](https://i.imgur.com/rs02wwf.png)
- 輸入`App key`和`App secret`
- 腳本會產生一個網址，使用瀏覽器打開此網址，取得Access Code，填入Access Code。

![產生Access code](https://i.imgur.com/F062qTc.png)
![填入Access code](https://i.imgur.com/jIWB5Pk.png "填入Access Code")

完成


## 檢驗
- 設定檔會自動建立在`CONFIG_FILE=~/.dropbox_uploader`，dropbox_uploader是用此token config去進行操作。
PS: 也可以加上 -f <file_path> 指定不同的token config來作不同的操作

```shell=bash
$ cat ~/.dropbox_uploader
CONFIGFILE_VERSION=2.0
OAUTH_APP_KEY=kmg07gljaah66q0
OAUTH_APP_SECRET=v25pltlop6ka2in
OAUTH_REFRESH_TOKEN=lVXGfajISOMAAAAAAAAAAYo0XdnLU9mzbPMcP79gABYp8E2X4J6WJtWwB7pjr2Y3
```

- 執行`./dropbox_uploader info`查驗是否能正常執行，產生正確的輸出。
```shell=bash
./dropbox_uploader.sh info
Dropbox Uploader v1.0

 > Getting info...

Name:           XXXXXXXXX
UID:            XXXXXXXXX
Email:          XXXXXXXXX
Country:        TW

```

## 使用

```shell=bash
$ ./dropbox_uploader.sh -h
Dropbox Uploader v1.0
Andrea Fabrizi - andrea.fabrizi@gmail.com

Usage: ./dropbox_uploader.sh [PARAMETERS] COMMAND...

Commands:
         upload   <LOCAL_FILE/DIR ...>  <REMOTE_FILE/DIR>
         download <REMOTE_FILE/DIR> [LOCAL_FILE/DIR]
         delete   <REMOTE_FILE/DIR>
         move     <REMOTE_FILE/DIR> <REMOTE_FILE/DIR>
         copy     <REMOTE_FILE/DIR> <REMOTE_FILE/DIR>
         mkdir    <REMOTE_DIR>
         list     [REMOTE_DIR]
         monitor  [REMOTE_DIR] [TIMEOUT]
         share    <REMOTE_FILE>
         saveurl  <URL> <REMOTE_DIR>
         search   <QUERY>
         info
         space
         unlink

Optional parameters:
        -f <FILENAME> Load the configuration file from a specific file
        -s            Skip already existing files when download/upload. Default: Overwrite
        -d            Enable DEBUG mode
        -q            Quiet mode. Don't show messages
        -h            Show file sizes in human readable format
        -p            Show cURL progress meter
        -k            Doesn't check for SSL certificates (insecure)
        -x            Ignores/excludes directories or files from syncing. -x filename -x directoryname. example: -x .git

For more info and examples, please see the README file.

```


常用的功能

**mkdir/list**

```shell=bash
$ ./dropbox_uploader.sh list
> Listing "/"... DONE

$ ./dropbox_uploader.sh mkdir test1
> Creating Directory "/test1"... DONE

$ ./dropbox_uploader.sh list
> Listing "/"... DONE
[D]  test1

$ ./dropbox_uploader.sh mkdir test1/test11
> Creating Directory "/test1/test11"... DONE
                                                             
$ ./dropbox_uploader.sh list test1
> Listing "/test1"... DONE
[D]  test11

```


**upload**
```shell=bash
$ ./dropbox_uploader.sh upload ~/.dropbox_uploader /
> Uploading "/home/mobaxterm/.dropbox_uploader" to "/.dropbox_uploader"... DONE

$ ./dropbox_uploader.sh list
> Listing "/"... DONE
[D]     test1
[F] 171 .dropbox_uploader
```

**search**
```shell=bash
$ ./dropbox_uploader.sh search uploader
> Searching for "uploader"... DONE
[F] 171 /.dropbox_uploader

```

**delete**
```shell=
$ ./dropbox_uploader.sh delete test1/test11
> Deleting "/test1/test11"... DONE

$ ./dropbox_uploader.sh list test1
> Listing "/test1"... DONE
```

**-d**
打開debug mode, 如果有錯誤的話，會紀錄在logfile: /tmp/du_resp_debug

```
$ ./dropbox_uploader.sh -d list test11
1.0
CYGWIN_NT-10.0-WOW MSI 3.0.4(0.338/5/3) 2019-03-18 19:35 i686 GNU/Linux
...(snip)

+ echo 'Some error occured. rerun the script with "-d" option and check the output and logfile: /tmp/du_resp_debug.'
Some error occured. rerun the script with "-d" option and check the output and logfile: /tmp/du_resp_debug.
+ exit 1
```

**-f**
指定不同的token config來操作。如果有多個app專案可利用此項來各別操作。


# 參考資料：
https://www.dropbox.com/developers
https://github.com/andreafabrizi/Dropbox-Uploader