Question:
I want to automatically save the file im downloading from AWS S3 to my application folder. Right now this can be done manually. Code below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
router.get("/download", (req, res) => { //File S3 URL var fileKey = "key"; AWS.config.update({ accessKeyId: IAM_USER_KEY, secretAccessKey: IAM_USER_SECRET, Bucket: BUCKET_NAME }); var s3 = new AWS.S3(); var file = fs.createWriteStream("test.csv"); var options = { Bucket: "name", Key: fileKey }; res.attachment(fileKey); var fileStream = s3 .getObject(options) .createReadStream() .on("error", error => { console.log(error); res.json({ error: "An error has occured, check console." }); }) .on("httpData", function(data) { file.write(data); }) .on("httpDone", function() { file.end(); }); fileStream.pipe(res); // fse.writeFileSync("text.csv"); }); |
As mentioned before, the file can be download and saved manually. But how can write the file and save it automatically in an specific folder?
Thank you
Answer:
Here’s an example of downloading an S3 object to a specific local file:
1 2 3 4 5 6 |
const AWS = require('aws-sdk'); var s3 = new AWS.S3({apiVersion: '2006-03-01'}); var params = {Bucket: 'mybucket', Key: 'test.csv'}; var file = require('fs').createWriteStream('/tmp/test.csv'); s3.getObject(params).createReadStream().pipe(file); |