1

我想使用 php 将一个大文件卷曲到 Hoop。如果我进行正常的 php 文件上传,则文件中会附加标头。

当我尝试这个时:

$url = http://hoop:14000/filename?op=create&user.name=root
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => "@" . $this->filepath));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/octet-stream', 'Expect:'));
$content  = curl_exec($ch);

箍上的文件将具有以下标题:

------------------------------f0f063939ed8
Content-Disposition: form-data; name="file"; filename="phpbsA4ty"
Content-Type: application/octet-stream
{binary data here........}

我猜它需要是原始的帖子数据。所以我可以让它像这样工作:

    $url = http://hoop:14000/filename?op=create&user.name=root
    $fileData = file_get_contents($this->filepath);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fileData);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/octet-stream', 'Expect:'));
    $content  = curl_exec($ch);

但是大文件会导致内存错误:

PHP Fatal error:  Allowed memory size of 33554432 bytes exhausted (tried to allocate 8388608 bytes)

有没有办法在不将文件加载到内存的情况下发布原始文件?

我可以使用 Hoop 文档中的说明在命令行上执行此操作

curl -X POST -c ~/.hoopauth "http://<HOOP_HOST>:14000/<PATH>?op=create[&<OPTION>]*" \ --data-binary @data.txt  --header "content-type: application/octet-stream"

来自 http://cloudera.github.com/hoop/docs/latest/HttpRestApi.html

4

0 回答 0