当从其他URL上传文件而不保存到本地时,保留Content-Type



给定许多文件(或一个大文件),我想从一台服务器下载并上传到另一台服务器,而不将文件存储在本地。现在,我使用:

wget -q -O - <source> | curl --silent --show-error --fail -X PUT -d @- <destination>

,但是这会用默认的Content-Type: application/x-www-form-urlencoded上传文件。是否有保留原始Content-Type的方法?

使用curl设置标题,例如,-H 'Content-Type: text/html':

cat 1.html | curl --silent --show-error --fail -X PUT -H 'Content-Type: text/html' -d @- localhost:8000

,在服务器上你得到:

Content-Type: text/html

通常(与浏览器一样),文件上传需要method=post和enctype="multipart/form-data"然后可以为每个部分设置Content-Type。对于curl,你可以这样做:

curl ... -F "file=@-;type=text/html" <destination>

在服务器上你会得到:

Content-Type: multipart/form-data; boundary=------------------------2fa1094be9afdaa6
...
--------------------------2fa1094be9afdaa6
Content-Disposition: form-data; name="file"; filename="-"
Content-Type: text/html
...

需要从wget报头中提取相关的内容类型。也许可以使用——save-headers选项?首先将内容读取到变量file中,将头字段值提取到变量content_type中,然后将除头之外的所有内容传递给curl,现在可以使用上面的$content_type per调用curl:

wget ... | { file=$(</dev/stdin); content_type=$(echo "$file" | sed -n '1,/^r$/ { /^Content-Type: /{ s/.*: (.*)/1/p; q } }'); echo "$file" | sed '1,/^r$/d' | curl ... }

最新更新