如何有效地使用 curl -Z (--parallel)?

How to use curl -Z (--parallel) effectively?

我需要使用 curl 下载数千个文件。我知道如何与 xargs -Pn(或 gnu parallel)并行化,但我刚刚发现 curl 本身可以使用 curl-7.66[= 中引入的参数 -Z|--parallel 并行化下载31=] (see curl-goez-parallel) 可能更清晰或更易于共享。 我需要使用 -o|--output 选项和 --create-dirs。 URLs 需要 percent-encoded,URL 路径成为文件夹路径,也需要转义,因为路径可以包含单引号、空格和通常的嫌疑人(因此 -O option 不安全并且 -OJ option 没有帮助)。 如果我理解得很好,curl 命令应该像这样构建:

curl -Z -o path/to/file1 http://site/path/to/file1 -o path/to/file2 http://site/path/to/file2 [-o path/to/file3 http://site/path/to/file3, etc.]

这确实有效,但处理数千 URLS 的最佳方法是什么。与 -K config 一起使用的 config 文件是否有用?如果 -o path/to/file_x http://site/path/to/file_x 是另一个程序的输出怎么办?我还没有找到任何方法在文件中记录命令,比如每行一个命令。

谢谢

我最后问了 curl user mailing list

对我有用的解决方案是以编程方式构建的配置(纯文本)文件:

config.txt:

url=http://site/path/to/file1
output="./path/to/file1"
url=http://site/path/to/file2
output="./path/to/file2"
...

并使用此 curl 命令:

$ curl --parallel --parallel-immediate --parallel-max 60 --config config.txt

该命令比这更复杂,需要最新的 curl 版本 (7.81+)

$ curl --parallel --parallel-immediate --parallel-max 60 --config config.txt \
  --fail-with-body --retry 5 --create-dirs \
  --write-out "code %{response_code} url %{url} type %{content_type}\n"

有关 -K、--config 等选项,请参阅 https://curl.se/docs/manpage.html

HTH