javascript Javascript从亚马逊s3存储桶下载文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16799956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript to download a file from amazon s3 bucket?
提问by c0mrade
I was trying to download a file from a bucket on Amazon S3. I was wondering if I can write a javascript to download such a file from a bucket. I was googling it, but couldn't find any resources that can help me do that.
我试图从 Amazon S3 上的存储桶下载文件。我想知道我是否可以编写一个 javascript 来从存储桶中下载这样的文件。我在谷歌上搜索它,但找不到任何可以帮助我做到这一点的资源。
Some steps in mind are: authenticate Amazon S3, then by providing bucket name, and file(key), download or read the file so that I can be able to display the data in the file.
需要记住的一些步骤是:验证 Amazon S3,然后通过提供存储桶名称和文件(密钥),下载或读取文件,以便我能够显示文件中的数据。
Thanks,
谢谢,
回答by yegor256
Maybe you can use AWS Node.js API:
也许您可以使用AWS Node.js API:
var AWS = require('aws-sdk');
AWS.config.update(
{
accessKeyId: ".. your key ..",
secretAccessKey: ".. your secret key ..",
}
);
var s3 = new AWS.S3();
s3.getObject(
{ Bucket: "my-bucket", Key: "my-picture.jpg" },
function (error, data) {
if (error != null) {
alert("Failed to retrieve an object: " + error);
} else {
alert("Loaded " + data.ContentLength + " bytes");
// do something with data.Body
}
}
);
回答by Sahith Vibudhi
I came here looking for away to download a s3 file on the client side. Here is how I solved it:
我来这里是为了在客户端下载一个 s3 文件。这是我解决它的方法:
As, I can not store my s3 auth keys on client side, I used my server-side scripts to generate a pre-signed url and send it back to client like:
因为,我无法在客户端存储我的 s3 身份验证密钥,我使用我的服务器端脚本生成一个预签名的 url 并将其发送回客户端,如:
const AWS = require('aws-sdk')
const s3 = new AWS.S3()
AWS.config.update({accessKeyId: 'your access key', secretAccessKey: 'you secret key'})
const myBucket = 'bucket-name'
const myKey = 'path/to/your/key/file.extension'
const signedUrlExpireSeconds = 60 * 5 // your expiry time in seconds.
const url = s3.getSignedUrl('getObject', {
Bucket: myBucket,
Key: myKey,
Expires: signedUrlExpireSeconds
})
// return the url to client
Use this URL in the front-end to trigger download:
在前端使用这个网址来触发下载:
function download(url){
$('<iframe>', { id:'idown', src:url }).hide().appendTo('body').click();
}
$("#downloadButton").click(function(){
$.ajax({
url: 'example.com/your_end_point',
success: function(url){
download(url);
}
})
});
回答by cssiamamess
Other answers here work, but wanted to expand on what worked for me.
这里的其他答案有效,但想扩展对我有用的内容。
In my case, I was dealing with files too large for
就我而言,我处理的文件太大了
function download(url){
$('<iframe>', { id:'idown', src:url }).hide().appendTo('body').click();
}
to work. ( Was getting url is too long
)
My solution was to include a hidden anchor tag, and trigger the click to that tag on ajax success.
You can't use the anchor tag right off the bat unless you don't care about handling errors.
去工作。(正在获取url is too long
)我的解决方案是包含一个隐藏的锚标记,并在 ajax 成功时触发对该标记的点击。除非您不关心处理错误,否则您不能立即使用锚标记。
S3 will respond with an XML error file if something goes wrong, so the browser will automatically display that XML response. By first attempting to hit the URL with ajax, you can catch that error without showing the ugly XML. On success in that ajax call is when you know you're clear to try and download the file.
如果出现问题,S3 将使用 XML 错误文件进行响应,因此浏览器将自动显示该 XML 响应。通过首先尝试使用 ajax 访问 URL,您可以在不显示丑陋 XML 的情况下捕获该错误。在该 ajax 调用成功时,您知道您可以尝试下载文件。