使用cURL从shell POST 4GB文件

a9wyjsp7  于 7个月前  发布在  Shell
关注(0)|答案(4)|浏览(76)

我尝试将文件大小为4 GB的文件发布到REST API。
而不是上传一个文件与此大小,cURL张贴一个文件与内容长度:0.

curl -v -i -d @"/work/large.png" -H "Transfer-Encoding: chunked" http://localhost:8080/files
* Adding handle: conn: 0x7fcafc00aa00
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7fcafc00aa00) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 8080 (#0)
*   Trying localhost...
* Connected to localhost (localhost) port 8080 (#0)
> POST /files HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:8080
> Accept: */*
> Transfer-Encoding: chunked
> Authorization: bearer XXX.XXX.XXX
> x-user-token: bearer XXX.XXX.XXX
* upload completely sent off: 5 out of 0 bytes
< HTTP/1.1 201 Created
HTTP/1.1 201 Created
< Date: Thu, 02 Jan 2014 14:55:46 GMT
Date: Thu, 02 Jan 2014 14:55:46 GMT
< ETag: "d41d8cd98f00b204e9800998ecf8427e"
ETag: "d41d8cd98f00b204e9800998ecf8427e"
< Location: http://localhost:8080/files/66032e34-9490-4556-8495-fb485ca12811
Location: http://localhost:8080/files/66032e34-9490-4556-8495-fb485ca12811
* Server nginx/1.4.1 is not blacklisted
< Server: nginx/1.4.1
Server: nginx/1.4.1
< Content-Length: 0
Content-Length: 0
< Connection: keep-alive
Connection: keep-alive

使用较小大小的文件将按预期工作。

-rw-r--r--  1 user1  wheel  4403200000  2 Jan 15:02 /work/large.png

为什么上传失败?如何正确上传这样的文件?
干杯

irtuqstp

irtuqstp1#

我认为你应该考虑使用-T选项而不是--data-binary--data-binary将整个文件加载到内存中(curl 7.47)。最好的情况是它很慢,最坏的情况是OOM杀手会回复一条Killed消息。

curl -XPOST -T big-file.iso https://example.com
vdgimpew

vdgimpew2#

要使用CURL上传大型二进制文件,您需要使用--data-binary标志。
在我的案例中,它是:

curl -X PUT --data-binary @big-file.iso https://example.com

**注意:**这实际上是@KarlC评论的扩展版本,这实际上是正确的答案。

w9apscun

w9apscun3#

是否验证连接未超时?检查CURLOPT_POSTFIELDS has a length or size limit?检查Can't post data to rest server using cURL with content length larger than 1MB
但根据我的研究,我只能说,问题出在服务器端。现在它可能是内存问题(缓冲区大小相关),超时问题.这在很大程度上取决于您在服务器端使用的平台。所以,提供一些关于服务器端的细节和一些日志输出...特别是尝试捕获错误日志。

raogr8fs

raogr8fs4#

在我的情况下, curl 消耗了太多的内存,因为文件大小.我注意到问题的答案是(可能是curl或bash中的内存泄漏?),我通过将curl输出定向到一个文件来解决这个问题:

curl {{command arguments and url}} > curl_response.data

解决了curl占用太多内存的问题。

相关问题