phpamazon-web-servicescurlfile-uploadamazon-workdocs

AWS WorkDocs : Your socket connection to the server was not read from or written to within the timeout period. Idle connections will be closed


I know there are similar cases to my problem but my case is about uploading a file with the WorkDocs API in php.

I saw that this problem could be related to the file size but mine is only 1KB. I also saw that it could be a cURL bug (source: https://github.com/aws/aws-sdk-php/issues/29).

I call the initiateDocumentVersionUpload service and get the upload url:

$result = $client->initiateDocumentVersionUpload([
    "Name" => "test-file.txt",
    "ParentFolderId" => "***"
]);

$content = $result->get('UploadMetadata');
$url = $content['UploadUrl'];

And my curl request:

// The full path to the file that you want to upload
$filePath = 'C:/wamp64/www/test_aws/test-file.txt';

// Initiate cURL
$curl = curl_init($url);

// Set the URL
curl_setopt($curl, CURLOPT_URL, $url);

// Set the HTTP request to POST
curl_setopt($curl, CURLOPT_PUT, true);

//Tell cURL to return the output as a string.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

//Open the file using fopen.
$fileHandle = fopen($filePath, 'r');

//Pass the file handle resorce to CURLOPT_INFILE
curl_setopt($curl, CURLOPT_INFILE, $fileHandle);

//Set the CURLOPT_INFILESIZE option.
curl_setopt($curl, CURLOPT_INFILESIZE, filesize($filePath));

$headers = array(
   "Content-Type: application/octet-stream",
   "x-amz-server-side-encryption: AES256"
);

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$data = "PUT request data";

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

// for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);

And so I get the error: Your socket connection to the server was not read from or written to within the timeout period. Idle connections will be closed.

Do you know how I can fix this problem and upload my file ? Thanks in advance !


Solution

  • I've found another solution that works perfectly.

    I've add the attribut 'verify' => false to the httpClient instance to avoid SSL issue.

    $filePath = 'C:/wamp64/www/test_aws/test-file.txt';
    
    $body = fopen($filePath, 'r');
    
    $guzzle = new httpClient(['verify' => false]);
    $upload = $guzzle->put($uploadUrl, [
        'headers' => [
            "Content-Type" => "application/octet-stream",
            "x-amz-server-side-encryption" => "AES256"
        ],
        'body' => $body
    ]);
    

    Found the solution here: https://docs.aws.amazon.com/code-samples/latest/catalog/php-workdocs-UploadDocument.php.html