bash 使用 curl 通过共享链接(非公共链接)下载 Dropbox 文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21322614/
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
Use curl to download a Dropbox folder via shared link (not public link)
提问by USCFan13
Dropbox makes it easy to programmatically download a single file via curl (EX: curl -O https://dl.dropboxusercontent.com/s/file.ext
). It is a little bit trickier for a folder (regular directory folder, not zipped). The shared link for a folder, as opposed to a file, does not link directly to the zipped folder (Dropbox automatically zips the folder before it is downloaded). It would appear that you could just add ?dl=1
to the end of the link, as this will directly start the download in a browser. This, however, points to an intermediary html document that redirects to the actual zip folder and does not seem to work with curl. Is there anyway to use curl to download a folder via a shared link? I realize that the best solution would be to use the Dropbox api, but for this project it is important to keep it as simple as possible. Additionally, the solution must be incorporated into a bash shell script.
Dropbox 可以轻松地通过 curl (EX:) 以编程方式下载单个文件curl -O https://dl.dropboxusercontent.com/s/file.ext
。对于文件夹(常规目录文件夹,未压缩)来说有点棘手。文件夹的共享链接(与文件相反)不会直接链接到压缩文件夹(Dropbox 会在下载之前自动压缩文件夹)。看起来你可以添加?dl=1
到链接的末尾,因为这将直接在浏览器中开始下载。但是,这指向一个中间 html 文档,该文档重定向到实际的 zip 文件夹并且似乎不适用于 curl。反正有没有使用 curl 通过共享链接下载文件夹?我意识到最好的解决方案是使用 Dropbox api,但对于这个项目,尽可能保持简单很重要。此外,该解决方案必须合并到 bash shell 脚本中。
回答by USCFan13
It does appear to be possible with curl by using the -L
option. This forces curl to follow the redirect. Additionally, it is important to specify an output name with a .zip extension, as the default will be a random alpha-numeric name with no extension. Finally, do not forget to add the ?dl=1
to the end of the link. Without it, curl will never reach the redirect page.
通过使用该-L
选项,curl 似乎是可能的。这会强制 curl 跟随重定向。此外,指定一个带有 .zip 扩展名的输出名称很重要,因为默认值将是一个没有扩展名的随机字母数字名称。最后,不要忘记?dl=1
在链接的末尾添加。没有它,curl 永远不会到达重定向页面。
curl -L -o newName.zip https://www.dropbox.com/sh/[folderLink]?dl=1
回答by bshea
Follow redirects (use
-L
). Your immediate problem is that Curl is not following redirects.Set a filename. (Optional)
- Dropbox already sends a Content-Disposition Headerwith its Dropbox filename.
There is no reason to specify the filename if you use the correct curl flags. - Conversely, you can force a filename using something of your choosing.
- Dropbox already sends a Content-Disposition Headerwith its Dropbox filename.
按照重定向(使用
-L
)。您当前的问题是 Curl 没有遵循重定向。设置文件名。(可选的)
- Dropbox 已经发送了一个带有 Dropbox 文件名的Content-Disposition Header。
如果您使用正确的 curl 标志,则没有理由指定文件名。 - 相反,您可以使用您选择的内容强制使用文件名。
- Dropbox 已经发送了一个带有 Dropbox 文件名的Content-Disposition Header。
Use one of these commands:
使用以下命令之一:
curl https://www.dropbox.com/sh/AAbbCCEeFF123?dl=1 -O -J -L
Preserve/write the remote filename (-O
,-J
) and follows any redirects (-L
).
保留/写入远程文件名 ( -O
, -J
) 并遵循任何重定向 ( -L
)。
- This same line works for both individually shared files or entire folders.
- Folders will save as a
.zip
automatically (based on folder name). - Don't forget to change the parameter
?dl=0
to?dl=1
(see comments).
- 同一行适用于单独共享的文件或整个文件夹。
- 文件夹将
.zip
自动另存为(基于文件夹名称)。 - 不要忘记将参数更改
?dl=0
为?dl=1
(见评论)。
OR:
或者:
curl https://www.dropbox.com/sh/AAbbCCEeFF123?dl=1 -L -o [filename]
Follow redirect (-L
) and sets a filename (-o
) of your choosing.
按照重定向 ( -L
) 并设置-o
您选择的文件名 ( )。
NOTE: Using the -J
flag in general:
注意:-J
一般使用标志:
WARNING: Exercise judicious use of this option, especially on Windows. A rogue server could send you the name of a DLL or other file that could possibly be loaded automatically by Windows or some third party software.
警告:谨慎使用此选项,尤其是在 Windows 上。恶意服务器可能会向您发送 DLL 或其他文件的名称,这些文件可能会被 Windows 或某些第三方软件自动加载。
Please consult: https://curl.haxx.se/docs/manpage.html#OPTIONS(See: -O, -J, -L, -o) for more.
请查阅:https: //curl.haxx.se/docs/manpage.html#OPTIONS(参见:-O、-J、-L、-o)了解更多信息。