Search code examples
phpcurlamazon-s3vimeo-apiphp-stream-wrappers

PHP CURL PUT from Amazon S3 stream wrapper (tcp stream)


I'm trying to (PHP) cURL PUT a file from Amazon S3 to Vimeo using S3 stream wrapper (s3://...) and getting the follow error:

curl_setopt_array(): cannot represent a stream of type tcp_socket/ssl as a STDIO FILE in [...]

Is there a way to cURL PUT a remote file? This are the curl opts being sent:

$curl_opts = array(
  CURLOPT_PUT => true,
  CURLOPT_INFILE => $file, // this refers to 's3://path-to-object'
  CURLOPT_INFILESIZE => $size,
  CURLOPT_UPLOAD => true,
  CURLOPT_HTTPHEADER => array('Expect: ', 'Content-Range: replaced...')
);

Solution

  • Save the file to your local server before PUTting it:

    $file = tempnam(sys_get_temp_dir(), "foo");
    $data = file_get_contents("s3://path-to-object");
    $size = strlen($data);
    file_put_contents($file, $data);
    $curl_opts = array(
      CURLOPT_PUT => true,
      CURLOPT_INFILE => $file, // this refers to 's3://path-to-object'
      CURLOPT_INFILESIZE => $size,
      CURLOPT_UPLOAD => true,
      CURLOPT_HTTPHEADER => array('Expect: ', 'Content-Range: replaced...')
    );