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

时间:2016-07-07 23:02:48

标签: angularjs google-app-engine google-cloud-storage

我想直接从AngularJS上传文件大小超过32MB到GCS

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

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

当我尝试使用网址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标志?

1 个答案:

答案 0 :(得分:1)

您可以在继续使用blobstore的同时完成此操作,而不是切换到云存储。 Google文档处理得相当好。警告:听起来像谷歌支持这作为一项遗留功能,但建议使用Can Storage。我个人没有这样做过,但听起来Signed URLs可能就是答案。

如果你想继续使用blobstore,那么Java中的服务器端设置指令是here。您应该在类似的URL上找到类似的其他语言说明。 :)

我一开始并不理解的棘手部分:直接上传请求会自行处理,然后直接向您的服务器调用上传信息,然后使用您服务器的响应作为回复客户端的响应。例如,您可以在服务器的上传处理程序中设置Access-Control-Allow-Origin标头,并将其添加到直接上传链接的响应中。