我试图通过AJAX从本地主机上的谷歌云存储获取文件。我也做了以下内容:谷歌云存储忽略访问控制源标题
通过gsutil会设置CORS我斗:
gsutil cors set cors.json gs://my-project
其中cors.json文件是:
[
{
"origin": [
"*"
],
"responseHeader": ["Origin", "Accept", "X-Requested-With", "Authorization", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token"],
"method": [
"GET",
"OPTIONS"
],
"maxAgeSeconds": 1
}
]
我与gsutil cors get gs://my-project
然后对于每个文件,我已经公开,通过node.js客户端库文件上传时:
bucket.file(object.name).makePublic()
通过控制台,并通过gsutil会:
gsutil -m acl set -R -a public-read gs://my-project
然后在我的Ajax请求,我也送头:
$.ajax({
method: "GET",
url: "https://googleapis.com/storage/v1/b/my-project/o?delimiter=audio",
headers: {
'Access-Control-Allow-Origin': '*'
},
crossDomain: true,
}).done((data) => {
console.log(data)
})
,我仍然获得科斯错误:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:3000 ' is therefore not allowed access.
如何通过CORS?
非常有帮助的回复 - 谢谢! – mheavers
以下是我的终端格式:“storage.googleapis.com/[BUCKET_NAME]/[OBJECT_NAME]”,是否意味着将其更改为“www.storage.googleapis.com/[BUCKET_NAME]/[OBJECT_NAME]”“? – KevinHu
storage.googleapis.com/BUCKET/OBJECT很好。 –