我正在使用库(https://github.com/google/google-api-php-client)将文件上传到项目中的一个桶中。我需要能够使用来自另一个域的AJAX请求获取文件,因此需要添加报头。
Access-Control-Allow-Origin: *我正努力想办法解决这个问题--我的谷歌搜索是徒劳的。供参考的代码示例:
$client = new Google_Client();
$client->setApplicationName("Test");
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/cloud-platform');
$storage = new Google_Service_Storage($client);
$file_name = "test.txt";
$file_content = "this is a test";
$postbody = array(
'name' => $file_name,
'data' => $file_content,
'uploadType' => "media",
'predefinedAcl' => 'publicRead'
);
$gsso = new Google_Service_Storage_StorageObject();
$gsso->setName( $file_name );
$result = $storage->objects->insert( "my_bucket", $gsso, $postbody );文件上传正确,可以在桶中查看,但没有正确的标题,因为我不知道如何添加它们。实际上,我甚至无法找到一种使用云平台控制台手动添加这些头的方法。感谢你的指点,谢谢
发布于 2016-10-12 13:37:13
因此,我终于找到了我需要的文档,只可能为桶本身设置CORS配置(它在文件级别上是不可配置的)。使用gsutil或XML API进行此操作的说明是这里。
我创建了一个包含内容的cors-json-file.json:
[
{
"origin": ["*"],
"method": ["*"]
}
]然后跑
gsutil cors set cors-json-file.json gs://my_bucket可以使用以下方法查看现有的存储区配置
gsutil cors get gs://my_bucket配置选项的完整列表可在参考中找到。
缓存是否存在问题,我不确定,但这似乎只适用于在更改CORS配置后添加到桶中的文件,不过,我很高兴在这方面得到纠正
发布于 2018-02-21 12:01:55
您还可以使用StorageClient在google-cloud-php中配置CORS。
$storage = new StorageClient([
'projectId' => '<project-id>',
'keyFilePath' => '<path-to-key-file>',
]);
$cors = [
[
'maxAgeSeconds' => '3600',
'method' => ['*'],
'origin' => ['*'],
'responseHeader' => ['Content-Type'],
],
]
// Creating a bucket with CORS
$storage->createBucket('<bucket-name>', [
'location' => 'EU',
'cors' => $cors,
]);
// Updating a bucket
$storage->bucket('<bucket-name>')->update([
'cors' => $cors,
]);https://stackoverflow.com/questions/39995762
复制相似问题