I am currently working on a Api just to get to know Node.js, as I am currently learning it.
I successfully created a route for uploading an mp3 file into an s3 bucket, but when I try to fetch a file from S3 in Uint8List format, I don't get the results I want. (Flutter requires me to send an Uin8List, if this is not a good solution I can also convert it into an Ui8List on the client side)
I am able to create a Readable stream, and when the stream receives chunks it logs it into the console. But I am not quite sure how I can send the data back to the client in buffers, I am only able to send the data in one big list but ofcourse for efficiency this is not the best option.
Anyone able to help me? This is the code is currently have:
var AWS = require('aws-sdk');
AWS.config.update(
{
accessKeyId: AWS_ACCESS_KEY,
secretAccessKey: AWS_SECRET_ACCESS_KEY,
region: AWS_REGION
}
);
var s3 = new AWS.S3();
router.get('/assets/:fileKey', auth, async function (req, res, next) {
try {
const fileKey = req.params.fileKey;
const options = {
Bucket: AWS_BUCKET_NAME,
Key: fileKey,
};
const chunks = [];
const getAsBytes = new Promise((resolve, reject) => {
const readStream = s3.getObject(options).createReadStream();
readStream.on('data', (chunk) => {
// console.log('-------new data received--------')
// console.log(chunk);
chunks.push(chunk);
// res.write(chunk);
});
readStream.on('error', reject)
readStream.on('end', resolve);
}).catch((err) => next(err));
await getAsBytes;
res.write(Uint8Array.from(chunks));
res.end();
} catch (error) {
next(error);
}
});
When I try to pipe the readstream I get a response full of question marks and weird symbols..