2

I have files stored on S3, and I want to automatically download them for a user when they click a button

What I've done so far is to have a route

/lib/routes/download

var fs = Npm.require('fs');

Router.route("download", function() {
  console.log('retrieving ' + this.params.signedURL);
  this.response.writeHead(200, {'Content-type': 'appplication/pdf'}, this.params.signedURL);
  this.response.end(fs.readFileSync(this.params.signedURL));
}, { where: 'server', path: '/d/:signedURL'});

But this doesn't work, because i Cant use fs on the client. And even if I could, im not sure this would work

Any advice on how best to accomplish this?

Tarlen
  • 3,657
  • 8
  • 30
  • 54
  • Instead of adding a route, can you transmit the signed URL to the client via a method call? – David Weldon Mar 08 '15 at 14:43
  • Well I store them in my database if that's what you mean – Tarlen Mar 08 '15 at 14:58
  • If you have the signed URL already on the client, can't you just use the [download attribute](http://davidwalsh.name/download-attribute) of an anchor tag to accomplish this? Or are you trying to support non-HTML5 browsers? – David Weldon Mar 08 '15 at 15:45
  • I did that initially, but it has very poor browser support – Tarlen Mar 08 '15 at 15:46
  • So `readFileSync` takes a filename (not a url). I believe what your route needs to do is [download the file](https://stackoverflow.com/questions/11944932/how-to-download-a-file-with-node-js) and pipe the result back out to the client. – David Weldon Mar 08 '15 at 15:52
  • But I would much rather have the client do the downloading, is this not possible? – Tarlen Mar 08 '15 at 16:12

2 Answers2

1

Easiest way to do this is to use the cfs:s3 package which is an add-on to CollectionFS. This not only supports S3 but also transparently breaks files up into smaller chunks during upload and download to/from S3.

Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
1

I retrieve the signedUrl via a meteor method call, then do this when the user clicks a download button :

window.open(_signedURL,"_self");
looshi
  • 1,226
  • 2
  • 9
  • 23