0

I want to write a javascript code that downloads a certain file from S3 once click a button. Can you propose me something that can work?

1 Answer 1

2

Downloading a file from S3 is done through the GetObject method of the SDK.

var params = {
  Bucket: 'STRING_VALUE', /* required */
  Key: 'STRING_VALUE', /* required */
  IfMatch: 'STRING_VALUE',
  IfModifiedSince: new Date || 'Wed Dec 31 1969 16:00:00 GMT-0800 (PST)' || 123456789,
  IfNoneMatch: 'STRING_VALUE',
  IfUnmodifiedSince: new Date || 'Wed Dec 31 1969 16:00:00 GMT-0800 (PST)' || 123456789,
  Range: 'STRING_VALUE',
  RequestPayer: 'requester',
  ResponseCacheControl: 'STRING_VALUE',
  ResponseContentDisposition: 'STRING_VALUE',
  ResponseContentEncoding: 'STRING_VALUE',
  ResponseContentLanguage: 'STRING_VALUE',
  ResponseContentType: 'STRING_VALUE',
  ResponseExpires: new Date || 'Wed Dec 31 1969 16:00:00 GMT-0800 (PST)' || 123456789,
  SSECustomerAlgorithm: 'STRING_VALUE',
  SSECustomerKey: new Buffer('...') || 'STRING_VALUE',
  SSECustomerKeyMD5: 'STRING_VALUE',
  VersionId: 'STRING_VALUE'
};
s3.getObject(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

More information about using the AWS SDK in javascript can be found here

3
  • using getObject method can download only one file? Is it possible that more than one file(object) match the the params? Sep 26, 2016 at 13:28
  • 1
    GetObject will return one file as you specify the Key in the params. Key is unique. See Object Keys for more info on keys
    – DieterC
    Sep 26, 2016 at 13:29
  • I wrote it. It returns ONE file object in the console, but I want to start the file download from the browser. How can I do it? Oct 5, 2016 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.