I am trying to download file from Amazon S3 bucket from my Node.js hosted application.
var folderpath= process.env.HOME || process.env.USERPROFILE // tried using os.homedir() also
var filename = 'ABC.jpg';
var filepath = 'ABC';
AWS.config.update({
accessKeyId: "XXX",
secretAccessKey: "XXX",
region: 'ap-southeast-1'
});
var DOWNLOAD_DIR = path.join(folderpath, 'Downloads/');
var s3 = new AWS.S3();
var s3Params = {Bucket: filepath,Key: filename, };
var file = require('fs').createWriteStream(DOWNLOAD_DIR+ filename);
s3.getObject(s3Params).createReadStream().pipe(file);
This code works fine on localhost but does not work from instance because on instance folderpath returns "/home/ec2-user" instead of path of downloads folder of user machine i.e something like "C:\Users\name".
Please suggest me how can I download file to user's machine? How to get path of user's home directory from ec2 instance?
Thank you.
You can use express to create http server and APIs. You can find numerous tutorials on get started with Express.js. After the initial setup of express.js is done, you can do something like this in node.js code:
Once this is done, you can call this API
/download
, and then download the file on user's machine. Depending on the framework or library (or plain javascript) which you are using in front end, you can download the file using this/download
api. Just google, how to download file using XYZ (framework).