我正在使用Google Cloud Platform PHP客户端库( 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 );文件正确上传,可以在存储桶中查看,但没有正确的标题,因为我不知道如何添加它们。 实际上,我甚至找不到使用Cloud Platform Console手动添加这些标头的方法。 任何指针都表示赞赏,谢谢
I'm using the Google Cloud Platform PHP Client library (https://github.com/google/google-api-php-client) to upload files to a bucket in my project. I need to be able to grab the file using an AJAX request from another domain so therefore need to add the header
Access-Control-Allow-Origin: *I am pulling my hair out trying to figure this out - my Google searches are fruitless. Code sample for reference:
$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 );The file is uploaded correctly and can be viewed in the bucket, but doesn't have the correct headers as I don't know how to add them. In fact, I can't even find a way of adding these headers manually using the Cloud Platform Console. Any pointers appreciated, thanks
最满意答案
所以我终于找到了我需要的文档,它只能为存储桶本身设置CORS配置(它在文件级别是不可配置的)。 这里使用gsutil或XML API执行此操作的说明
我用内容创建了一个cors-json-file.json:
[ { "origin": ["*"], "method": ["*"] } ]然后跑了
gsutil cors set cors-json-file.json gs://my_bucket可以使用查看存储桶的现有CORS配置
gsutil cors get gs://my_bucket可以在Google Bucket API参考中找到完整的配置选项列表
无论是否存在缓存问题我都不确定,但这似乎仅适用于在进行 CORS配置更改后添加到存储桶中的文件,但是我很高兴能够更正
So I finally found the documentation I needed, it's only possible to set the CORS configuration for the bucket itself (it is not configurable at file level). Instructions to do so using either gsutil or the XML API are here
I created a cors-json-file.json with contents:
[ { "origin": ["*"], "method": ["*"] } ]Then ran
gsutil cors set cors-json-file.json gs://my_bucketIt's possible to view the existing CORS configuration for your bucket using
gsutil cors get gs://my_bucketA full list of configuration options can be found at the Google Bucket API Reference
Whether it's an issue with caching or not I'm unsure, but this seems to work only for files added to the bucket after you make the CORS configuration change, however I'm happy to be corrected on that
更多推荐
发布评论