cors - 使用在 gsutil 中配置了 cors 的 PUT 方法上传到 Google Cloud Storage 时没有访问控制允许来源

标签 cors google-cloud-storage cloudflare

我们已经搜索了整个网络以找到解决此问题的方法,但仍然没有成功! 我们有一个网站,我们让用户使用后端生成的签名上传 URL 将文件直接上传到 Google Cloud Storage 存储桶,我们使用 PUT 请求上传。每当用户上传文件时,chrome 开发工具控制台中都会出现错误

No 'Access-Control-Allow-Origin' header is present on the requested resource.

我们已经更新了存储桶的 cors 设置,如下所示:

$ gsutil cors get gs://{BUCKET_NAME}
[{"method": ["POST", "PUT", "GET", "HEAD", "OPTIONS"], "origin": ["*"], "responseHeader": ["*"]}]

但是,无论何时我们发送请求,响应 header 中都不会返回 Access-Control-Allow-Origin header 。

注意事项:我们使用的是计算引擎实例,我们的域名由 Cloudflare 管理。

一些文章表明这​​可能是一个缓存问题,但我们已经面对这个问题数周了,没有任何改变。

我听说 PUT 方法可能需要飞行前请求才能工作,我知道飞行前请求的理论部分,但我以前从未使用过它并且不确定该怎么做,我是否应该在发送主提取之前添加另一个请求请求,还是全部自动并在幕后发生?

最佳答案

我会发表我的评论作为答案,因为它对评论部分来说太长了。

在研究引用此错误的众多帖子时,我偶然发现了 this one这可能与您有关,因为用户正在 GCS 上上传文件。

Here您还可以找到有关在 GCS 上启动可恢复上传的文档。

When using resumable uploads, you first need to make a POST request to the endpoint to start it and then 1 or more (depending on the number of chunks) PUT requests for the actual upload. When the Origin header is not specified in the first POST, the last chunk will not contain the headers that allow CORS to work and the browser will reject the request, even though it could be actually made (if file gets actually uploaded).

希望这些信息对您有所帮助。

关于cors - 使用在 gsutil 中配置了 cors 的 PUT 方法上传到 Google Cloud Storage 时没有访问控制允许来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61577457/

相关文章:

javascript - NestJS 在生产中启用 cors

java - 实现谷歌云存储的最佳方式是什么?

heroku - Cloudflare 和 Heroku 上的子域

javascript - 对 ASP.NET Web API 的跨域请求

javascript - 由于 cors,将图像上传到 imgur 失败

java - Google App Engine 和 CORS

google-app-engine - 在 "unexpected error"中使用 google cloud 添加计费方式

machine-learning - Cloud ML 高效地从 Google Storage 读取大量图像

haskell - 带有 Keter 和 Cloudflare 的免费 SSL 的 Yesod 网络应用程序

.htaccess - 除 1 个单页外,在整个网站上强制 HTTPS 重定向