Upload file via CURL to HDFS

I am still trying to upload a file to HDFS via cURL. I tried this before in the cURL CLI and it worked, but when I tried to create PHP code that runs the same command, it wonโ€™t want to load.

Here is the command:

curl -i -X PUT -T "c:/xampp/htdocs/experiment/csv.php" "http://123.123.78.9:14000/webhdfs/v1/user/flume/test2?op=CREATE&buffersize=1024&user.name=flume&blocksize=1048576&overwrite=false&data=true&permission=755" --header "Content-Type: application/octet-stream"

And here is the PHP code:

function call_curl($headers, $method, $url, $data)
{           
    $handle = curl_init();
    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
    curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);

    switch($method) {
            case 'GET':
                break;
            case 'POST':
                curl_setopt($handle, CURLOPT_POST, true);
                curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
                break;
            case 'PUT': 
                curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
                curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
                break;
            case 'DELETE':
                curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
                break;
           }

    $response = curl_exec($handle);
    $code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
    curl_close($handle);
    return $response;
}

$header = array('Content-Type: application/octet-stream');
$method = "PUT";
$url =  "http://123.123.78.9:14000/webhdfs/v1/user/flume/filter/".$target_file."? op=CREATE&overwrite=true&blocksize=1048576&permission=777&buffersize=1024&user.name=flume"; 
$data = array('@c:/xampp/htdocs/experiment/'.$target_file);
$call_2 = call_curl($header, $method, $url, $data);
+4
source share

Source: https://habr.com/ru/post/1617193/


All Articles