Beginner
Beginner

Reputation: 9125

returning status once the file is written to local from s3 bucket

trying to fetch a file from s3 bucket and storing it on the local, once its written to the local reading the file from the local and converting the data to json format and sending it.

i need to check whether the file is downloaded and written to local, once the file exist only read and convert it to json else send an error message.

once the file is on open i am writing the file and making end. So after end i can't send a return value. So how i can solve this one and use try catch to send proper error message.

const fetchFileDownloadAndWriteIt = () => {
 let Bucket = "DataBucket";
 let filename = "sample_data.csv";
 let s3 = new AWS.S3();
 const params = {
   Bucket: Bucket,
   Key: filename
 };

 return s3.getObject(params)
          .promise()
          .then(data => {
            const file = fs.createWriteStream('./localdata/' + filename);
            file.on("open", () => {
             file.write(data.Body);
             file.end();  
            })
            .on("error", err => {
               console.log("Error Occured while writing", err.message)
             })
          })
            .catch(err => {
             console.log("unable to fetch file from s3 Bucket", err.message)
          })


}


exports.fetchData = async (req,res) => {
  let fileDownloadAndWrite = await fetchFileAndDownloadWriteIt();
  // need to check file is downloaded and written properly
  const path = "./localdata/sample_data.csv";
  const json = await csv().fromFile(path);
  res.send({data: json})

}

Upvotes: 1

Views: 71

Answers (1)

vibhor1997a
vibhor1997a

Reputation: 2376

You can return a new Promise instead of the one instead of the one you get by calling the SDK's API.

return new Promise((res, rej) => {
    s3.getObject(params)
        .promise()
        .then(data => {
            const file = fs.createWriteStream('./localdata/' + filename);
            file
                .on("open", () => {
                    file.write(data.Body);
                    file.end();
                    //success
                    res();
                })
                .on("error", err => {
                    rej(err);
                })
        })
        .catch(err => {
            rej(err);
        })
});

This will resolve to undefined and rejected with the proper error occured, like while writing file, etc.

How to Call it in your handler?

Something like this would be fine.

exports.fetchData = async (req, res, next) => {
    try {
        await fetchFileDownloadAndWriteIt();
        // need to check file is downloaded and written properly - here the file is actually downloaded and written properly.
        const path = "./localdata/sample_data.csv";
        const json = await csv().fromFile(path);
        res.send({ data: json })
    }
    catch (err) {
        return next(err);
    }
}

Upvotes: 1

Related Questions