Amazon S3 CORS PUT not working

I am trying to upload a large file (1.5 GB) in Amazon S3 using REST Api and HTML5. Here's what the download code looks like (code is removed 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 6 MB. After the piece finishes loading, the next next, etc. But sometimes (every 80 pieces or so) the PUT request fails, with e.type == "error" , e.target.status == 0 (which surprises me) and e.target.responseText == "" . After a fragment fails, the code tries to load it again and receives the same error. When I refresh the page and continue loading (the same piece!), It works like a charm (80 pieces or so when it gets stuck again). Here's how the request looks in chrome dev tools:

enter image description hereenter image description hereenter image description here

Any ideas why this might happen, or how to debug something like this?

EDIT: Here is the OPTIONS answer:

enter image description here

+6
source share
2 answers

I finally found a problem sniffing packages: there are two problems:

  • for PUT requests that receive 4xx (have not tested other 2xx responses), the xhr request is returned as aborted (status = 0); still have not found an explanation for this, check Why is the PUT 403 displayed as interrupted?

  • Amazon S3 responded with 403 , which said RequestTimeTooSkewed , because my signatures are generated at startup, and after 15 minutes (timeout causing the RequestTimeTooSkewed error) it fails and the signatures need to be restored. This 403 error never occurs in the developer console or in js code due to the first problem.

After the regeneration of signatures, everything works like a charm.

+4
source

You checked to see if your browser makes any "OPTIONS" request. If so, what are the response headers.

+2
source

Source: https://habr.com/ru/post/926405/


All Articles