Kit Kit - 3 years ago 110
Node.js Question

S3 Force File Download with NodeJS

I am trying to force files to download from Amazon S3 using the GET request parameter

response-content-disposition
.

I first created a signed URL which works fine when I want to view the file.

I then attempt to redirect there with the
response-content-disposition
header. Here is my code:

res.writeHead(302, {
'response-content-disposition': 'attachment',
'Location': 'http://s3-eu-west-1.amazonaws.com/mybucket/test/myfile.txt?Expires=1501018110&AWSAccessKeyId=XXXXXX&Signature=XXXXX',
});


However, this just redirects to the file and does not download it.

Also when I try and visit with the file with the
response-content-disposition
as GET variable:

http://s3-eu-west-1.amazonaws.com/mybucket/test/myfile.txt?Expires=1501018110&AWSAccessKeyId=XXXXXX&Signature=XXXXX&response-content-disposition=attachment


..I reveive the following response:

The request signature we calculated does not match the signature you provided. Check your key and signing method.

Answer Source

The correct way of using the response-content-disposition option is to include it as a GET variable but you're not calculating the signature correctly.

You can find more information on how you should calculate the signature in the Amazon REST Authentication guide

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download