Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you download a file from AWS S3 to a client's device?

Have looked at all the tutorials on how to download files from S3 to local disk. I have followed all the solutions and what they do is download the file to the server and not to the client. The code I currently have is

app.get('/download_file', function(req, res) {
  var file = fs.createWriteStream('/Users/arthurlecalvez/Downloads/file.csv');
  file.on('close', function(){console.log('done'); });
  s3.getObject({ Bucket: 'data.pool.al14835', Key: req.query.filename }).on('error', function (err) {
   console.log(err);
   }).on('httpData', function (chunk) {
      file.write(chunk);
   }).on('httpDone', function () {
       file.end();
   }).send();

  res.send('success')

 })

How do I then send this to the client so that it is downloaded onto their device?

like image 941
Arthur Le Calvez Avatar asked Jan 26 '18 06:01

Arthur Le Calvez


People also ask

How do I download data from AWS S3?

You can download an object from an S3 bucket in any of the following ways: Select the object and choose Download or choose Download as from the Actions menu if you want to download the object to a specific folder. If you want to download a specific version of the object, select the Show versions button.

How do I transfer files from S3 bucket to local machine?

You can use cp to copy the files from an s3 bucket to your local system. Use the following command: $ aws s3 cp s3://bucket/folder/file.txt .

How can you download an S3 bucket including all folders and files?

Copy an entire folder from S3Use the cp command to download a folder inside a bucket from S3 to local. recursive option will download all files and folders if you have a recursive folder/file structure.


2 Answers

you can use SignedURL Like that

var params = {Bucket: bucketname , Key: keyfile , Expires: 3600 , ResponseContentDisposition :  `attachment; filename="filename.ext"` };
var url = s3.getSignedUrl('getObject', params);

the generated link will force download with the name filename.ext

like image 141
Alsemany Avatar answered Sep 19 '22 11:09

Alsemany


S3 supports the ability to generate a pre-signed URL via the AWS Javascript API. Users can then GET this URL to download the S3 object to their local device.

See this question for a Node.js code sample.

like image 42
Adil B Avatar answered Sep 18 '22 11:09

Adil B