Search code examples
javascriptamazon-web-servicesamazon-s3download

Javascript to download a file from amazon s3 bucket?


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.

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.

Thanks,


Solution

  • Maybe you can use AWS Node.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
        }
      }
    );