无法通过浏览器中的URL从S3下载文件 [英] Unable to download a file from S3 by the URL in a browser
问题描述
我有这样的代码段,该代码段以前可以与以前版本的Google Chrome一起使用,但现在不起作用.当我运行此脚本时,我使用文件内容(文本)和AWS URL(与setAttribute中的内容相同)重定向到页面.
I have such code snippet that used to be work with the previous versions of Google Chrome, but now it does not work. When I run this script I redirected to the page with the content of the file(it's a text) and with AWS URL(the same as in setAttribute).
var element = document.createElement('a');
element.setAttribute('href', 'https://s3-us-east-1.amazonaws.com/XXX/XXX/XXX?Signature=XXX&Expires=XXX&AWSAccessKeyId=XXX');
element.setAttribute('download', 'filename.txt');
document.body.appendChild(element);
element.click();
如何下载此文件?
推荐答案
我在注释中犯了一个错误.Chrome仍然偏爱download属性,但是如果anchor元素具有跨源属性(也就是说,如果文件托管在其他域中),它将完全忽略该属性.
I made an error in the comments. Chrome still favours download attribute, but it completely ignores it if the anchor element has cross origin attributes ( meaning, if the file is hosted on a different domain).
要下载该文件,文件必须带有标题: Content-Disposition:附件;
To be able to download it, file needs to be served with header: Content-Disposition: attachment;
查看本教程,了解如何在s3管理控制台中设置Content-Disposition: http://iwantmyreal.name/s3-download-only-presigned-upload
Check this tutorial to see how to set Content-Disposition in s3 management console: http://iwantmyreal.name/s3-download-only-presigned-upload
这篇关于无法通过浏览器中的URL从S3下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!