I'm ultimately trying to attach a file from s3 without saving it to disc, but I'm having trouble getting the file in the first place.
Here is my code:
const s3 = require('@aws-sdk/client-s3')
const bucketParams = {
"Bucket": "test-upload",
"Key": 'test.pdf'
};
try {
const data = await client.send(new s3.GetObjectCommand(bucketParams));
await new Promise((resolve, reject) => {
data.pipe(fs.createWriteStream('example.pdf'))
.on('error', err => reject(err))
.on('close', () => resolve())
})
console.log('File saved successfully');
} catch (err) {
console.log(err);
}
The error that I'm getting is:
TypeError: data.pipe is not a function
I can see that it's saving it to disc, but it's an empty file and also not formatted correctly. What am I doing wrong and how can I ultimately save this in memory to that I can attach it to an email without having to save it to disc?
await client.send
return? Try usingdata.Body.pipe(...)
.