简体   繁体   English

Amazon S3 CORS PUT失败

[英]Amazon S3 CORS PUT fails

I'm trying to upload a large file (1.5GB) to Amazon S3 by using the REST Api and HTML5 file slicing. 我正在尝试使用REST Api和HTML5文件切片将大文件(1.5GB)上传到Amazon S3。 Here's how the upload code looks like (code stripped down for readability): 以下是上传代码的样子(为了便于阅读而删除了代码):

File.prototype.slice = File.prototype.webkitSlice || File.prototype.mozSlice || File.prototype.slice;

var length = u.settings.chunk_size; // 6MB
var start = chunk * length;
var end = Math.min(start + length, u.file.size);

var xhr = new XMLHttpRequest();
var path = "/" + u.settings.key;

path += "?partNumber=" + chunk + "&uploadId=" + u.upload_id;

var method = "PUT";
var authorization = "AWS " + u.settings.access_key + ":" + signature;
var blob = u.file.slice(start, end);

xhr.upload.addEventListener("progress", progress_handler, true);
xhr.addEventListener("readystatechange", handler, true);
xhr.addEventListener("error", error_handler, true);
xhr.addEventListener("timeout", error_handler, true);

xhr.open(method, u.settings.host + path, true);

xhr.setRequestHeader("x-amz-date", date);
xhr.setRequestHeader("Authorization", authorization);
xhr.setRequestHeader("Content-Type", u.settings.content_type);
xhr.setRequestHeader("Content-Disposition", "attachment; filename=" + u.file.name);

xhr.send(blob);

chunk_size is 6MB. chunk_size是6MB。 After a chunk finishes uploading, the next one follows, and so on. 块完成上传后,下一个块跟随,依此类推。 But sometimes (every 80 chunks or so), the PUT request fails, with e.type == "error" , e.target.status == 0 (which surprises me), and e.target.responseText == "" . 但有时候(每80块左右), PUT请求失败, e.type == "error"e.target.status == 0 (令我惊讶), e.target.responseText == "" After a chunk fails, the code re-attempts to upload it, and gets the exact same error. 块失败后,代码会重新尝试上传它,并获得完全相同的错误。 When I refresh the page and continue the upload (the same chunk!), it works like a charm (for 80 chunks or so, when it gets stuck again). 当我刷新页面并继续上传(相同的块!)时,它就像一个魅力(80块左右,当它再次卡住时)。 Here's how the request looks in chrome dev tools: 以下是Chrome开发工具中请求的外观:

在此输入图像描述在此输入图像描述在此输入图像描述

Any ideas why this might happen, or how to debug something like this? 任何想法为什么会发生这种情况,或者如何调试这样的东西?

EDIT: Here is the OPTIONS response: 编辑:这是OPTIONS回复:

在此输入图像描述

I finally found the issue by sniffing packets: there are two issues: 我终于通过嗅探数据包找到了这个问题:有两个问题:

  1. for PUT requests that get a 4xx (didn't test for other non- 2xx responses), the xhr request returns as aborted (status = 0); 对于获得4xx PUT请求(未测试其他非2xx响应),xhr请求返回为中止(状态= 0); still haven't found an explanation for that, check out Why does a PUT 403 show up as Aborted? 仍然没有找到解释,请查看为什么PUT 403显示为已中止?

  2. Amazon S3 responded with a 403 that said RequestTimeTooSkewed , because my signatures are generated when the upload starts, and after 15 minutes (the timeout that triggers the RequestTimeTooSkewed error), it fails, and the signatures have to be regenerated. Amazon S3回复了403 ,表示RequestTimeTooSkewed ,因为我的签名是在上传开始时生成的,15分钟后(触发RequestTimeTooSkewed错误的超时),它失败了,签名必须重新生成。 That 403 error is never seen in the dev tools console or by the js code, because of the first problem.. 由于第一个问题,在开发工具控制台或js代码中从未见过403错误。

After regenerating the signatures, everything works like a charm. 重新生成签名后,一切都像魅力一样。

Did you verify whether browser is making any 'OPTIONS' request. 您是否验证了浏览器是否正在发出任何“OPTIONS”请求。 If yes what is the response headers. 如果是,响应标头是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM