2012-10-02 60 views
0

我沒有收到任何來自谷歌的迴應。我會說99%的請求成功,但我最近開始注意到請求沒有成功。它不會失敗,因爲沒有響應返回來表示失敗:沒有HTTP狀態碼,沒有XML響應... nada。什麼可能導致PUT請求在Google雲端存儲上失敗?

這可能是由於設置超時時間過低造成的?煤礦被設置爲10秒

這裏是我的代碼的外觀:

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

回答

1

10秒可能沒有足夠的根據請求的大小。如果請求花費很長時間,我會在60秒後開始超時。而且,這是互聯網。有些請求會超時並需要重試,但這應該比0.001%的時間少得多。

+0

感謝您的回覆Maldred。請求的大小几乎不超過300字節,這就是爲什麼我將超時設置爲10秒的原因。但我會聽取您的建議並實施指數退避,然後重試失敗。 –

相關問題