Amazon S3 REST API大小不正确

Amazon S3 REST API incorrect size

本文关键字:不正确 API S3 REST Amazon      更新时间:2023-09-26

我试图实现一个HTML5 Amazon S3上传器(通过使用REST API),无意中发现了以下问题:当试图上传一个小的文本文件时,一切都像一个魅力。当试图上传一个二进制文件时,S3上的文件会变大,而且显然已经损坏。我在做什么:

// multipart upload init / finish code hidden; if you need it, I'll add it
// file is read by using a file input
var blob = file.slice(start, end);
var reader = new FileReader();
reader.readAsBinaryString(blob);
// in reader.onloadend:
var path = "/" + settings.key;
path += "?partNumber=" + chunk + "&uploadId=" + u.upload_id;
var method = "PUT";
var authorization = "AWS " + settings.access_key + ":" + signature;
var xhr = new XMLHttpRequest();
xhr.open(method, settings.host + path, true);
xhr.setRequestHeader("x-amz-date", date);
xhr.setRequestHeader("Authorization", authorization);
// application/octet-stream used
xhr.setRequestHeader("Content-Type", settings.content_type); 
xhr.send(e.target.result);

此外,我还尝试创建一个10mb的文本文件(0123456789的1000万行),该文件工作正常。

如果有人能解决这个问题,或者偶然发现了这个问题,请告诉我。

StackOverflow似乎也很适合自己解决问题——我刚刚把我的想法写下来就解决了。似乎xhr.send()方法可以直接接收file.slice() blob,因此不需要FileReader

我希望这能帮助其他偶然发现这个问题的人。