Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda