Server Side
const aws = require('aws-sdk');
router.get('/getfilefroms3', async (req, res, next) => {
aws.config.update({
secretAccessKey: config.secret_access_key,
accessKeyId: config.access_key_id,
signatureVersion: config.signature_version,
region: config.region
})
const s3 = new aws.S3({ });
var params = { Bucket: config.sample_bucket_name, Key: req.query.filename };
s3.getObject(params, function (err, data) {
if (err) {
res.status(200);
res.end('Error Fetching File');
}
else {
res.attachment(params.Key); // Set Filename
res.type(data.ContentType); // Set FileType
res.send(data.Body); // Send File Buffer
}
});
})
Client Side
If you are using Web Application you can use any HTTP REST API Client like Axios or Fetch, The Download Manager will capture the file.
curl --location --request GET 'http://localhost:5001/getfilefroms3?filename=sample.pdf'
If you are using NodeJS Application
var http = require('http');
var fs = require('fs');
var download = function (url, destination, callback) {
var file = fs.createWriteStream(destination);
http.get(url, function (response) {
response.pipe(file);
file.on('finish', function () {
file.close(callback);
});
});
}
var fileToDownload = "sample.pdf"
download("http://localhost:5001/getfilefroms3?filename=" + fileToDownload, "./" + file