简体   繁体   中英

How do I create a bucket in the EU region?

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("\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;
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM