AngularJS 直接上传到 Google 云存储错误

AngularJS Direct Upload to Google Cloud Storage Error

我想直接从 AngularJS

上传大于 32MB 的文件到 GCS

当文件大小小于 32MB 时,我可以使用下面给出的代码成功上传到 Blobstore

$http.post(uploadUrl, $scope.formData, {
    withCredentials: true,
    headers: {'Content-Type': undefined},
    transformRequest: angular.identity
});

当我尝试使用 url https://storage.googleapis.com/bucket-name/filename 上传文件时,出现以下错误。

Credentials flag is 'true', but the 'Access-Control-Allow-Credentials' header is ''. It must be 'true' to allow credentials. Origin 'http://localhost:8080' is therefore not allowed access.

当我删除 http post 中使用的标志 withCredentials: true 时,我收到 403 响应,没有给出错误详细信息。

我已经为上传存储桶设置了cors [{"maxAgeSeconds": 3600, "method": ["GET", "POST", "HEAD"], "origin": ["http://localhost:8088"], "responseHeader": ["Content-Type"]}]

如何在服务器中启用 Access-Control-Allow-Credentials 标志?

您可以在继续使用 blobstore 的同时完成此操作,而不是切换到云存储。 Google 处理得很好的文档。警告:在我看来 Google 支持将其作为一项遗留功能,但建议改用 Could Storage。我没有亲自做过,但听起来 Signed URLs 可能就是答案。

如果您想像我一样继续使用 blobstore,Java 中 server-side 设置的说明是 here。您应该可以在类似的 URL 上找到针对其他语言的类似说明。 :)

一开始我不明白的棘手部分:直接上传请求完成它的工作,然后使用有关上传的信息直接调用您的服务器,然后使用您服务器的响应作为返回到客户。因此,例如,您可以在服务器的上传处理程序中设置 Access-Control-Allow-Origin header,它将添加到直接上传 link.

的响应中