如何在欧盟地区创建存储桶?

如何在欧盟地区创建存储桶?

问题描述:

I'm trying to create a bucket on google cloud storage using PHP and REST. I can create the bucket just fine in the US region but cannot seem to create one in the EU region.

Here's what I'm doing -

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("

", $response, 2);
    // tokenize - first token is the status
    $status = strtok($header, "
"); 

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

    return $result;
}

This function works fine for a US bucket but fails in case of EU. I'm making sure that the name generated for the bucket is a unique name (the same pattern works for US buckets).

edit: actually the function does not fail, it does create a bucket ... it just creates the bucket in the US region in spite of specifying the EU region.

我正在尝试使用PHP和REST在Google云端存储上创建一个存储桶。 我可以在美国地区创建一个桶,但似乎无法在欧盟地区创建一个。 p>

这就是我正在做的事情 - p>

  function createBucket($ accessToken,$ bucket,$ region)
 {
 $ version_header =“x-goog-api-version:2”; 
 $ project_header =“x-goog-project-id:  “。$ this-&gt; projectID; 
 $ url ='https://'.$bucket.'.commondatastorage.googleapis.com'; 
 $ timestamp = date(”r“); 
 define('  XML_PAYLOAD','&lt; xml version = \'1.0 \'?&gt;&lt; CreateBucketConfiguration&gt;&lt; LocationConstraint&gt;'。$ region。'&lt; / LocationConstraint&gt;&lt; / CreateBucketConfiguration&gt;'); 
 
 $ header  = array(
'主机:'。$ bucket。'。commondatastorage.googleapis.com',
'日期:'。$ timestamp,
 $ version_header,
 $ project_header,
'Content-Length:0  ',
'授权: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); 
 
 //将响应拆分为header和xml body 
 list($ header,$ xml)= explode(“
 
 
 \”  n“,$ response,2); 
 // tokenize  - 第一个令牌是状态
 $ status = strtok($ header,”
 
“);  
 
 if(stristr($ status,“200 OK”))
 {
 //成功
 $ result =“成功”; 
} 
其他
 {
 //失败
  $ result =“fail”; 
} 
 
返回$ result; 
} 
  code>  pre> 
 
 

此函数适用于美国存储桶但在以下情况下失败 欧盟 我确保为存储桶生成的名称是一个唯一的名称(相同的模式适用于美国存储桶)。 p>

编辑: strong>实际上该功能确实如此 没有失败,它确实创造了一个桶...它只是在美国地区创建了桶,尽管指定了欧盟区域。 p> div>

How are you telling whether it succeeded? gsutil -L?

I just used gsutil to specify a location and dumped the contents of the request with -DD and noticed that the XML document sent in the request body did not include an XML header. Next I tried using curl to formulate the REST request with a similar header-less XML doc and it worked fine for me. Next I tried curl again with the XML header and it failed with this error:

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

Can you retry your PHP code without the XML header and see if that helps? If it does, I can file a bug for this.