[英]Upload files from browser to S3
I have a pre signed url that allows you to PUT documents objects into S3. 我有一个预签名的url,它允许您将对象文档放入S3。 http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjectPreSignedURLDotNetSDK.html
http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjectPreSignedURLDotNetSDK.html
How do I put a file from the browser to S3 using javascript? 如何使用javascript将文件从浏览器放置到S3? I am using angular, but I am open to using any javascript library.
我正在使用angular,但可以使用任何JavaScript库。
I believe I could POST to my server, and then PUT the object on the amazon server, but I would prefer to do it from the browser. 我相信我可以发布到我的服务器上,然后将对象放在亚马逊服务器上,但是我更喜欢从浏览器中进行操作。
I have changed the CORS settings on S3 to allow PUTs. 我已经更改了S3的CORS设置以允许PUT。
I was planning to use angular file upload, but it is hard coded to POST not PUT. 我打算使用角度文件上传,但是很难将其编码为POST而不是PUT。 https://github.com/danialfarid/angular-file-upload
https://github.com/danialfarid/angular-file-upload
Amazon has a guide ( here ) that describes how to POST-upload a file into your S3 bucket. 亚马逊提供了一个指南( here ),该指南描述了如何将文件POST上传到您的S3存储桶中。 It relies on an input <form> signed with your AWS private key.
它依赖于使用您的AWS私钥签名的输入<form>。 You can specify restrictions on the target directory, as well as file-size restrictions.
您可以在目标目录上指定限制,以及文件大小限制。
It's a bit annoying to use, because you have to duplicate most of the fields in both the <form> and the signed policy, but it seems to work. 使用起来有点烦人,因为您必须复制<form>和已签名策略中的大多数字段,但这似乎可行。
After the POST, S3 will redirect the browser to a URL you specify in the form (with parameters specifying the name of the uploaded file, etc.). POST之后,S3会将浏览器重定向到您在表格中指定的URL(参数指定上传文件的名称,等等)。 This isn't ideal for Angular web sites, which tend to be "applications" rather than a set of discrete pages, but you could probably work with it.
对于Angular网站(这往往是“应用程序”而不是一组离散页面)而言,这不是理想的选择,但是您可以使用它。
On my Angular site, I did the POST in Javascript using $http.post() and passed all the appropriate form data. 在我的Angular网站上,我使用$ http.post()在Javascript中进行了POST,并传递了所有适当的表单数据。 Unfortunately, I was always getting "cancelled" errors, even though the uploads were actually successful.
不幸的是,即使上传实际上是成功的,我也总是遇到“已取消”的错误。 In my case, I just double-checked by downloading the file with $http.get() and comparing it to the original data... but this was only a viable solution because my files were only a couple of KB.
就我而言,我只是通过使用$ http.get()下载文件并将其与原始数据进行比较进行了双重检查……但这只是一个可行的解决方案,因为我的文件只有几个KB。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.