我使用谷歌控制台的UI创建了一个Bucket。我想以编程方式在Bucket中上传一些对象。我查看了Google的文档。 https://cloud.google.com/storage/docs/object-basics#upload
$storage = new StorageClient();
$file = fopen($source, 'r');
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload($file, [
'name' => $objectName
]);
printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($source), $bucketName, $objectName);
问题是,返回有关权限的错误。
{
"error": {
"errors": [
{
"domain": "global",
"reason": "forbidden",
"message": "starting-account-f8mo7two5s1@kjs-speech-api-1506584214035.iam.gserviceaccount.com does not have storage.objects.create access to object kjs-lms/0.mp3."
}
],
"code": 403,
"message": "starting-account-f8mo7two5s1@kjs-speech-api-1506584214035.iam.gserviceaccount.com does not have storage.objects.create access to object kjs-lms/0.mp3."
}
}
我尝试在IAM角色中授予权限,但仍然是同样的错误。
答案 0 :(得分:1)
您用于运行PHP程序的帐户没有对存储桶的写入权限。您是在应用引擎或GCE等其他Google云环境中运行吗?在这种情况下,您的程序可能正在运行Application Default Credentials,这与您自己运行的不同。授予相关存储桶的错误消息所有权中提到的帐户,并且应该解决该问题。