0

我没有从谷歌得到任何形式的回复。我会说请求在 99% 的情况下都会成功,但我最近开始注意到请求没有成功。它不会失败,因为没有返回指示失败的响应:没有 HTTP 状态代码,没有 XML 响应...... nada。

这可能是由于将超时设置得太低造成的吗?我的设置为10s

这是我的代码的外观:

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;
}
4

1 回答 1

1

根据请求的大小,10 秒可能不够。如果请求需要很长时间,我会从 60 秒开始超时。而且,这就是互联网。一些请求将超时并需要重试,但这应该远少于 0.001% 的时间。

于 2012-10-02T13:19:43.200 回答