将Access

编程入门 行业动态 更新时间:2024-10-20 11:39:58
Access-Control-Allow-Origin标头添加到使用PHP客户端库上载的文件中(Adding Access-Control-Allow-Origin header to file uploaded using PHP client library)

我正在使用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_bucket

It's possible to view the existing CORS configuration for your bucket using

gsutil cors get gs://my_bucket

A 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

更多推荐

本文发布于:2023-08-05 08:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428842.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Access

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!