I have a function that is from Amazon's S3, in a file called aws-s3.js.
const AWS = require('aws-sdk');
const fs = require('fs');
const AWSCredentials = {
accessKey: process.env.AWSAccessKeyId,
secret: process.env.AWSSecretKey,
bucketName: 'YOUR_S3_BUCKET_NAME'
};
const s3 = new AWS.S3({
accessKeyId: AWSCredentials.accessKey,
secretAccessKey: AWSCredentials.secret
});
const uploadToS3 = (fileName) => {
// Read content from the file
const fileContent = fs.readFileSync(fileName.path);
// Setting up S3 upload parameters
const params = {
Bucket: AWSCredentials.bucketName,
Key: fileName.name,
Body: fileContent
};
s3.upload(params, function(err, data) {
if (err) {
throw err;
}
console.log(`File uploaded successfully. ${data.Location}`);
return data.Location;
});
};
module.exports= uploadToS3;
I need to return to another file (methods.js) the result of the execution of this, that is, if there is an error, it returns a false or otherwise, it returns the path where the file was left to register it in bd, I also leave the methods.js code
let _insert = async function (req, res, next){
try{
const { params } = req;
if(typeof req.files.detalle_comercio_adherido === 'object'){
// await upload(req.files.detalle_comercio_adherido);
let upload = uploadToS3(req.files.detalle_comercio_adherido);
let result = await comAdhServices.insertComAdh(params,new_path);
if(result === null){
res.json(httpStatus.NOT_FOUND);
res.end();
return;
}
res.json(httpStatus.CREATED, result[0]);
res.end();
}
}catch(err){
res.send(httpStatus.INTERNAL_SERVER_ERROR, JSON.stringify({Error: httpStatus.INTERNAL_SERVER_ERROR, Message: constants.Error.INTERNALERROR}) );
}
};
Unfortunately, the value always comes to me as undefined in the upload variable, what should be the correct way to capture the value returned by the s3 function?