Google云端存储存储桶内容

时间:2017-07-17 10:09:46

标签: json google-cloud-platform google-cloud-storage json-api php-curl

<?php

$url="https://www.googleapis.com/storage/v1/b/sts_bucket/o?key=xxx&project=xxxxx
$ch=curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$json_response = curl_exec($ch);

if ($json_response == FALSE){
    echo "cURL error: " . curl_error($ch);
}

curl_close($ch);

echo "</pre>";
echo $json_response;
echo "</pre>";


?>

我请求谷歌云存储获取存储桶信息。但它显示以下错误: -

{ "error": { "errors": [ { "domain": "global", "reason": "required", 
"message": "Anonymous users does not have storage.objects.list access to 
 bucket sts_bucket.", "locationType": "header", "location": "Authorization" 
 } ], "code": 401, "message": "Anonymous users does not have 
storage.objects.list access to bucket sts_bucket." } }

1 个答案:

答案 0 :(得分:0)

您需要包含授权:标头。 例如,尝试运行:

gsutil -d ls

并查看授权的调试输出:它包含的标头。输出看起来像这样:

... 
connect: (www.googleapis.com, 443)
send: 'GET /storage/v1/b?project=your-project&fields=nextPageToken%2Citems%2Fid&alt=json&projection=noAcl&maxResults=1000 HTTP/1.1\r\nHost: www.googleapis.com\r\ncontent-length: 0\r\nauthorization: Bearer your-oauth-token\r\naccept-encoding: gzip, deflate\r\naccept: application/json\r\nuser-agent: apitools gsutil/4.27 Python/2.7.6 (linux2) analytics/enabled\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
...

希望这清楚地说明了您需要在代码中更改的内容。