简体   繁体   English

Javascript从亚马逊s3存储桶下载文件?

[英]Javascript to download a file from amazon s3 bucket?

I was trying to download a file from a bucket on Amazon S3.我试图从 Amazon S3 上的存储桶下载文件。 I was wondering if I can write a javascript to download such a file from a bucket.我想知道我是否可以编写一个 javascript 来从存储桶中下载这样的文件。 I was googling it, but couldn't find any resources that can help me do that.我在谷歌上搜索它,但找不到任何可以帮助我做到这一点的资源。

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,谢谢,

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
    }
  }
);

I came here looking for away to download a s3 file on the client side.我来这里是为了在客户端下载一个 s3 文件。 Here is how I solved it:这是我解决它的方法:

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:在前端使用这个 URL 来触发下载:

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);
        }
    })
});

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. (获取url is too long )我的解决方案是包含一个隐藏的锚标记,并在 ajax 成功时触发对该标记的点击。 You can't use the anchor tag right off the bat unless you don't care about handling errors.除非您不关心处理错误,否则您不能立即使用锚标记。

S3 will respond with an XML error file if something goes wrong, so the browser will automatically display that XML response.如果出现问题,S3 将使用 XML 错误文件进行响应,因此浏览器将自动显示该 XML 响应。 By first attempting to hit the URL with ajax, you can catch that error without showing the ugly XML.通过首先尝试使用 ajax 访问 URL,您可以在不显示丑陋 XML 的情况下捕获该错误。 On success in that ajax call is when you know you're clear to try and download the file.在该 ajax 调用中成功是当您知道您可以尝试下载文件时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM