2012-09-14 45 views
1

我正試圖在使用PHP和REST的谷歌雲存儲上創建一個存儲桶。我可以在美國地區創建這個桶,但似乎無法在歐盟地區創建一個桶。如何在歐盟地區創建一個存儲桶?

下面是我在做什麼 -

function createBucket($accessToken, $bucket, $region) 
{ 
    $version_header = "x-goog-api-version: 2"; 
    $project_header = "x-goog-project-id: ".$this->projectID; 
    $url = 'https://'.$bucket.'.commondatastorage.googleapis.com'; 
    $timestamp = date("r"); 
    define('XML_PAYLOAD','<xml version=\'1.0\' ? ><CreateBucketConfiguration><LocationConstraint>'.$region.'</LocationConstraint></CreateBucketConfiguration>'); 

    $headers = array(
       'Host: '.$bucket.'.commondatastorage.googleapis.com', 
       'Date: '.$timestamp, 
       $version_header, 
       $project_header, 
       'Content-Length: 0', 
       'Authorization: OAuth '.$accessToken); 

    $c = curl_init($url); 
    curl_setopt($c, CURLOPT_HEADER, 1); 
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($c, CURLOPT_HTTPHEADER,$headers); 
    curl_setopt($c, CURLOPT_POSTFIELDS,XML_PAYLOAD); 
    curl_setopt($c, CURLOPT_CUSTOMREQUEST, "PUT"); 
    $response = curl_exec($c); 
    curl_close($c); 

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

    if(stristr($status,"200 OK")) 
    { 
     //success 
     $result = "success"; 
    } 
    else 
    { 
     //failed 
     $result = "fail"; 
    } 

    return $result; 
} 

此功能工作正常的美國鬥,但在歐盟的情況下失敗。我確保爲該存儲桶生成的名稱是唯一的名稱(相同的模式適用於美國存儲桶)。

編輯:實際上功能並不失敗,它確實創建了一個存儲桶......它只是在美國地區創建存儲桶,儘管指定了EU地區。

回答

1

你是如何告訴它是否成功? gsutil -L?

我只是用gsutil以指定位置和傾倒與-DD請求的內容,並注意到,在請求體中發送的XML文檔不包括XML頭。接下來,我嘗試使用curl來制定一個類似無頭文件的XML文檔的REST請求,它對我來說工作得很好。接下來,我又試了一次與捲曲XML頭,但失敗與此錯誤:

The XML you provided was not well-formed or did not validate against our published schema.

您可以重試你的PHP代碼,而不XML頭,看看有沒有什麼幫助?如果是這樣,我可以爲此提出一個錯誤。

+0

嗨馬克, 是的,我使用gsutil測試鬥創作。 其實我意識到,我的REST請求具有0內容長度和我改變了到: '的strlen(XML_PAYLOAD)' ,我收到了你看到確切的錯誤: 'MalformedBucketConfiguration' 的'您提供的XML是沒有很好地形成或沒有驗證對我們公佈的schema.' –

+0

還要提到的是,我收到了錯誤的美國和歐盟地區 –

+1

你的XML似乎缺少開幕問號'<'XML? '定義( 'XML_PAYLOAD', ' ...'(這是[在DOCO]的情況下(https://developers.google.com/storage/docs/developer-指導#specifiedlocations)以及) –

相關問題