从amazon s3 bucket下载文件的Javascript ?

时间:2023-02-06 00:05:28

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进行身份验证,然后提供bucket名称和文件(键),下载或读取文件,以便能够在文件中显示数据。

Thanks,

谢谢,

2 个解决方案

#1


18  

Maybe you can use AWS Node.js API:

也许您可以使用AWS节点。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
    }
  }
);

#2


2  

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:

在前端使用此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);
        }
    })
});

#1


18  

Maybe you can use AWS Node.js API:

也许您可以使用AWS节点。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
    }
  }
);

#2


2  

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:

在前端使用此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);
        }
    })
});