Question

I'm not getting any sort of response back from google. I would say that the request succeeds 99% of the time but I've recently started noticing that the request does not succeed. It does not fail because there is no response returned to indicate a failure: no HTTP status code, no XML response ... nada.

Could this be caused by setting the timeout too low? Mine is set at 10s

Here's how my code looks:

public function putObject($objectPath, $bucket, $accessToken, $metaHeaders)
{
    $version_header =   "x-goog-api-version: 2";
    $project_header =   "x-goog-project-id: ".$this->projectID;
    $timestamp      =   date("r");

    $url = 'https://'.$bucket.'.commondatastorage.googleapis.com/object';

    $fp = fopen($objectPath, 'r'); 

    $headers    = array('Host: '.$bucket.'.commondatastorage.googleapis.com',
                    'Date: '.$timestamp, $version_header, 'Content-Type: text/plain',
                    $project_header, 'Content-Length: '.filesize($objectPath),
                    'Authorization: OAuth '.$accessToken);
    if(isset($metaHeaders))
    {
        foreach($metaHeaders as $metaHeader)
        {
            array_push($headers,$metaHeader);
        }
    }

    $c   = curl_init();
    curl_setopt($c, CURLOPT_URL, $url);
    curl_setopt($c, CURLOPT_PUT, 1);
    curl_setopt($c, CURLOPT_INFILE, $fp); 
    curl_setopt($c, CURLOPT_INFILESIZE, filesize($objectPath)); 
    curl_setopt($c, CURLOPT_HEADER, 1);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($c, CURLOPT_TIMEOUT, 10); //timeout in 10s
    curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
    $response = curl_exec($c);
    curl_close($c);
    fclose($fp);

    // split up the response into header and xml body
    list($header, $xml) = explode("\r\n\r\n", $response, 2);
    // tokenize 
    $status = strtok($header, "\r\n"); 

    if(stristr($status,"200 OK"))
    {
        //success
        $result = "success";
        //check xml object for specific bucket creation errors

    }
    else
    {
        //failed
        $result = "fail";
        //check xml object for specific bucket creation errors

    }

    return $result;
}
Was it helpful?

Solution

10 seconds may not be enough depending on the size of the request. I start my timeout at 60 seconds an increase it if requests are taking a long time. And, this is the Internet. Some requests will timeout and need to be retried, but this should be much less that 0.001% of time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top