将 Linux (CLI) 上的文件上传到 Dropbox(通过 bash/sh)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42120767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:44:26  来源:igfitidea点击:

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

linuxbashcurldropbox-api

提问by Roger

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).

我需要通过 cron(每小时)将文件保存(并覆盖)到我的 Dropbox 帐户。该文件需要存储在预定义的位置(与其他一些用户共享)。

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

我已经看到了创建一个的可能性Dropbox App,但它会创建自己的 dropbox 文件夹。

Also looked at Dropbox Saverbut that seems for browsers.

也看过,Dropbox Saver但这似乎适用于浏览器。

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

我在想(希望)一些超轻量级的东西,很长CURL,所以我不需要安装库。只是一个简单的sh脚本会很棒。我只需要 PUT 文件(覆盖),无需读取(GET)它。

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

正在浏览 dropbox开发人员 API 文档,但有点迷路了。

Anybody a good hint?

有人有好的提示吗?

回答by Greg

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

首先,由于您需要访问现有的共享文件夹,请注册一个具有“完整 Dropbox”访问权限的“Dropbox API”应用程序:

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

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.

然后,为您的应用程序的帐户获取访问令牌。最简单的方法是使用应用程序页面上的“生成”按钮,创建应用程序后您将被发送到该页面。它也可以通过App Console访问。

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

然后,您可以通过curl如本示例所示上传到指定路径:

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

这会将文件从matrices.txt当前文件夹的本地路径上传到/Homework/math/Matrices.txtDropbox 帐户,并返回上传文件的元数据:

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.

<ACCESS_TOKEN>应替换为 OAuth 2 访问令牌。

回答by Zaccharie Ramzi

Another solution I just tried is a bash utility called Dropbox-Uploader. After configuration through the same steps as above (app creation and token generation), you can just do: ./dropbox_uploader.sh upload mylocal_file my_remote_file, which I find pretty convenient.

我刚刚尝试的另一个解决方案是名为Dropbox-Uploader的 bash 实用程序。通过与上述相同的步骤(应用程序创建和令牌生成)进行配置后,您可以执行:./dropbox_uploader.sh upload mylocal_file my_remote_file,我觉得这很方便。