I have a need to retrieve individual files from Node.js with Express.js. For that, I have installed aws-sdk, as well as @aws-sdk/client-s3. I am able to successfully fetch the file by using this simple endpoint:
const app = express(),
{ S3Client, GetObjectCommand } = require('@aws-sdk/client-s3'),
s3 = new S3Client({ region: process.env.AWS_REGION });
app.get('/file/:filePath', async (req,res) => {
const path_to_file = req.params.filePath;
try {
const data = await s3.send(new GetObjectCommand({ Bucket: process.env.AWS_BUCKET, Key: path_to_file }));
console.log("Success", data);
} catch (err) {
console.log("Error", err);
}
});
...but I have no idea how to return the data correctly to the React.js frontend so that the file can be further downloaded. I tried to look up the documentation, but it's looking too messy for me, and I can't even get what does the function return. .toString() method didn't help because it simply returns `"[object Object]" and nothing really else.
On React, I am using a library file-saver, which works with blobs and provides them for download using a filename defined by user.
Node v15.8.0, React v16.4.0, @aws-sdk/client-s3 v3.9.0, file-saver v2.0.5
Thanks for your tips and advices! Any help is highly appreciated.