'how to copy data from a file under /tmp in lambda to a s3 in aws?

I am saving a data in a text file under /tmp folder , now I want to copy data from sample.txt to a file in s3. Can I directly copy the sample.txt file to a s3 bucket. Or is it possible to write to a s3 bucket in a stream, like I'm doing here to save it to /tmp/sample.txt file ?

exports.handler = async (event) => {

  const wStream = fs.createWriteStream('/tmp/sample.txt');
  const filePath = wStream.path;

  getArrayData(args)
  .then(function(data){
    data.forEach(value => wStream.write(value);)
    wStream.on('finish', () => {console.log('file saved');});
    wStream.on('error', (e) => {console.log(e);});
    wStream.end();
  }).catch(error => console.log(error));

  //read the file and save it to s3
  fs.readFile('/tmp/sample.txt', (err, result)=> {
    if(error){ console.log(error);}

    ....
  }
};


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source