Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我如何在欧盟地区创建一个桶?_Php_Rest_Google Cloud Storage - Fatal编程技术网

Php 我如何在欧盟地区创建一个桶?

Php 我如何在欧盟地区创建一个桶?,php,rest,google-cloud-storage,Php,Rest,Google Cloud Storage,我正在尝试使用PHP和REST在google云存储上创建一个bucket。我可以在美国地区创建一个桶,但在欧盟地区似乎无法创建一个桶 这就是我要做的- function createBucket($accessToken, $bucket, $region) { $version_header = "x-goog-api-version: 2"; $project_header = "x-goog-project-id: ".$this->projectID; $u

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

这就是我要做的-

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;
}
函数createBucket($accessToken,$bucket,$region)
{
$version\u header=“x-goog-api-version:2”;
$project\u header=“x-goog-project-id:”.$this->projectID;
$url='https://'.$bucket'.commondatastorage.googleapis.com';
$timestamp=日期(“r”);
定义('XML_有效载荷','.$region');
$headers=数组(
'主机:'.$bucket.'.commondatastorage.googleapis.com',
“日期:”.$timestamp,
$version_标题,
$project_标题,
'内容长度:0',
“授权:OAuth”。$accessToken);
$c=curl_init($url);
curl_setopt($c,CURLOPT_头,1);
curl_setopt($c,CURLOPT_RETURNTRANSFER,1);
curl_setopt($c,CURLOPT_HTTPHEADER,$headers);
curl_setopt($c,CURLOPT_POSTFIELDS,XML_有效载荷);
curl_setopt($c,CURLOPT_CUSTOMREQUEST,“PUT”);
$response=curl_exec($c);
收尾费($c);
//将响应拆分为标头和xml正文
列表($header,$xml)=分解(“\r\n\r\n”,$response,2);
//标记化-第一个标记是状态
$status=strtok($header,“\r\n”);
如果(stristr($status,“200 OK”))
{
//成功
$result=“success”;
}
其他的
{
//失败
$result=“fail”;
}
返回$result;
}
对于美国桶,此功能可以正常工作,但对于欧盟桶,此功能将失败。我要确保为bucket生成的名称是唯一的名称(相同的模式适用于我们的bucket)


编辑:实际上该函数没有失败,它确实创建了一个bucket。。。它只是在美国地区创建了一个桶,尽管它指定了欧盟地区。

你如何判断它是否成功?gsutil-L

我只是使用gsutil指定了一个位置,并使用-DD转储了请求的内容,并注意到在请求正文中发送的XML文档没有包含XML头。接下来,我尝试使用curl用一个类似的无头XML文档来表示REST请求,它对我来说效果很好。接下来,我再次尝试使用XML头进行curl,但失败了,出现以下错误:

您提供的XML格式不正确,或者没有针对其进行验证 我们发布的模式


您是否可以在不使用XML头的情况下重试PHP代码,看看这是否有帮助?如果成功了,我可以为此提交一个bug。

你如何判断它是否成功?gsutil-L

我只是使用gsutil指定了一个位置,并使用-DD转储了请求的内容,并注意到在请求正文中发送的XML文档没有包含XML头。接下来,我尝试使用curl用一个类似的无头XML文档来表示REST请求,它对我来说效果很好。接下来,我再次尝试使用XML头进行curl,但失败了,出现以下错误:

您提供的XML格式不正确,或者没有针对其进行验证 我们发布的模式


您是否可以在不使用XML头的情况下重试PHP代码,看看这是否有帮助?如果有,我可以为此提交一个bug。

Hi Marc,是的,我正在使用gsutil测试bucket创建。实际上,我意识到我的REST请求的内容长度为0,我将其更改为:
strlen(XML\u负载)
我收到了您看到的确切错误:
格式错误的BucketConfiguration
您提供的XML格式不正确,或者没有根据我们发布的架构进行验证。
还应该提到,我收到了美国和欧盟地区的错误。您的XML似乎缺少开头的问号
I用正确制定的p.s.尝试了我的curl测试。同时,尝试删除XML头-这对我很有用。嗨,Marc,是的,我正在使用gsutil测试bucket创建。实际上,我意识到我的REST请求的内容长度为0,我将其更改为:
strlen(XML\u负载)
我收到了您看到的确切错误:
格式错误的BucketConfiguration
您提供的XML格式不正确,或者没有根据我们发布的架构进行验证。
还应该提到,我收到了美国和欧盟地区的错误。您的XML似乎缺少开头的问号
I用正确制定的p.s.尝试了我的curl测试。同时,尝试删除XML头-这对我很有用。