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地區。
嗨馬克, 是的,我使用gsutil測試鬥創作。 其實我意識到,我的REST請求具有0內容長度和我改變了到: '的strlen(XML_PAYLOAD)' ,我收到了你看到確切的錯誤: 'MalformedBucketConfiguration' 的'您提供的XML是沒有很好地形成或沒有驗證對我們公佈的schema.' –
還要提到的是,我收到了錯誤的美國和歐盟地區 –
你的XML似乎缺少開幕問號'<'XML? '定義( 'XML_PAYLOAD', ' ...'(這是[在DOCO]的情況下(https://developers.google.com/storage/docs/developer-指導#specifiedlocations)以及) –