Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
How do I pipe a file from S3 to the client in Uint8List format in Node.js?

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..

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!