![](/img/trans.png)
[英]PhP - How do I block every EU country except the UK, and allow all non EU countries?
[英]How do I create a bucket in the EU region?
我正在嘗試使用PHP和REST在Google雲存儲上創建存儲桶。 我可以在美國地區創建好一個水桶,但似乎不能在歐盟地區創建一個水桶。
這是我在做什么-
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 -L?
我只是使用gsutil來指定位置,並使用-DD轉儲了請求的內容,並注意到在請求正文中發送的XML文檔不包含XML標頭。 接下來,我嘗試使用curl使用類似的無標題XML文檔來制定REST請求,它對我來說很好用。 接下來,我再次嘗試使用XML標頭進行curl,但由於以下錯誤而失敗:
您提供的XML格式不正確或未針對我們發布的架構進行驗證。
您可以重試不帶XML標頭的PHP代碼,看看是否有幫助? 如果是這樣,我可以為此提交一個錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.