什么可能导致PUT请求在Google云端存储上失败?

时间:2012-10-02 05:09:10

标签: php rest google-cloud-storage

我没有从谷歌那里得到任何回复。我会说这个请求在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;
}

1 个答案:

答案 0 :(得分:1)

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