如何使用签名的URL和JavaScript上传为Amazon S3的二进制字符串的文件?
我在获取签名 URL 时没有遇到任何问题,并且该文件实际上已上传到 S3,但当我下载该文件时,我无法打开它。我尝试过 PDF 和图像文件。
我得到这样的文件,其中“e”是来自浏览器文件输入的文件上传事件:
let fileData = e.target.files[0];
let reader = new FileReader();
reader.readAsBinaryString(fileData); // generates a binary representation of the image
reader.onload = function(e) {
let bits = e.target.result;
let data = {
originalFilename: fileData.name,
filename: fileData.name,
mimeType: fileData.type,
fileSizeBytes: fileData.size,
lastModified: fileData.lastModified,
bin: bits
};
}
我将“数据”json存储在IndexeddB中,稍后当浏览器在线时,我得到一个签名的URL并尝试像这样上传它这个:
// signedUrl is the signed URL
// data is the saved file data from the IndexeddB (above)
let contentType = data.mimeType
let binaryString = data.bin; // bin is a binary string
let formData = new FormData();
formData.append("file", btoa(data.bin));
// upload the file directly to AWS
return $.ajax({
url: signedUrl,
method: "POST",
contentType: contentType ,
data: formData,
processData: false
})
.then(function (response) {
console.log(response);
})
.catch(function (e) {
console.log('Error in file upload to AWS:');
console.log(e);
throw('Upload failed');
})
有很多示例展示了如果您有 File 对象(或者您正在使用 Postman),如何将文件发布到签名 URL,但我的网络应用程序允许用户离线“上传”文件,并将它们存储在IndexeddB 作为二进制字符串。这一切都工作正常,我可以轻松地将文件发布到我的服务器,重新创建文件,然后将它们发送到 S3,但我想避免额外的行程。
我尝试过创建 Blob 和其他一些东西,但我陷入了困境。任何帮助将不胜感激。
实际上,我需要知道的是“发布到发布数据中签名 URL 的文件到底是什么格式,以及如何将我的文件数据转换为该格式?”
I am not having any trouble getting the signed URL, and the file is actually getting uploaded to S3, but when I download the file I can't open it. I have tried PDF and image files.
I get the file like this, where 'e' is the file upload event from the browser file input:
let fileData = e.target.files[0];
let reader = new FileReader();
reader.readAsBinaryString(fileData); // generates a binary representation of the image
reader.onload = function(e) {
let bits = e.target.result;
let data = {
originalFilename: fileData.name,
filename: fileData.name,
mimeType: fileData.type,
fileSizeBytes: fileData.size,
lastModified: fileData.lastModified,
bin: bits
};
}
I store the 'data' json in the IndexeddB and later when the browser is online I get a signed URL and try to upload it like this:
// signedUrl is the signed URL
// data is the saved file data from the IndexeddB (above)
let contentType = data.mimeType
let binaryString = data.bin; // bin is a binary string
let formData = new FormData();
formData.append("file", btoa(data.bin));
// upload the file directly to AWS
return $.ajax({
url: signedUrl,
method: "POST",
contentType: contentType ,
data: formData,
processData: false
})
.then(function (response) {
console.log(response);
})
.catch(function (e) {
console.log('Error in file upload to AWS:');
console.log(e);
throw('Upload failed');
})
There are lots of examples which show how to post the file to the signed URL if you have the File object (or you are using Postman), but my web app allows users to "upload" files offline and they are stored in the IndexeddB as binary strings. This all works fine, and I can easily POST the files to my server, recreate the file, and then send them to S3, but I want to avoid the extra trips.
I have tried creating a Blob and quite a few other things and I am stuck. Any help would be greatly appreciated.
Really, all I need to know is "Exactly what format is the file that gets posted to the signed URL in the post data and how can I convert my file data to that format?"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我终于知道该怎么做了。亚马逊网站上的文档很少,网络上有很多错误信息。您只需重新创建文件 Blob(并且不要使用 fileData):
OK, I finally figured out exactly what to do. There is very little documentation on the Amazon site, and a lot of misinformation on the web. You just have to recreate the file Blob (and DO NOT use fileData):
在 aws s3 上传时,您可以简单地传递二进制编码选项,如下所示 -
You can simply pass binary encoding option when doing upload at aws s3 like below -